# How to Backtest a Trading Strategy (Manually and on TradingView)

Web page: https://uptickflow.com/en/learn/how-to-backtest-a-trading-strategy  
Updated: 2026-10-17

How to backtest a trading strategy: write exact rules, test by hand with Bar Replay or with the Strategy Tester, read the report, and avoid overfitting.

To backtest a trading strategy, write its rules so precisely that two people would take the same trades, apply them to past price data, by hand with TradingView’s Bar Replay or automatically with a Pine Script strategy in the Strategy Tester, include realistic costs and record every trade. Then forward test the same rules on new data or a paper account before you risk money.

A backtest shows how a set of rules would have behaved in the past; it does not predict future results. Examples are hypothetical, and trading carries real risk.

## 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](https://uptickflow.com/en/learn/trading-plan) covers.

Hypothetical, a precise version of an [opening range breakout](https://uptickflow.com/en/learn/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](https://uptickflow.com/en/learn/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](https://uptickflow.com/en/learn/tradingview-bar-replay) 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.

## 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.

## 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](https://uptickflow.com/en/learn/multi-timeframe-analysis).
- Repainting: signals that move or vanish after the fact make history look better than live trading. The [repainting guide](https://uptickflow.com/en/learn/tradingview-repainting) 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.

## 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](https://uptickflow.com/en/learn/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 optional results table of [UpTickFlow](https://uptickflow.com/en/indicator), our own invite-only indicator: it is a simulation on your chart’s bars, and past results do not predict future results.

## Key takeaways

- Write rules so exact that every trade in the test is one you really would have taken: market, trigger, stop, exit, size and filters.
- Test judgment-based rules by hand with Bar Replay and coded rules with the Strategy Tester, with commission and slippage entered.
- Read net profit, max drawdown, profit factor and the number of trades together: a smooth curve on few trades is a warning, not a result.
- Guard against overfitting, look-ahead and repainting, then forward test on new data and journal the results before trading real money.

## FAQ

**How many trades do you need for a backtest?**

There is no magic number, but a handful of trades proves nothing. The more trades, and the more varied the market conditions they cover, the more the result can tell you; a few dozen is a small first sample, not proof.

**Is backtesting on TradingView free?**

The Strategy Tester works on every plan, including the free one, over the history loaded on the chart. Testing over all stored history, called Deep Backtesting, needs Premium or higher, and intraday Bar Replay needs a paid plan. Plans change, so check TradingView’s current comparison.

**Does a good backtest mean the strategy will work?**

No. A backtest shows how rules behaved on past data, with the costs and fills you assumed. Overfitting, look-ahead, repainting and changing markets can all make live results worse, which is why a forward test on new data comes before real money.

---

Educational content, not financial advice. Trading involves risk of loss. https://uptickflow.com/llms.txt
