Skip to content
Back to App

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:

FunctionWhat 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:

ParameterDefaultMeaning
initial_capital1000000Starting account balance for the simulation
pyramiding0Maximum additional entries in the same direction
default_qty_value1Order quantity when a call does not pass qty
slippage0Slippage in ticks, applied to market and stop fills
commission_valueCommission amount, interpreted per commission_type
commission_typestrategy.commission.fixed, strategy.commission.percent, strategy.commission.cash_per_contract, or strategy.commission.cash_per_order
process_orders_on_closefalseProcess 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 pyramiding cap are ignored.
  • Commission and slippage are applied to every fill per your declaration settings.
  • process_orders_on_close shifts 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:

VariableMeaning
strategy.position_sizeCurrent position size (negative when short)
strategy.position_avg_priceAverage entry price of the open position
strategy.equityCurrent equity (capital + closed and open profit)
strategy.netprofitRealized net profit
strategy.openprofitUnrealized profit on the open position
strategy.initial_capitalThe declared starting capital
strategy.opentradesNumber of open trades
strategy.closedtradesNumber of closed trades

Per-trade accessors are also supported:

  • Open tradesstrategy.opentrades.entry_id(), .entry_price(), .entry_time(), .size(), .profit()
  • Closed tradesstrategy.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.

Next steps