Backtesting runs a trading rule over historical prices as if it had traded at the time, and reports what it would have earned, lost and risked. A useful backtest lets the rule see only data that existed at each moment, fills its orders at prices that were actually available, and charges the costs a real account would have paid.
The four parts of a backtest
- Historical data. Bars of open, high, low, close and volume, or ticks, for each instrument the rule trades, plus any other series it reads, such as earnings dates or an index level.
- The rule. Code that looks at the data up to now and decides whether to buy, sell or do nothing. It might be a moving-average crossover, a mean-reversion signal or a model's forecast.
- A simulated broker. It decides when and at what price each order fills: a market order at the next price, a limit order only if price reaches it, a stop when price trades through it. It charges spread, slippage and commission.
- A report. Net profit, drawdown, win rate, Sharpe ratio and the list of trades. See how to read a backtest report.
What a backtest can tell you
- Whether an idea made money in the past, after costs, and how much it risked to do so.
- How the result depends on its parameters, costs and market conditions.
- What to expect from a live account: how often it trades, how long trades last, how deep drawdowns go.
A backtest cannot prove a rule will work in the future. It can show that a rule did not work, or that it worked only under assumptions a live account will not meet, before real money finds out.
What makes a backtest trustworthy
- No look-ahead. Each decision uses only bars that had closed. See look-ahead bias.
- Realistic fills. An order placed on a bar's close fills on the next bar, at the bid or ask, with slippage. See how realistic slippage and spread need to be.
- Complete data. Delisted stocks stay in the universe. See survivorship bias.
- Data held back. Parameters chosen on one period and checked on another. See in-sample and out-of-sample splits.
- Repeatable runs. The same inputs give the same result every time, so a change in the result means a change in the strategy. See what makes a backtest deterministic.
Two ways to build one
- Vectorised. Signals and returns are computed over whole arrays at once, for example in pandas. It is fast and good for a first look, but orders, stops and partial fills are hard to express, and it is easy to shift a column the wrong way and see the future.
- Event-driven. The engine steps through time one bar at a time, calls the rule, and fills its orders on the following bars. It is closer to live trading, and the same rule can later run live.
How Reamer Research handles it
Reamer Research is an event-driven backtesting and live trading engine for Python and C++. Your rule is an on_bar that sees a window of closed bars and returns its orders; the engine fills them from the next bar under a public execution specification:
import reamer_research as rr
def on_bar(d): # called once per closed bar
if d.close[0] == 0: # window still filling
return None
fast, slow = d.close[-10:].mean(), d.close.mean()
if d.position() == 0 and fast > slow:
return rr.buy_market(10) # fills from the next bar
if d.position() > 0 and fast < slow:
return rr.close_position()
result = rr.backtest(on_bar, 'data/SPY.bin', lookback=50,
spread=0.01, commission_per_unit=0.005)
print(result.net_pnl, result.sharpe_ratio, result.equity_drawdown_relative)
The same rule can be written as a class or as an object configured with its parameters. open_viewer=True opens the visual trade diagnostics tool on the finished run, with each trade on its candles beside the values the strategy printed.
Limits to know
- Data is yours to bring. The kit reads CSV,
.bin, Parquet and pandas, and includes fetchers for Alpaca and Databento bars. - Evaluation Mode runs 30 days or 50,000 bars of one ticker. Enough to learn the engine on your own data; longer and multi-ticker runs need a licence.
A $20 month includes the full kit, so you can backtest your own rules on your own data and cancel if it does not fit.