A backtest can look highly profitable and still fail in live trading if it ignores commissions, bid-ask spreads, slippage, market impact, latency, and position limits. This guide explains how to backtest a trading bot with repeatable cost assumptions, compare gross and net performance, test for overfitting, and decide when the analysis needs to be rerun.
Overview
Backtesting applies a trading bot's rules to historical market data to estimate how the strategy might have behaved. It is useful for checking whether entry and exit rules are internally consistent, measuring drawdowns, and comparing results with a simple benchmark. It is not proof that the bot will make money in the future.
The most common backtesting mistake is to treat every historical fill as if it occurred at the displayed market price with no friction. In practice, a strategy may pay a commission, cross the spread, receive a worse price during a fast move, move the market with a large order, or miss a trade because an order arrived too late. A realistic trading strategy backtest models these effects explicitly and reports results after costs.
Use the process below as a repeatable audit rather than a one-time search for the best parameter setting. Keep a record of the data version, bot rules, assumptions, and output so that a later result can be compared with the original.
For related validation work, see How to Backtest a Trading Strategy Without Overfitting. Once a bot is running, the Trading Bot Performance Audit Kit can help compare live and simulated results.
How to estimate
Start with the bot's unambiguous rules. Define the market, timeframe, trading hours, order type, position-sizing method, stop or exit logic, and treatment of dividends, splits, funding, or other instrument-specific events. A rule that cannot be expressed consistently in historical data is a source of hidden discretion.
1. Calculate gross trade results
For each completed trade, calculate the result before trading costs. For a long position, a simple approximation is:
Gross profit = (exit price − entry price) × quantity
For a short position, reverse the price difference. Add income such as dividends only when the bot could actually have been entitled to it, and account for corporate actions using appropriately adjusted or unadjusted data. Do not mix price series without documenting the choice.
2. Estimate each cost separately
Model costs per order rather than applying one unexplained percentage to the final return. A basic net result is:
Net profit = gross profit − commissions − spread cost − slippage − market impact − financing or borrow costs
The exact categories depend on the instrument and broker. If a cost cannot be estimated precisely, use a range and run conservative, base, and favourable cases. A strategy that works only under the favourable case deserves additional scrutiny.
3. Rebuild the equity curve
Apply each trade's net result in chronological order to produce an equity curve. From that curve, calculate total return, annualised return where appropriate, maximum drawdown, losing streaks, trade count, average trade, profit factor, exposure, turnover, and periods of inactivity. Compare the bot with a relevant buy-and-hold or cash benchmark, using the same period and a consistent treatment of distributions.
4. Test unseen data
Separate development data from evaluation data. Use the first period to design rules and reserve later observations for a final test that is not repeatedly used to tune parameters. Walk-forward testing can provide a more realistic process: train or select settings on one window, test them on the next, then advance the window. This helps reveal whether a strategy depends on one unusually favourable period.
Inputs and assumptions
Document the following inputs before reviewing the results. The objective is not to predict the exact future cost, but to make the estimate visible and reproducible.
| Input | What to record | Why it matters |
|---|---|---|
| Commission | Per-share, per-contract, percentage, or fixed order charge | Frequent trading can turn small charges into a material drag. |
| Bid-ask spread | Spread at entry and exit, preferably by time of day | A market order may buy near the ask and sell near the bid. |
| Slippage | Expected difference between the signal price and fill price | Fast markets and thin liquidity can produce worse fills. |
| Market impact | Extra price movement caused by order size relative to available volume | A historical quote may not support the bot's full order. |
| Latency | Delay between signal, order submission, and execution | The available price may change before the order arrives. |
| Position sizing | Fixed quantity, fixed value, volatility target, or portfolio percentage | Size affects risk, turnover, and market impact. |
| Data quality | Timestamp, corporate-action treatment, missing records, and survivorship rules | Bad or incomplete data can create artificial entries and exits. |
| Risk limits | Maximum position, leverage, daily loss, and concurrent trades | Limits can change both returns and drawdown. |
For spread-sensitive strategies, use bid and ask data when available instead of assuming that the last traded price was executable. If only bar data is available, state whether fills occur at the open, close, or an assumed intrabar price. Avoid assuming that a stop was filled at its exact trigger during a gap; a scenario range may be more defensible.
Also check for look-ahead bias. The bot must use only information available at the decision timestamp. Indicators calculated with a later closing price, revised fundamentals, or a data field that includes future adjustments can make a backtest appear stronger than a live implementation could have been.
Worked examples
Suppose a bot buys 100 shares at a signal price of 50 and later sells at 51. Before costs, the trade produces 100 dollars of gross profit. Assume, purely as an example, that the combined entry and exit commission is 4 dollars, the spread and execution effects total 8 dollars, and additional slippage is estimated at 6 dollars. The estimated net profit is:
100 − 4 − 8 − 6 = 82 dollars
This is a single-trade illustration, not a forecast or a fee schedule. In a full backtest, apply the assumptions to every entry and exit. If the bot completes 500 similar trades, a 22-dollar cost per round trip would represent 11,000 dollars before considering changes in trade size, spread, or liquidity. That is why gross profit per trade should be compared with total estimated friction.
Next, run three cases:
- Favourable: lower assumed spread and slippage, with fills close to the signal price.
- Base: assumptions judged most representative of the intended broker, instrument, and trading hours.
- Conservative: wider spreads, greater slippage, and a delay between signal and fill.
Suppose the bot has a positive return in all three cases but experiences a maximum drawdown that exceeds the intended risk limit. The result is not automatically acceptable. Position sizing, leverage, or a shutdown rule may need adjustment. Conversely, a strategy with a modest net return and manageable drawdown may be more usable than one with a higher gross return that disappears after costs.
Compare the net equity curve with a simple benchmark and inspect the worst months, not only the final percentage return. You can also review whether performance depends on a narrow group of trades, one market regime, or a handful of unusually large winners. Such concentration is a reason to reduce confidence and seek more out-of-sample evidence.
When to recalculate
Recalculate the backtest whenever an input that affects execution or risk changes. That includes broker pricing, exchange or venue rules, order types, trading hours, margin terms, borrow availability, data vendor coverage, and the bot's timeframe or position size. A change in spread assumptions alone can materially alter a high-turnover strategy.
Re-run the analysis when new historical data is added, a corporate action changes the price series, or the bot receives a new entry, exit, filter, or parameter. Do not simply append new results to an old report if the methodology has changed; preserve the old version and label the new assumptions.
Once live trading begins, compare actual fills with the backtest's expected fills. Track realised commission, average spread paid, slippage by order type, missed orders, latency, turnover, drawdown, and net return. If live costs are consistently higher, update the model and reassess the strategy rather than widening assumptions until the result looks acceptable.
A practical review checklist is: save the data snapshot; record every cost assumption; test base and conservative cases; separate development from evaluation data; compare against a benchmark; inspect drawdown and losing streaks; and rerun after any material change. Backtesting cannot remove uncertainty, but transparent assumptions can show whether a trading bot's apparent edge survives the conditions it is likely to face.