Trading Strategy Backtesting: What It Is and How to Test Without Overfitting

Cover of the article “Trading Strategy Backtesting: What It Is and How to Test Without Overfitting”: strategy rules flow into a candlestick chart and a separate independent-validation track

What is a backtest in simple terms?

TradingView defines backtesting as recreating a strategy's performance on historical data. The object being tested is not a name such as “trend trading,” but a precise set of instructions: when an entry occurs, the price at which it is considered filled, where the exit is, what position size is used, and when a trade is skipped.

If these rules are executed by a program, that is already part of algorithmic trading; a backtest is responsible only for testing them on the selected historical data.

Consider an educational rule: after a daily candle closes above the high of the previous 20 candles, open a Long position at the next available price; exit at a predefined stop-loss order or when an opposite signal occurs. Testing it also requires a source of price data, a historical period, a method for handling price gaps, a commission, and slippage. Without them, two people will obtain different results and both will call them “the same backtest.”

A backtest has three layers. The first is market data: price, time, available volume, and corporate-action adjustments. The second is the strategy logic. The third is the execution model, which determines when and at what price a simulated order could have been filled. An error in any layer changes the result.

  1. 1

    Idea

    An observation without a conclusion about profitability

  2. 2

    Rules

    Entry, exit, stop-loss, and skip conditions

  3. 3

    Data

    Instrument, timeframe, and period

  4. 4

    Execution

    Commission, slippage, and order type

  5. 5

    Run

    Every trade, without removing inconvenient cases

  6. 6

    Validation

    An untouched data segment and a version log

Each step is defined before the final statistics are viewed. If the rule changes, a new test version begins.

Diagram 1. A backtest begins before you click “Run”: first, define the version of the idea being tested.

How does a backtest differ from a forward test and a demo account?

Historical testing is convenient because it is fast. In a few minutes, you can examine numerous market episodes, compare periods, and identify an obvious weakness. However, the researcher already knows how the historical period ended and may unknowingly use that knowledge when choosing parameters.

A forward test begins after the rules have been frozen. TradingView describes it as running a strategy in real time as the chart updates. MetaTrader 5 allows the later portion of historical data to be separated for forward validation of optimization results. The general principle is the same: the validation segment must not be used to select parameters.

A demo account tests another layer: the path of a simulated order through the interface and the current price feed. It helps reveal delays, missed orders, and the behavior of different order types, but it does not create a real order queue or fully reproduce real-market liquidity. Moving to a small amount of real risk, if that is planned at all, remains a separate decision.

  1. Historical data

    Backtest

    Tests the rules on a selected past period

  2. New candles

    Forward test

    Parameters are frozen before the result appears

  3. Current market

    Demo account

    Tests the flow of simulated orders

  4. After testing

    Decision

    Accounts for discrepancies, risk, and an acceptable position size

Each subsequent stage adds new data but does not remove the limitations of the preceding one.

Diagram 2. Historical data, new data, and a demo account test different causes of discrepancies; success at one stage does not make the others unnecessary.

What makes up a sound backtest: data, rules, costs, and execution

Start with the data. Specify the ticker together with the venue, time zone, timeframe, and start and end dates. For stocks, it is important to know whether the historical data has been adjusted for splits and dividends. For futures, specify how the continuous contract was constructed. For a round-the-clock market, identify the provider from which the candles were obtained. The same symbol may have different highs and lows across data sources.

Next, rewrite the trading strategy in a form that contains no words such as “approximately,” “strong,” or “attractive.” A signal must have a computable threshold. Instead of “buy during strong growth,” write “after a close above the highest value of the previous N closed candles.” Define the exit, stop-loss order, re-entry, simultaneous signals, position size, and what to do when insufficient data is available separately.

Trading costs usually consist of more than one number. A commission may be charged on both entry and exit. The spread separates the buying and selling prices. Slippage reflects the difference between the expected execution price and the actual fill price. For high-frequency rules, even a small cost per trade can change the conclusion more than a new filter.

The execution model determines what happens within a candle. If both the stop-loss and the target were touched during the same bar, OHLC values alone are insufficient to determine which occurred first. TradingView uses a broker emulator and allows users to configure commissions, slippage, limit-order fill strictness, and delay. These settings are not cosmetic: they determine which trades appear in the report.

A minimum test specification looks like this:

  1. Name and version of the rule.
  2. Instrument, venue, data source, and time zone.
  3. Timeframe and exact period boundaries.
  4. Entry, exit, stop-loss, and skip conditions.
  5. Position size and re-entry rules.
  6. Commission, spread, slippage, and order type.
  7. Calculation timing: within the candle or after it closes.
  8. Code version or a snapshot of the settings.

If even one field changes, save a new version. Do not overwrite the old report: otherwise, the number of variants tested disappears and the risk of overfitting becomes invisible.

How to backtest manually on a chart: a step-by-step protocol

Manual testing is useful when a rule cannot yet be expressed in code or when you need to understand how it works. It is slower than automated testing, but it quickly exposes vague wording. If you evaluate two identical patterns differently, formalization is not yet complete.

First, select a portion of historical data and hide the right side of the chart using replay mode. Do not begin at a point you remember as the start of a major move. Record the rule version and all parameters in the table header. Then advance one closed candle at a time and make decisions using only the data visible to the left.

Save the same fields for every signal: date and time, signal price, assumed execution price, direction, stop-loss order, exit, commission, slippage, result after costs, and maximum adverse excursion during the trade. A separate “reason for skipping” column guards against cherry-picking: the absence of a trade also becomes an observation.

Manual test procedure:

  1. Open the chart before a period whose outcome you do not know and enable candle-by-candle replay.
  2. Record the instrument, period, timeframe, and rule version.
  3. On every closed candle, check the entry conditions in the same order.
  4. If there is a signal, record it before revealing the next candle.
  5. Apply the predefined execution model, commission, and slippage.
  6. Follow the trade through to its exit without moving the stop-loss order after the fact.
  7. Retain profitable, losing, and skipped cases.
  8. After finishing the segment, lock the table against edits and only then calculate the metrics.

A useful control technique is to repeat part of the sample several days later without looking at the first table. If the trades differ, the cause is usually an ambiguous rule. Clarify the wording and start a new version instead of editing individual rows in the old one.

What manual testing handles well and what requires automation
Suitable for
  • Understanding ambiguous parts of a trading rule
  • Testing a small set of signals using closed candles
  • Collecting examples of errors and reasons for skipping trades
Not suitable for
  • Rapidly testing dozens of parameters and markets
  • Precisely reconstructing the order of ticks within a candle
  • Eliminating human fatigue and subjective selection

Card 1. Manual testing is a tool for formalization; a large, reproducible test is better entrusted to a strategy or testing system.

Which metrics to examine: trades, drawdown, profit factor, and streaks

The number of trades is the denominator underlying every other conclusion. Five trades may produce a high success rate by chance. There is no universal “sufficient” number: it depends on the strategy's frequency, the dispersion of results, and the variety of market regimes. This is why the length of the period and the composition of the market must always be shown alongside the number.

The net result is calculated after commissions, spread, and slippage. If some costs are unknown, present separate scenarios, such as a baseline and a more stringent one, instead of using zero. The difference between them is often more informative than one optimistic figure.

Maximum drawdown is the largest decline in capital from a local peak to the subsequent trough. It shows not only the final loss but also the path taken to reach it. Two strategies with the same ending result may require entirely different abilities to withstand a decline.

Profit factor equals the sum of profits from winning trades divided by the absolute sum of losses. A value above one means that gross profit exceeded gross loss in this sample, but it guarantees nothing on new data. If there are no losing trades, the division loses its usual meaning: instead of displaying an attractive infinity symbol, show the number of trades and note that no losses were observed.

The worst losing streak helps assess a regime in which the rule temporarily stops working and the psychological burden this creates. An average result conceals this sequence. It is also worth examining the largest winning trade: if it accounts for most of the total result, robustness depends on a rare event.

Minimum results table:

  • number of all signals, executed trades, and skipped trades;
  • gross profit, gross loss, and net result after costs;
  • maximum drawdown in monetary and percentage terms;
  • profit factor with the number of trades shown alongside it;
  • percentage of profitable trades, without inferring a future probability;
  • average profit, average loss, and their dispersion;
  • worst losing streak;
  • the largest trade's contribution to the total result.

Compare versions using the same dataset and execution model. If version A uses zero commissions while version B uses real commissions, ranking them is meaningless. Likewise, you cannot compare a daily strategy tested over ten years with a one-minute strategy tested during a single quiet month based only on profit factor.

Overfitting and other mistakes that make a backtest useless

Research by Bailey, Borwein, Lopez de Prado, and Zhu formalizes the problem of selecting the best version after running numerous backtests. Even if an individual test appears methodical, a series of parameter searches turns the research into a contest for a randomly attractive result. The log must therefore retain not only the winner but also the number of variants tested.

The most obvious mistake is look-ahead bias. It occurs when a condition uses the high of a candle that has not yet closed, uses the final composition of an index for earlier years, or fills an order at a price that became known only after the signal. A less obvious form is manually choosing the start of the period immediately before a favorable trend.

The second mistake is survivorship bias. A test using today's list of assets omits stocks and funds that closed or were delisted. The result describes winners identified with hindsight. The third mistake is perfect execution: every limit order is treated as filled when touched, while commissions and slippage are set to zero.

The fourth mistake is changing the rules after every failure. An individual change is acceptable as a new hypothesis, but it must not be silently inserted into an old report. Freeze the version, split the data chronologically into a tuning segment and an untouched validation segment, and do not return to adjust the parameters after viewing the validation result.

123

Rules and costs defined in advance

Parameters selected using the best curve

  1. 1.One version
  2. 2.Several versions recorded in the log
  3. 3.A winner without the search history

One version

Several versions recorded in the log

A winner without the search history

The more decisions are made after viewing the result, the less the report says about the rule's robustness.

Diagram 3. The danger comes not from parameter selection itself, but from a hidden search without independent validation or a record of every attempt.

Signs of a useless backtest:

  • the rules cannot be reproduced from the written description;
  • the period was selected after viewing the chart;
  • trades were removed as “atypical” after the result was known;
  • future data or data recalculated after the fact was used;
  • commission, spread, and slippage are not specified;
  • one bar touches both the stop-loss and the target, but the order of events is always chosen in the strategy's favor;
  • only the best of many versions is shown;
  • there is no separate validation segment;
  • the report gives percentages without the number of trades or the test period.

Protection against these mistakes does not require complex mathematics. What is needed is an immutable version specification, a log of every test run, a reasonable cost model, a chronological split of the data, and a separate forward test. If the result disappears with a slight deterioration in execution or during an adjacent period, that is an important finding, not a reason to hide the test.

Reproducibly testing the same strategy in Midas BackTester

Use the same specification that you used for manual testing. The Midas BackTester guide describes the tool as a bot strategy in TradingView: Long and Short conditions, exits, stop-loss orders, and targets are configured through settings, while TradingView performs the historical calculations. This turns a manual idea into a repeatable configuration without rewriting the rules for every trade.

The procedure remains the same:

  1. Save the original wording of the entry, exit, and skip conditions.
  2. Record the instrument, timeframe, period, and price-data source.
  3. Transfer the rules into the BackTester conditions and save a snapshot of the settings.
  4. Set the available commission and execution parameters in the TradingView strategy properties.
  5. Run the test and export the trade table or save the report.
  6. Compare several cases with the manual log, especially price gaps and bars on which both the stop-loss and the target may have been triggered.
  7. Record the number of trades, net result, maximum drawdown, profit factor, and worst losing streak.
  8. Repeat the unchanged configuration on an untouched time period.

If the manual log and automated report differ, do not choose the more favorable result. Check the signal timing, execution price, rounding, commission, re-entries, and the order of events within the candle. No conclusion about the strategy should be drawn until the discrepancy has been explained.

What a backtest does not guarantee: the method's limitations

Historical data contains only the path that actually occurred. The future may bring different volatility, a different participant structure, different commissions, and different liquidity availability. A strategy that survived a past crisis will not necessarily survive the next one because the mechanism driving the market may be different.

An emulator does not know your position in the limit-order queue or the full available volume at each price. In a thin market, a simulated order may be filled in full where a real order would have been only partially filled. A price gap may carry the price through a stop-loss order. A connection or venue failure is not a property of the trading rule at all and requires separate controls.

A percentage result from one instrument and period cannot be transferred to a user's capital. Position size, account currency, leverage, and broker restrictions change the risk. The conclusion should therefore be narrowly worded: “Version 1.3, tested on data from source X over period Y with costs Z, produced the stated result and passed/failed the untouched validation segment.”

After the backtest, leave the parameters unchanged and collect new signals. Compare trade frequency, average result, drawdown, and streaks with their historical ranges. If behavior diverges significantly, suspend interpretation and determine whether the market, data, code, or execution has changed.

What a backtest report can answer and what it cannot show
Suitable for
  • Testing whether a rule's result is reproducible on recorded data
  • Seeing how commissions and slippage change the conclusion
  • Running the rule on a segment that was not used for tuning
Not suitable for
  • Predicting the future market regime and available liquidity
  • Estimating your position in the real order queue at actual volume
  • Accounting for price gaps, connection failures, and the restrictions of a particular broker

Card 2. The method's limitations do not negate its usefulness: a backtest reduces uncertainty about the rule, but not about the market itself.

Related rules for managing position size and loss limits are covered in the material on risk management. A good report does not itself determine an acceptable level of risk. This material is educational and does not constitute personalized investment advice.

Sources

Frequently asked questions

What is a trading strategy backtest?

It is the application of predefined entry, exit, and risk rules to historical data. The result applies only to the selected data, settings, costs, and execution model. It helps filter out weak or ambiguous ideas, but it does not guarantee future results.

How do you backtest a strategy manually?

Write down the rules and parameters before viewing the result, hide the future portion of the chart, and advance one closed candle at a time. Record every signal in a table along with the assumed execution price, commission, exit, and outcome. Do not remove losing or inconvenient cases.

How many trades are needed for a backtest?

There is no universal number. It depends on how frequently the rule triggers, the dispersion of trade results, and the variety of market regimes. Always show the number of trades alongside each metric, test several periods, and reserve a separate data segment that was not used for tuning.

Which metrics matter most?

The basic set includes the number of trades, net result after costs, maximum drawdown, profit factor, average profit and loss, and the worst losing streak. Also check whether the entire result was produced by a single large trade.

How does a backtest differ from a forward test?

A backtest uses historical data that is already known. A forward test applies frozen rules to new data that appears after tuning is complete. It reduces the risk of overfitting, but it still does not fully reproduce real execution.

How should commissions and slippage be accounted for?

Set the commission for both sides of the trade according to the venue's current terms, and model the spread and slippage as separate parameters or through a conservative scenario. If the exact value is unknown, show a range. Do not use zero merely because the data is unavailable.

Can a high profit factor prove that a strategy works?

No. Profit factor describes the ratio of gross profit to gross loss in a particular sample. It must be considered together with the number of trades, drawdown, test period, costs, and performance on untouched data.

What you will learn
  • Understand what a backtest does and does not prove
  • Define the data, rules, and costs before viewing the result
  • Run manual and automated tests using the same protocol
Apply in 25 min
beginner

Was this material useful?

Feedback is not available for this language version yet.

Comments

Source comments are not translated and are never replaced with Russian text.

Subject-matter contributor
Max Vitkovsky
Market analyst

Analyses cryptocurrency market structure, levels and on-chain context, with attention to risk and invalidation conditions.

TradingView chart with Buy and Sell signals from the Midas indicator
Midas multi-indicator for TradingViewOne of the most advanced indicators for trading
  • Non-repainting signals
  • Interactive technical analysis
  • 7 strategies to choose from
Midas multi-indicator for TradingViewSignal, stop, and 3 targets - directly on the chart
  • Signal locks at candle close
  • Stop and 3 targets build automatically
  • The trade plan is visible before entry