Guides

Scripts

Backtesting

A backtest runs a strategy on the bars of the chart in front and fills its orders in a simulated broker. It runs in your browser, on every plan, as often as you want.

Run a backtest

  1. Compile a strategy: a script that calls strategy().
  2. Keep a chart in front with the instrument and the interval to test.
  3. In Backtest, choose Test. The panel says which market, interval and capital it tested, and when the script was compiled.

What it takes from the strategy

The broker takes the initial capital, the order size, pyramiding, commission and slippage that strategy() declares, so a strategy tests the same in any account.

  • Market orders fill at the next open, or at the close with process_orders_on_close.
  • Limit, stop and stop-limit orders fill when a bar reaches them, at the open when the bar gaps past them.
  • Inside a bar, the price goes first to the extreme nearer the open, then to the other.
  • strategy.exit with target, stop and trailing stop, partial closes and reversals.

The report

Net result, Max drawdown, Win rate, Profit factor, and the list of Trades with their entries and exits. A position still open when the bars end is reported apart: the result counts what closed, and the drawdown counts everything.

What a backtest is not

A backtest uses the bars of the chart, as far back as your plan's history goes. It does not see the order book or the queue of the exchange, so a real fill can differ, above all with large sizes or thin markets.