Step 1: write rules a computer could follow

A backtest is only as good as its rules. Buy strong breakouts is an idea; a rule says exactly what strong and breakout mean, so that every trade in the test is one you really would have taken. A complete rule set covers the decisions in the list below, the same ones a trading plan covers.

Hypothetical, a precise version of an opening range breakout: on a 5-minute chart, after 9:45 ET, buy when a candle closes above the 15-minute opening range high with price above VWAP; stop at the range low; target 2R; risk 0.5% of the account; no trades on earnings days; close any open trade at 15:55 ET.

  • Market and timeframe: which symbols, which chart and which hours.
  • Setup and trigger: the exact condition, judged on a closed candle.
  • Stop and exit: where the trade is wrong, and how it ends.
  • Size: the risk per trade and how the share count is calculated, as in the position size calculator.
  • Filters: what keeps you out, such as news days or a falling market.

Manual backtesting with Bar Replay

If your rules involve judgment, such as drawing levels, test them by hand. TradingView’s Bar Replay hides the future and plays the chart forward one bar at a time; the Bar Replay guide shows the controls. At each closed bar, decide whether the setup is there, mark the entry, stop and target before stepping, and log the result in R.

Start from random dates, include dull and choppy stretches, and log every qualifying trade, including the ones you would rather skip. A handful of trades proves nothing, so aim for a sample that covers different market conditions.

Automated backtesting with the TradingView Strategy Tester

If the rules are exact enough to code, write them as a Pine Script strategy, a script declared with strategy() instead of indicator(), and add it to a chart. The Strategy Tester panel below the chart then simulates every trade over the loaded history and reports the results. Before reading anything, open the strategy’s settings and enter realistic commission and slippage on the Properties tab; otherwise the test assumes trading is free.

The List of trades tab lets you check each trade on the chart, which is the quickest way to catch a coding mistake. Of the many numbers in the report, four answer the first questions, and none means much alone:

  • Net profit (Total P&L on the Overview tab): the result after the commission and slippage you entered.
  • Max drawdown: the largest fall in equity from a peak. Ask whether you could sit through it, in money and in time, on a real account.
  • Profit factor: gross profit divided by gross loss. Above 1 means the winners outweighed the losers in total, in this sample and with these costs.
  • Total trades: the sample size. A strategy with a dozen trades has barely been tested, however good the other numbers look.

Turn your phone sideways to read the drawing. Tap to close.

A hand-drawn Strategy Tester report card with no numbers: four tiles, each showing only a dash for its value, labeled net profit (after commission and slippage?), max drawdown (worst drop from a peak), profit factor (gross profit ÷ gross loss) and total trades (enough to mean anything?), under a header note reading read them together, never one alone. Below, a green equity curve with a red hatched dip labeled drawdown.

Overfitting: why a perfect backtest is a warning sign

Overfitting, or curve-fitting, means tuning the rules until they match the noise of the past. Every extra condition and every tweaked setting can improve the backtest, and each one makes the result less likely to repeat. The drawing below shows the typical outcome: a smooth equity curve on the data the rules were tuned on, then a flat or falling line on new data. Defences that help:

  • Keep the rules few and the settings round. A 20-period average is easier to trust than a 17-period one picked by an optimizer.
  • Hold data back. Tune on one stretch of history, then run the unchanged rules once on a later stretch they have never seen (out-of-sample).
  • Check the neighbours. If a setting of 20 works but 18 and 22 fall apart, the edge is probably noise.
  • Test other symbols and periods, including a sideways market, and count how many versions you tried: the best of many tries looks better than it is.

Turn your phone sideways to read the drawing. Tap to close.

Two hand-drawn equity curves over time, split by a dashed vertical line into in-sample: rules tuned here (left) and out-of-sample: new data (right, shaded). The red over-fitted curve rises smoothly and steeply up to the line, labeled too perfect, then zigzags lower on new data. The green curve of simple rules is bumpy but keeps rising slowly on both sides, labeled bumpy, but it holds up.

Other backtesting traps: look-ahead, repainting, costs and small samples

Even honest rules can produce a misleading test. The usual traps:

  • Look-ahead bias: the test uses information that was not available at the time, such as the day’s high used on a morning bar, or a higher-timeframe value taken before its bar closed, a common slip in multi-timeframe analysis.
  • Repainting: signals that move or vanish after the fact make history look better than live trading. The repainting guide shows how to test for it.
  • Costs and slippage: commission, spread and slippage are paid on both sides of every trade. Hypothetical, as in the drawing below: a rule that averages $0.05 a share per trade before costs, with $0.01 of commission and $0.02 of slippage a share on each side, nets −$0.01 a share. Fast strategies with small targets are hit hardest.
  • Fill assumptions: a strategy that buys at the close of the signal bar, or assumes every limit order fills, can look better than anything you could get.
  • Too few trades: a handful of results can be luck in either direction.
  • Survivorship bias: testing only on stocks that exist today leaves out the companies that were delisted or dropped from an index, which flatters the results.

Turn your phone sideways to read the drawing. Tap to close.

A hand-drawn waterfall chart per share and per round trip, marked hypothetical: a green bar of +0.05 before costs, a red step of −0.02 for commission and a larger red step of −0.04 for slippage + spread, ending in a small red net bar of −0.01 below the zero line.

Forward test before real money, and keep a journal

A forward test runs the unchanged rules on data that did not exist when you wrote them: in TradingView’s Paper Trading, by hand on live charts, or with a very small real size. It checks what a backtest cannot: real fills and spreads, and whether you actually follow the rules when the signals arrive.

Log every forward-test trade in R in a trading journal, and compare the results with the backtest once you have enough trades. If they are clearly worse, find the reason, such as costs, fills, a rule you bend or a market that changed, before adding money. The same checks apply to any indicator’s built-in statistics, including the results table of UpTickFlow, our own invite-only indicator: it is still a test of the past.