Backtesting
See what your algorithm would have done, using the same code that trades it live.
A backtest replays past prices through your algorithm and records every position it opens. Use it to find bugs, to compare versions and settings, and to throw out ideas that never worked.
What it does
Section titled “What it does”- Runs any version of an algorithm on one instrument, over a date range you choose, at an interval from 1 min to 1 day.
- Replays prices at one of two resolutions: Every tick (each recorded price change) or Minute data (four prices per minute: open, high, low and close).
- Fills orders against the recorded bid and ask, and applies stop losses, take profits, trailing stops and margin as a broker would.
- Shows the result live as it runs: a Dashboard of balance, equity, profit, return, maximum drawdown, position count, realized pips and daily and monthly Sharpe and Sortino ratios; eighteen Analysis charts, such as profit by hour of the day and by day of the week; an equity chart; a price chart with every position and anything you
plot(); the Positions list, which exports to CSV; and the Log. - Keeps every backtest in the algorithm’s Backtest tab, where you can Run Again, Stop one in progress, Delete one, or start an optimization or a trading session from its settings.
How it works
Section titled “How it works”- On the algorithm’s Backtest tab, select New Backtest.
- Choose the Version:, Instrument:, Interval:, Resolution:, start and end dates and starting Balance:, and set any Externals. The window shows how many backtest compute units it will use.
- Select OK. The backtest is queued and its result page fills in as it runs.
Your algorithm runs in the same runtime as a live trading session; only the broker behind it is emulated. Its indicators are warmed up with prices from before the start date, so they have values from the first tick. If the algorithm throws an error, the backtest stops at once and the error and its stack trace are in the Log.
To debug, write to the log with log() and draw values on the price chart with plot().
Limits
Section titled “Limits”- Fills have no slippage, commission or financing. Live results will be somewhat worse. See Backtesting honestly.
- Minute data cannot tell whether the high or the low came first in a minute, so stops and targets inside one minute can trigger in the wrong order. Check close stops on Every tick.
- Dates go back at most 10 years and end no later than yesterday. How far back an instrument goes depends on the instrument; the window warns you if you start before its data begins.
- The backtest account is in US dollars.
- A backtest stops after 30 minutes of running, after 500 positions, or if the algorithm requests 50 positions within one minute.
plot()takes up to 10 values per call and 50 series per backtest.- If a backtest is stopped, fails or errors, the compute units for the part that did not run are returned.
Every backtest uses backtest compute units: one unit for every 30 days on minute data, or every 3 days on every tick. Every plan includes backtesting on Every tick.
Read next
Section titled “Read next”- Optimization: run many backtests over ranges of settings.
- Backtesting honestly: what a result can and cannot tell you.