Strategy scripts
strategy() instead of indicator(), then strategy.entry, exit and close. Sizes are in lots.
A strategy is a script with a strategy(...) header. Its strategy.* calls never reach a broker from the script: each becomes an order intent that the chart's trading controller handles. The same script backtests, paper trades and trades live without edits.
//@oxscript=1
strategy("Supertrend SL/TP", overlay=true, initial_capital=500000, default_qty_type=strategy.fixed, default_qty_value=1, commission_type=strategy.commission.percent, commission_value=0.01)
factor = input.float(3.0, "Factor", step=0.1)
atrLen = input.int(10, "ATR length")
stopTicks = input.int(200, "Stop (ticks)", minval=1)
targetTicks = input.int(400, "Target (ticks)", minval=1)
[st, dir] = ta.supertrend(factor, atrLen)
if ta.change(dir) < 0
strategy.entry("Long", strategy.long)
strategy.exit("Long exit", from_entry="Long", profit=targetTicks, loss=stopTicks)
if ta.change(dir) > 0
strategy.entry("Short", strategy.short)
strategy.exit("Short exit", from_entry="Short", profit=targetTicks, loss=stopTicks)
plot(dir < 0 ? st : na, "Up trend", color=color.green)
plot(dir > 0 ? st : na, "Down trend", color=color.red)| Call | Does |
|---|---|
strategy.entry(id, strategy.long) | Opens or adds to a position. An entry in the opposite direction reverses. |
strategy.exit(id, from_entry=, profit=, loss=) | Attaches a target and a stop, in ticks from the entry (or limit= / stop= as prices). They become the position's TP and SL. |
strategy.close(id) / strategy.close_all() | Closes at market. |
strategy.order(...) | A raw order with no position logic. |
strategy.cancel(id) / strategy.cancel_all() | Cancels resting orders. |
strategy.position_size, strategy.equity… | Read the account: the broker's truth, not the script's own bookkeeping. |
Sizing is in lots
When the symbol trades in lots, default_qty_value=1 and qty=2 mean one and two lots; the broker receives lots × lot size. syminfo.lotsize reads the size. On a symbol without a lot size the numbers are plain units. strategy.cash and strategy.percent_of_equity size by money instead.
- Header settings:
initial_capital,commission_type/commission_value,slippage(ticks),pyramiding(same-direction entries allowed),process_orders_on_close. - Market orders in a backtest fill at the next bar's open, or at the bar's close with
process_orders_on_close=true. - The Library's Strategies group has three to start from: SMA cross, Supertrend with SL/TP and RSI mean reversion.