Strategies
Use strategy() in AlgoCode to backtest entries and exits bar-by-bar. The Strategy Tester panel shows equity, trades, and drawdown. Eligible scripts can be published to live algo trading.
Minimal strategy example
//@version=6
strategy("MA Cross", overlay=true, initial_capital=100000)
fastLen = input.int(9, "Fast MA")
slowLen = input.int(21, "Slow MA")
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
plot(fast, color=color.green)
plot(slow, color=color.red)
longCond = ta.crossover(fast, slow)
shortCond = ta.crossunder(fast, slow)
if longCond
strategy.entry("Long", strategy.long)
if shortCond
strategy.close("Long")Supported strategy API
strategy()— header withinitial_capital,commission_*,pyramiding,slippagestrategy.entry()— market/limit/stop entries with pyramidingstrategy.close()/strategy.close_all()strategy.exit()— limit/stop exits inside the script (used when risk mode is "Script manages exits" — not withalgo.risk)algo.risk()/algo.stoploss()/algo.target()— platform SL/target for live trading (see Risk management)
Strategy Tester
- Run a
strategy()script on the chart. - Open the Strategy Tester panel below the chart.
- Review net profit, win rate, trade list, and per-trade entry/exit prices.
Tester results use script logic and strategy() header settings. algo.risk does not change tester trades — it applies only after you publish to live algo trading, where the platform monitors LTP using the values from algo.risk or the publish modal.
Platform risk in code — algo.risk
atrSl = ta.atr(14) * 2 algo.risk(stoploss = atrSl, type = algo.points, target = 20)
Type constants: algo.points, algo.percent, algo.trailing, algo.break_even, algo.off. Trailing uses named args trail_steps and trail_by. Full reference on the Risk management page.
Publish to Algo Trading
When your script contains strategy(), the chart shows a rocket icon. Click it to open the publish modal (no 4-step wizard).
- Identity — display name, symbol (from chart), linked script.
- Script inputs — AlgoCode
input()values (if any). - Broker execution — quantity, product (MIS/NRML), order type, trade type.
- Risk — stop loss, target, square-off, and risk mode (or read-only preview when
algo.riskis in the script). See Risk management. - Save or Save & Start — creates a strategy card on Algo Trading.
Risk modes (custom scripts)
| Mode | Behaviour |
|---|---|
| Platform SL & Target | Stop loss and target from the publish modal or from algo.risk in code. Live exits use platform LTP checks. strategy.exit stop fields are ignored when algo.risk is used. |
| Script manages exits | AlgoCode strategy.close / strategy.exit drive live exits. Cannot be used together with algo.risk. Platform SL fields are disabled. |
| Signal only | No automatic SL/target. Exit on reverse signal or square-off time only. |
Built-in algo strategies
RSI, MACD, SuperTrend, and other built-in strategies use the standard 4-step create wizard on the Algo Trading page. Step 3 is where you set stop loss, target, timeframe, and square-off — same fields as the publish modal.
Evaluation
- Custom AlgoCode strategies evaluate on bar close only.
- Timeframe is taken from the chart interval when publishing (1m, 5m, 15m, etc.).
- Press Start on the strategy card to begin live monitoring.
indicator()) cannot be published to algo trading. Add strategy() or use Bridge webhooks for external signals.