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.riskin 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)
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 / constant | Meaning |
|---|---|
| stoploss | Points or % value (can be dynamic, e.g. ta.atr(14) * 2) |
| type = algo.points | Fixed points from entry (UI: Points) |
| type = algo.percent | Percent of entry price (UI: Percentage) |
| type = algo.trailing | Trailing SL — also set trail_steps and trail_by |
| type = algo.break_even | Trailing break-even SL |
| type = algo.off | No platform stop loss |
| target | Target 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.
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 field | How it works |
|---|---|---|
| Off | — | No platform stop loss. Position closes on reverse signal, script exit, or square-off. |
| Points | e.g. 10 | Fixed points from entry. Buy: SL = entry − points. Sell: SL = entry + points. |
| Percentage | e.g. 2 | Percent of entry price. Buy: SL = entry − (entry × % / 100). |
| Trailing Break Even | e.g. 10 | Initial SL at entry ± points. After price moves past the entry candle high/low, SL trails using candle extremes minus/plus the stoploss offset. |
| Trailing Steps | e.g. 10,20,5 | Comma-separated: initialSL, stepSize, trailBy. SL ratchets in steps as price moves in your favour. |
| Trailing (steps + trail) | e.g. 10 | Separate 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 reason | Trigger |
|---|---|
| Stop loss | Live LTP crosses calculated SL level |
| Target | LTP reaches entry ± target points |
| Reverse signal | Opposite entry signal while position is open |
| Script risk (algo.risk) | Same as stop loss / target — values from algo.risk synced to platform |
| Script exit | AlgoCode strategy.close / strategy.exit (script risk mode only) |
| Square-off | Configured square-off time reached — position closed and strategy paused |
| Manual Stop | User 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.