How to Backtest a Trading Strategy (And Read the Result Honestly)
What backtesting actually tells you, how to run one on TradingView, and how to read the report so a losing strategy does not look like a winning one.
Key Takeaways
- A backtest measures one sample of history, not the future.
- Read profit factor before win rate, and sample size before either.
- Exits shape results more than entries, and a time-based exit can invert a good idea.
- Lookahead bias and repainting indicators make a broken test look excellent.
- Split your data before you optimise, or you will only learn what already happened.
A backtest answers one question: what would these rules have produced over this stretch of history. That is genuinely useful. It is also much narrower than most people treat it, and the gap between those two things is where the money goes.
This covers how to run one, and then the part that gets skipped — how to read the output without fooling yourself.
What a backtest is actually measuring
You apply a fixed set of rules to historical price data and record every trade they would have taken. Out comes a list of trades and some summary statistics.
The rules have to be fixed. If you adjust them while looking at the results, you are no longer testing an idea, you are fitting one to data you have already seen. That distinction sounds pedantic until you notice how naturally it happens — you see a losing stretch, add a filter to avoid it, and the equity curve improves. It always improves. You have just learned what happened in the past, which you already knew.
Running one on TradingView
Most retail traders use TradingView, so that is the practical starting point. Write the strategy in Pine Script, add it to a chart, and the Strategy Tester panel appears at the bottom with three tabs: Overview, Performance Summary, and List of Trades.
The minimum a strategy needs to produce a testable result is an entry condition, an exit, and a position size. In Pine v6 that is roughly:
//@version=6
strategy("Range Break", overlay = true, initial_capital = 100000)
rangeHigh = ta.highest(high, 20)
breakUp = ta.crossover(close, rangeHigh)
if breakUp
strategy.entry("Long", strategy.long)
strategy.exit("Exit", from_entry = "Long", stop = close * 0.99, limit = close * 1.02)
Two things worth setting before you trust any number it gives you. In the strategy properties, set commission and slippage to something realistic for your broker and instrument — a strategy that survives at zero cost and dies at realistic cost is not a strategy. And check the order fill assumptions, because default settings can fill you at prices you would not have received.
The free plan limits how much history you get. That matters more than it sounds, and I will come back to it.
Reading the report
This is where most guides stop and most mistakes start. The Performance Summary gives you a lot of numbers. Four of them do most of the work.
Profit factor is gross profit divided by gross loss. Below 1.0 the strategy lost money over the sample, regardless of how the equity curve looks. Around 1.0 to 1.2 is fragile — realistic costs usually erase it. Above 1.5 over a large sample is worth taking seriously.
Net profit tells you the outcome but not the quality. A strategy can be net positive because of one enormous trade and be worthless.
Max drawdown is the largest peak-to-trough fall in equity. This is the number that decides whether you could actually have traded it. A strategy returning 40% with a 35% drawdown is not tradeable by most people, because they would have abandoned it partway down.
Number of trades governs how much any of the above means. Thirty trades is an anecdote. Several hundred, spanning trending and ranging conditions, starts to be evidence.
Here is a real result from a session range break strategy with risk-based sizing:
| Metric | Result |
|---|---|
| Trades | 19 |
| Net P&L | −3,030.28 (−0.30%) |
| Max drawdown | 28,267.78 (2.80%) |
| Win rate | 36.84% (7 of 19) |
| Profit factor | 0.939 |
The win rate is not the problem — plenty of profitable strategies win a third of the time because the wins are large enough. Profit factor at 0.939 is the problem: it earned 93.9 for every 100 it gave back. And with only 19 trades, even that figure is barely more than noise.
Where the money went, and why it was not a bug
The trade list explains it, and learning to read that list is worth more than any summary statistic.
The exits were not symmetrical. Several winners closed with the reason Session end at values like +119.2 and +135.4 — a fraction of what the target would have paid. The losers had no equivalent brake, because a stop is a price and price reaches it whenever it reaches it.
So the rules cut the right tail of the distribution and left the left tail intact. Position sizing made it worse: risk was sized from a reward multiple the exit rule frequently did not permit, so every trade was sized for a payoff it was not allowed to collect.
Nothing about that is a coding fault. The script compiled cleanly and did exactly what it was told. It is a rules problem, and the two need separating — if trades close for reasons you deliberately wrote, the code works and the idea needs changing. Rewriting working logic because a backtest disappointed is a common and expensive detour.
The faults that make a backtest lie
Four things produce excellent results that do not survive contact with a live market.
Lookahead bias. The strategy uses information the bar had not produced yet. In Pine this creeps in through request.security without proper barmerge settings, or by referencing a value that is only final once the bar closes. The symptom is a suspiciously smooth equity curve.
Repainting indicators. Some indicators revise their own history as new bars arrive. Backtested against their final values they look prophetic, because on the bar you would actually have traded they said something different. If an indicator's past signals change while you watch, it cannot be backtested honestly.
Survivorship and data gaps. Testing on an instrument that exists today ignores the ones that did not make it. Gaps, bad ticks and missing sessions all shift results, usually favourably.
Overfitting. The most common of the four. Every parameter you tune against the same data buys you a better result and a worse strategy.
Splitting the data, which is the part nearly everyone skips
The fix for overfitting is not restraint, it is procedure.
Divide your history before you begin. Develop and optimise on the first portion — the in-sample period. Then run the finished rules, unchanged, over the portion you never looked at. If performance holds roughly, you have learned something. If it collapses, you fitted noise, which is the normal outcome and worth knowing before you risk money.
Walk-forward testing formalises this: optimise over a window, test on the next, roll both forward, repeat. It is more work and it is the difference between a result and a guess.
Review checklist
Before you trust a backtest
- The rules were written down before the test ran
- Commission and slippage are set to something realistic
- The sample has enough trades, across more than one market condition
- Profit factor and max drawdown were read before net profit
- Exits are grouped by reason and none of them is truncating winners
- No indicator in the strategy repaints its own history
- Results held on a period you did not optimise against
What a backtest cannot tell you
It cannot tell you whether you would have traded it.
Every backtested trade assumes flawless execution by someone who never hesitated, never skipped a signal after three losses, and never widened a stop. That person is not available. The gap between a strategy's tested performance and its realised performance is mostly behavioural, and no amount of historical data measures it.
That is the argument for testing your own reading alongside your rules — going through history bar by bar with the future hidden, committing to a decision before the outcome is visible, and finding out where your judgement was wrong rather than where your syntax was. A backtest tells you whether the rules had an edge. Practice tells you whether you can follow them.
Both are worth doing. Only one of them is usually done.
Next step
Replay strategy code setups inside SkillCandle
Move from reading about the setup to actually practicing it with a partial chart, replay reveal, and tracked review notes.
FAQ
Questions traders ask about this topic
What is backtesting in trading?
Running a set of trading rules over historical price data to see what they would have produced. It tells you whether the rules were profitable over that specific sample. It does not tell you whether they will be profitable next month, and treating those as the same thing is the most common mistake.
What is a good profit factor?
Profit factor is gross profit divided by gross loss. Below 1.0 the strategy lost money. Around 1.0 to 1.2 is fragile and usually disappears once costs are included. Above 1.5 over a large sample is genuinely promising. A high figure over 20 trades means very little.
How many trades does a backtest need?
More than most people use. Thirty trades is an anecdote. A few hundred across different market conditions starts to be evidence. If your rules only trigger a handful of times a year, you need years of data rather than months.
Can I backtest for free?
Yes. TradingView's Strategy Tester runs on the free plan with limits on history and bars. Manual bar-by-bar replay costs nothing but your time and tests your judgement rather than only your rules.
Why do my backtest results look better than my live results?
Usually costs, slippage, or a rule that used information the bar had not produced yet. Repainting indicators and lookahead bias both make a backtest look excellent and are invisible unless you check for them specifically.
Related Reads
Keep building the cluster
A Strategy That Compiled Perfectly and Still Lost Money
A session range break strategy that built with zero errors and finished at 0.939 profit factor. The code was fine and the exit rule was not.
Pine Script v6 Errors That Stop Your Script Compiling
The Pine v6 mistakes that actually break a TradingView build, what each error means, and the ones that compile cleanly and still draw nothing on your chart.
Break-Even Win Rate Calculator for Risk Reward [Free Tool]
See how reward-to-risk changes the win rate your setup needs just to break even, with worked examples for 1R, 2R and 3R ideas.