Strategies
A Pine strategy() script does everything an indicator does, plus places simulated orders against the replay data. When you apply a strategy, it backtests over the loaded chart history, then keeps trading live as the replay streams — with entry and exit markers on the chart and full results in the Strategy Tester tab.
Order functions
The following strategy.* order functions are supported:
| Function | What it does |
|---|---|
strategy.entry() | Opens (or reverses into) a position; opposite-direction entries reverse the position |
strategy.order() | Places an order without entry/reversal semantics |
strategy.exit() | Attaches exit orders — stop loss, take-profit limit, or both as a bracket |
strategy.close() | Closes a specific entry by ID |
strategy.close_all() | Flattens the entire position |
strategy.cancel() | Cancels a pending order by ID |
strategy.cancel_all() | Cancels all pending orders |
Use the strategy.long and strategy.short constants for direction.
Declaration settings
TestMax reads the following parameters from your strategy() declaration:
| Parameter | Default | Meaning |
|---|---|---|
initial_capital | 1000000 | Starting account balance for the simulation |
pyramiding | 0 | Maximum additional entries in the same direction |
default_qty_value | 1 | Order quantity when a call does not pass qty |
slippage | 0 | Slippage in ticks, applied to market and stop fills |
commission_value | — | Commission amount, interpreted per commission_type |
commission_type | — | strategy.commission.fixed, strategy.commission.percent, strategy.commission.cash_per_contract, or strategy.commission.cash_per_order |
process_orders_on_close | false | Process orders on the bar close instead of the next open |
Position sizing is fixed-quantity only — contracts per order via default_qty_value or the qty argument. Percent-of-equity sizing is not supported yet.
The fill model
TestMax uses a deterministic backtest model that matches TradingView’s default assumptions:
- Orders fill at the next bar’s open. An order placed during a bar’s calculation (on its close) fills at the open of the following bar. Market and stop fills have slippage applied, in ticks.
- Resting limit and stop orders fill intrabar. Once a limit or stop order is working, it fills within any later bar whose range touches its price.
- Stop fills before limit. When a bracket’s stop and take-profit limit are both touched within the same bar, the stop fills first. With only OHLC data there is no way to know which level traded first, so TestMax takes the conservative assumption — the losing exit.
- Opposite entries reverse. A
strategy.entry()in the opposite direction closes the current position and opens the new one in a single fill. - Pyramiding is enforced. Same-direction entries beyond the
pyramidingcap are ignored. - Commission and slippage are applied to every fill per your declaration settings.
process_orders_on_closeshifts fills to the closing bar’s close instead of the next open, if your script declares it.
Reading strategy state
Your script can read the simulated account and position state through the standard Pine variables:
| Variable | Meaning |
|---|---|
strategy.position_size | Current position size (negative when short) |
strategy.position_avg_price | Average entry price of the open position |
strategy.equity | Current equity (capital + closed and open profit) |
strategy.netprofit | Realized net profit |
strategy.openprofit | Unrealized profit on the open position |
strategy.initial_capital | The declared starting capital |
strategy.opentrades | Number of open trades |
strategy.closedtrades | Number of closed trades |
Per-trade accessors are also supported:
- Open trades —
strategy.opentrades.entry_id(),.entry_price(),.entry_time(),.size(),.profit() - Closed trades —
strategy.closedtrades.entry_id(),.entry_price(),.exit_price(),.profit(),.entry_time(),.exit_time(),.size()
The Strategy Tester
Applying a strategy adds a Strategy Tester tab to the editor drawer:
- Metrics row — Net P&L, total trades, win rate, max drawdown, final balance, and the current open position.
- Equity curve — A live chart of simulated equity, updating as the strategy trades through the replay.
- Trade log — Every simulated trade with its entry and exit details.
On the price chart itself, entry and exit markers show exactly where each fill happened.
Everything updates in real time: as the replay streams new candles, orders are processed, the metrics recompute, and the equity curve extends — including reacting to the still-forming candle with Pine’s open-bar semantics.