Articles

Strategy Backtesting: How to Test a Trading Algorithm Properly

A practical guide to historical testing: data, commissions, spread, slippage, re-optimization, sustainability, and linking backtesting to real trading.

Stages of backtesting and verification of trading strategy

Backtest trading strategy It helps to understand how an algorithm could trade on historical data, but in itself. trading algorithm testing It doesn't prove future profits. The reliability of the results depends on the quality of quotations, the execution model, commission, spread, slippage and whether the strategy was not adjusted to the past.

A trading algorithm can show smooth capital growth on histories and then lose money in real trading. Sometimes the reason is not the idea of the strategy, but in the test conditions: the tester used a too good spread, did not take into account the commission, entered at a price that was not yet available, or chose the best set of parameters from thousands of options.

What is a Backtest Trading Strategy

Backtesting is a test of strategy rules against past market data. The strategy tester consistently processes historical candlesticks or tics, checks the entry conditions, simulates the opening of a position, then calculates the output, profit, loss, drawdown and other indicators.

For example, the algorithm receives historical candles EUR/USD, checks its trading conditions on each candle and simulates the opening or closing of a position. The report includes transactions, capital chart, profit factor, maximum drawdown, average transaction and statistics on profit and loss series.

The main limitation is simple: the backtest is a model, not a real trade. He does not know the future liquidity, the actual queue of orders, the behavior of the broker, the server delay and the market reaction to a particular position volume.

Backtest trading strategy It is a modeling of trades on historical data that shows how an algorithm might have worked in the past under given rules and trading conditions. It helps to assess the sustainability of an idea, but does not guarantee a repeat of the result in the future.

What historical data is needed for testing

The data must conform to the logic of the strategy. For the system on the daily chart, high-quality daily or minute quotes may be sufficient. Scalping, short stops, candle trading and frequent entries usually require tick data, separate Bid and Ask prices, and an accurate understanding of the trading session.

It is important to check the timeframe, depth of history, omissions, abnormal prices, time zone, transition to summer and winter time. Corporate actions need to be taken into account. For futures, the correct combination of contracts, expiration dates and specification of each instrument.

The most detailed data in itself does not make the test reliable. If the strategy works to close the day, tics may be redundant. But if the algorithm puts a stop in a few points, minute candles may not be enough, because inside a minute the price could first pass the stop, and then take profit.

Why the quality of historical data affects the reliability of backtesting

The quality of historical data directly affects the reliability of the backtest. The omissions create false pauses, incorrect prices can form non-existent trades, and different data providers sometimes give different highs, lows, and spreads on the same stretch of the market.

One and the same algorithm can show a good result on the data of one broker and a weak result on the data of another. This does not always mean a mistake in the algorithm. Sometimes trading sessions, quote flow, point size, instrument specification, or method of constructing a continuous futures contract differ.

  • Check for the absence of major passes;
  • compare trading sessions with the broker’s terms;
  • find abnormal candles and sharp emissions;
  • check several sections of history with the terminal;
  • check the size of the item and the cost of the tick;
  • Make sure that Bid and Ask prices are used correctly.

How to Consider the Broker’s Commission

A test without commission almost always overstates the result. A commission may be charged for one side of a trade or for a full trading cycle. Therefore, it is necessary to understand whether the fee is taken for opening a position, closing a position or is already indicated as a round turn.

If the commission is $2 for opening and $2 for closing a position, a full trading cycle costs $4. With 1,000 transactions, the annual commission costs will be $4,000. For a strategy with a large average trade, this can be tolerable, and for a frequent algorithm with a small goal, it is critical.

The commission of one broker cannot be automatically transferred to another. The terms depend on the type of account, instrument, volume, exchange fees and execution model. Especially carefully you need to check algorithms with a large number of trades: the more often the strategy trades, the more the commission affects the result.

The spread and its impact on test results

The spread is the difference between the Ask purchase price and the Bid sale price. When a algorithm buys, it is usually executed by Ask, and when it sells, it is executed by Bid. If the test uses one notional price without a real spread, the profit can be markedly inflated.

A fixed spread is convenient for rough checking, but real markets often use a floating spread. It expands at night, on the news, during times of low liquidity and sudden movements. For a short-term strategy, this can completely change the mathematical expectation.

A simple example: the algorithm expects to take a movement of 8 points, and the average spread is 2 points. A quarter of the target goes into the price of entry and exit. If at night the spread expands to 5 points, such a trade can become statistically unprofitable even with the correct direction of the forecast.

Slippage and modelling of order execution

Slippage is the difference between the expected price of a trade and the actual execution price. The market order is not always executed at the signal price: as long as the order reaches the server and meets the available liquidity, the price may change.

Stop orders can also be executed below the set price, especially on news, gaps and fast movements. Slippage is affected by liquidity, volatility, position size, server location, brokerage infrastructure and the type of instrument.

The baseline scenario

Execution is close to the average market conditions. Suitable for initial evaluation.

Increased slippage

The test shows how the system can withstand less comfortable conditions.

Stress scenario

It is necessary to assess the behavior on news, gaps and a sharp increase in volatility.

Major errors in modeling

Testing errors often occur not in the strategy code but in the transaction model. The most dangerous cases are the use of future data, entry at a price that was not yet available, ignoring trading pauses, incorrect rounding of price and volume, incorrect price step and incorrect combination of futures contracts.

A separate problem is the sequence of price movement inside the candle. If in one minute candle there were both stop loss and take profit, according to the candle itself, it is impossible to determine what level was reached first. For an accurate answer, you need more detailed data or a conservative modeling rule.

  • prevent the use of future data;
  • Do not execute the signal at a price that has not yet been
  • Model limit orders more carefully than simply touching the price.
  • take into account trading pauses, market closures and weekends;
  • check the minimum price, volume and margin requirements;
  • For indices, do not use the current composition for past periods.

Re-optimizing the trading strategy

Re-optimizing a trading strategy is a situation where the parameters fit past data perfectly, but do not work well in a new market. Strategy seems to remember history instead of finding a consistent pattern.

Useful optimization looks for the parameters workspace. The fit chooses the only point where the schedule turned out to be the most beautiful. The more parameters, filters, and exceptions there are, the greater the risk that the algorithm has learned to explain the past rather than trade the future.

A re-optimized strategy is like a student who has learned the answers to a particular exam ticket but has not understood the subject itself. On a familiar question, it looks brilliant, and with a slight change of conditions is lost.

Trading costs that are often overlooked

In addition to commission, spread and slippage, additional costs appear in real operation. These are swaps and transfer fees, financing for indefinite contracts, exchange and clearing fees, currency conversion, the cost of market data and infrastructure.

There are also less obvious factors: partial execution, delay in sending an order, minimum volume restrictions, margin requirements, and price differences between liquidity providers. Direct transaction costs need to be separated from the total cost of operating the system, but both blocks affect the final economics of the strategy.

How to test your strategy for re-optimization and sustainability

Professional counselor testing does not end with the first beautiful report. Once the strategy is developed, you need to check the data that was not involved in the selection of parameters, and then see how it reacts to the deterioration of trading conditions.

  1. Divide the story into a development period and an independent test period.
  2. Do not use an independent period when selecting parameters.
  3. Check neighboring parameters, not just the best result.
  4. Increase commission, spread and slippage.
  5. Test the strategy on trends, sideways and high volatility.
  6. Make a random change in the order and parameters of transactions.
  7. Run forward testing on a demo account or account with minimal risk.
  8. Compare real trades with expected backtest trades.

Useful testing outside the optimization period, sliding testing, parameter stability analysis, random scenario modeling and forward observation. The methodology is applicable not only to MetaTrader, but also to StrategyQuant, TradeStation, MultiCharts and other platforms.

How professional backtesting is different from surface testing

Surface verification usually comes down to running an algorithm on one piece of history and picking the most profitable report. The professional process is arranged differently: first, the trading hypothesis, the market, the timeframe, the type of orders and the rules of money management are fixed, then the appropriate data are selected and the criteria for which the strategy will be considered suitable or weak are set in advance.

This approach disciplines development. If the criteria are known before the test, it is more difficult to justify a random result with a beautiful schedule. Verification becomes not a search for an advertising picture, but a quality filter: the strategy must withstand costs, normal deviations of parameters, different market phases and an independent period. It is this process that distinguishes system development from the selection of a successful set of settings.

How to Interpret Backtest Results

Backtest results can not be evaluated only on net profit. Maximum and relative drawdown, profit factor, average transaction, mathematical expectation, number of transactions, share of profitable transactions, average profit and average loss, duration of drawdown and stability over the years are important.

One metric without context is easily misleading. The high profit factor on 20 trades is statistically less convincing than the moderate result on several hundred trades. A strategy with high returns but unacceptable drawdowns can be unsuitable for real trading. If most of the profits are made in one month, the outcome depends on a rare episode, not a sustained benefit.

Surface testQuality testWhy is it important?
Minimum spread

Realistic or floating spread

Reduces the risk of inflated profits.

There is no

Commission on entry and exit

Shows the real economics of the transaction.

Zero slippage

Several execution scenarios

Checks the sensitivity to the market.

One period of history

Development and independent test

Helps identify fit.

Only profit.

Profit, drawdown and stability

Evaluate the quality of the way to the result.

How to know if you can trust the backtest

The backtest deserves more credibility if not only the final profit and capital chart are checked, but also the conditions in which this chart was obtained. Even completing all the points does not guarantee future profits, but it significantly reduces the likelihood of running a random or over-optimized strategy.

  • Use of appropriate historical data;
  • commission, spread and slippage are taken into account;
  • no use of future data;
  • Properly modeled execution of orders;
  • there is a check outside the development period;
  • parameters are resistant to small changes;
  • the result is maintained when conditions worsen;
  • the number of transactions is sufficient to assess;
  • Profit does not depend on several transactions;
  • forward testing was carried out;
  • The actual execution is comparable to the model;
  • The risks are described in clear language.

Common mistakes in testing a trading algorithm

Testing errors are often repeated: too short a period, no commission, constant minimum spread, zero slippage, selecting the most profitable set of parameters, and repeatedly reviewing an independent period until it too becomes part of the fit.

Added to this is an insufficient number of trades, ignoring drawdowns, inappropriate data, and trying to evaluate an algorithmic trading strategy only on the return chart. The counselor’s background check should not answer the question of “how to get the most beautiful report,” but “what will remain of the result after realistic assumptions.”

Internal material on the topic

To better assess the strategy, it is useful to examine these materials:

Risk management Checking the algorithm before buying Futures and CFDs Diversification of strategies

FAQ

What does the backtest of a trading strategy show?

It shows how a strategy could trade on past data under given rules. In the report, you can see profit, drawdown, number of transactions, average transaction and stability of the result. This is a model of the past, not a forecast with a guarantee.

What is the quality of historical data needed to test a algorithm?

The data must correspond to the timeframe and logic of the algorithm. High-quality daily or minute data can be suitable for the day system. For scalping, short stops and intracandle logic, tick data and correct Bid and Ask prices are usually needed.

Do you need to take into account the spread and commission?

Yeah. The spread and commission reduce the outcome of each transaction. If the algorithm trades frequently or takes a small target movement, even small expenses can turn a profitable strategy into a weak or unprofitable system.

Why are backtest results different from real trading?

In reality, the spread, liquidity, execution speed, slippage and broker behavior change. In addition, the market after the test period can go into another mode. Therefore, the backtest should be supplemented with forward testing.

How do you define a re-optimized strategy?

Check whether the result worsens with a small change in parameters, whether the strategy works outside the development period, and whether the profit is focused on a short plot. Too complex rules and a lot of exceptions increase the risk of fitting.

How many deals should be on the test?

There is no rigid norm because strategies trade at different frequencies. But a small number of transactions makes the conclusions less reliable. It is better to evaluate not only the number, but also the distribution of transactions by year, market and market phases.

Can I trust the testing of an algorithm in MetaTrader?

It can be used as a working tool if the data, spread, commission, slippage and tool specification are configured correctly. The mere fact of testing in MetaTrader does not make the result either bad or guaranteed reliable.

What to do after a successful backtest?

Test the strategy beyond the optimization period, worsen trading conditions in the stress test, run forward testing and compare real trades with the model. Only then can a cautious launch with risk control be considered.

Conclusion

Backtesting is a valuation tool, not a proof of guaranteed profits. Its reliability depends on data, execution model, trading costs, and fair division of development and independent verification. The purpose of testing is not to get the most beautiful chart, but to find a stable trading logic.

At Algo Trade Systems, trading strategies undergo multi-step testing: historical testing, trading costs assessment, sustainability analysis and testing outside the development period. Be familiar with the approach and available algorithmic portfolios can be found in the relevant sections of the site, and for an individual task is available teamwork.

The material is informational in nature and is not an individual investment recommendation. Past testing and trading results do not guarantee similar results in the future. Algorithmic trading involves the risk of financial loss.

ALGO TRADE SYSTEMS

Evaluate strategies before capital launch

Study portfolios, methods of verification and conditions of application of algorithmic systems.

See the trading algorithms