Risk management

Stop loss, target, trailing stop, and square-off are enforced by the platform on live LTP after you publish. For custom AlgoCode strategies you can set them in the publish modal or in Pine using the algo.* API (see below). Built-in strategies use the create-strategy wizard (Step 3).

  • Algo Trading → Create Strategy → Step 3 (built-in strategies)
  • Chart → Publish modal (custom strategies without algo.risk in code)
  • algo.risk() in your script (custom strategies — script is the source of truth; modal fields are read-only previews)
  • Bridge strategy card (external webhook signals)
Who closes the trade?
strategy.entry / signals decide when to enter. Platform SL/target monitor live LTP (option LTP for options) and place closing orders automatically. Use algo.risk in code, the publish modal, or wizard Step 3 — or choose Script manages exits to use strategy.exit instead. See Strategies.

Risk in AlgoCode — algo.risk

Algocrab-specific functions (not TradingView Pine). They configure the same platform stop loss and target engine as the publish modal — they do not place orders inside the script. Use the algo namespace, not ta.*.

//@version=6
strategy("EMA + platform risk", overlay=true)

slPts = input.int(10, "SL Points", minval=1)
atrSl = ta.atr(14) * 2

// Single block (recommended)
algo.risk(stoploss = atrSl, type = algo.points, target = 20)

// Or shorthand:
// algo.stoploss(10, algo.points)
// algo.target(20)

fast = ta.ema(close, 12)
slow = ta.ema(close, 26)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)
Argument / constantMeaning
stoplossPoints or % value (can be dynamic, e.g. ta.atr(14) * 2)
type = algo.pointsFixed points from entry (UI: Points)
type = algo.percentPercent of entry price (UI: Percentage)
type = algo.trailingTrailing SL — also set trail_steps and trail_by
type = algo.break_evenTrailing break-even SL
type = algo.offNo platform stop loss
targetTarget in points (not absolute price)

Trailing in code

algo.risk(
    stoploss = 10,
    type = algo.trailing,
    trail_steps = 10,
    trail_by = 5,
    target = 25,
)

When algo.risk is present, the publish modal Risk section shows a read-only preview. Values are re-evaluated on each bar close if stoploss is dynamic; between bars the platform checks live LTP every second against the last synced levels.

Do not mix algo.risk with strategy.exit(..., stop=...) for live trading — platform SL from algo.risk wins. Use strategy.exit only when risk mode is Script manages exits (no algo.risk).

Stop loss types

Type (UI label)Stoploss fieldHow it works
OffNo platform stop loss. Position closes on reverse signal, script exit, or square-off.
Pointse.g. 10Fixed points from entry. Buy: SL = entry − points. Sell: SL = entry + points.
Percentagee.g. 2Percent of entry price. Buy: SL = entry − (entry × % / 100).
Trailing Break Evene.g. 10Initial SL at entry ± points. After price moves past the entry candle high/low, SL trails using candle extremes minus/plus the stoploss offset.
Trailing Stepse.g. 10,20,5Comma-separated: initialSL, stepSize, trailBy. SL ratchets in steps as price moves in your favour.
Trailing (steps + trail)e.g. 10Separate Trailing steps and Trail by fields. Each time price moves trailingSteps points in profit, SL shifts by trailBy. Rule: trailing steps > trail by.

Trailing example

Entry (BUY): 100
Stoploss (Points): 10        → initial SL at 90
Trailing steps: 10
Trail by: 5

LTP reaches 110 → SL moves to 95
LTP reaches 120 → SL moves to 100
LTP reaches 130 → SL moves to 105

Target

Toggle Target → On and enter points (not absolute price).

  • Buy: exit when LTP ≥ entry + target points
  • Sell: exit when LTP ≤ entry − target points

Target hits place a closing order and appear in strategy Logs. For options strategies, option LTP is used instead of spot.

Exit types

Exit reasonTrigger
Stop lossLive LTP crosses calculated SL level
TargetLTP reaches entry ± target points
Reverse signalOpposite entry signal while position is open
Script risk (algo.risk)Same as stop loss / target — values from algo.risk synced to platform
Script exitAlgoCode strategy.close / strategy.exit (script risk mode only)
Square-offConfigured square-off time reached — position closed and strategy paused
Manual StopUser presses Stop on strategy card — no new orders

Square-off time

Set Square Off Time in Step 3 (default 15:13 for NSE, 23:13 for MCX). When the clock passes this time:

  • Any open position is closed.
  • The strategy is paused until the next session.
  • New entry webhooks/signals are blocked until the next day.

Where to configure

Built-in strategies (wizard Step 3)

  • Stoploss dropdown + value
  • Target On/Off + value
  • Trailing steps / Trail by (when type is Trailing)
  • Square Off Time picker

Custom AlgoCode (publish modal)

  • If the script has no algo.risk: set stop loss, target, and risk mode in the Risk section (Platform / Script / Signal only).
  • If the script has algo.risk: fields are read-only previews; edit risk in Pine code.
  • Square-off time is always set in the publish modal.

Logs & monitoring

All SL, target, break-even, and square-off events are written to the strategy card Logs. If a broker rejects the closing order, read the rejection message and check product type (MIS/NRML) and quantity.

Backtest

The backtest module uses the same stop loss types and target points from Step 4 parameters. Trailing logic in backtest mirrors live trailing type behaviour. Always backtest or run a small live quantity before full deployment.