Backtesting and Optimising Trading Algorithms: A Guide for Algorithmic Traders

Backtesting is a crucial step in algorithmic trading, allowing traders to evaluate a strategy’s effectiveness using historical data. By simulating how a strategy would have performed in the past, backtesting helps identify strengths, weaknesses, and potential improvements before risking capital in live markets. In this article, we’ll explore the basics of backtesting, common pitfalls to avoid, and techniques for optimising algorithms. We’ll also provide an MQL4 example to show you how to backtest a trading strategy effectively.

What is Backtesting?

Backtesting is the process of running a trading strategy on historical data to see how it would have performed. By observing the strategy’s simulated results, traders can assess potential profitability, risk, and overall effectiveness.

Key Metrics in Backtesting:

  1. Net Profit: Total profit or loss after all trades.
  2. Win Rate: Percentage of profitable trades.
  3. Drawdown: The peak-to-trough decline in equity, which shows potential risk.
  4. Sharpe Ratio: A measure of risk-adjusted returns.
  5. Maximum Loss: The largest loss on a single trade or series of trades.

Steps for Effective Backtesting

1. Define Clear Rules for the Strategy:

  • Ensure the trading rules are clear and objective, such as specific entry and exit signals.

2. Select an Appropriate Data Set:

  • Use historical data that reflects the timeframe and asset you intend to trade. For intraday strategies, high-resolution tick or minute data may be necessary, while daily data might be sufficient for longer-term strategies.

3. Run the Backtest:

  • Simulate the strategy on historical data. Use MQL4’s Strategy Tester in MetaTrader 4 (MT4) for quick and reliable backtests.

4. Analyse Key Metrics:

  • Evaluate metrics like profit, drawdown, and win rate to determine if the strategy meets your risk and reward expectations.

5. Adjust and Optimise:

  • Based on the initial results, tweak parameters to improve performance. Ensure changes are made carefully to avoid overfitting.

Common Pitfalls in Backtesting

1. Overfitting:

  • Overfitting occurs when a strategy is too closely tailored to historical data, often resulting in poor performance on new data. Avoid using too many parameters, and focus on robust, simple strategies.

2. Data Snooping Bias:

  • This bias occurs when a strategy is developed based on prior knowledge of the data, creating a false sense of confidence in its performance.

3. Look-Ahead Bias:

  • This happens when information not available during the time of the trade is inadvertently used in the backtest, creating unrealistic results.

4. Survivorship Bias:

  • Survivorship bias occurs when the backtest only includes assets that have “survived” and excludes those that have been delisted or failed, giving a misleading picture of performance.

5. Ignoring Slippage and Commissions:

  • Real-world trading includes costs such as slippage and commissions. Not accounting for these can lead to overly optimistic results.

Introduction to Optimisation

Optimisation is the process of refining a strategy’s parameters to maximise performance based on historical data. This can include adjusting indicator periods, entry and exit levels, or risk parameters to achieve a better balance between risk and return.

Types of Optimisation:

  1. Parameter Optimisation: Adjusting key variables to improve performance.
  2. Walk-Forward Optimisation: A method that involves testing a strategy on a rolling window of data, allowing for more realistic performance by reducing overfitting.

Backtesting Example in MQL4

The following MQL4 example demonstrates how to set up a simple moving average crossover strategy for backtesting in MetaTrader 4. This example includes basic performance metrics you can track to evaluate the strategy’s performance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Simple Moving Average Crossover Strategy for Backtesting
input int FastMAPeriod = 10;           // Period for fast moving average
input int SlowMAPeriod = 20;           // Period for slow moving average
input double LotSize = 0.1;            // Lot size for orders
 
double initialBalance;
double netProfit;
int wins = 0;
int losses = 0;
double largestDrawdown = 0.0;
double currentDrawdown = 0.0;
double peakBalance = 0.0;
 
void OnTesterInit() {
    initialBalance = AccountBalance();
    peakBalance = initialBalance;
}
 
void OnTick() {
   double fastMA = iMA(NULL, 0, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
   double slowMA = iMA(NULL, 0, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
 
   if (fastMA > slowMA && OrdersTotal() == 0) {
       OpenBuy();
   } else if (fastMA < slowMA && OrdersTotal() == 0) { OpenSell(); } UpdateDrawdown(); } void OpenBuy() { int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "SMA Buy", 0, 0, clrGreen); if (ticket > 0) {
        Print("Buy order opened.");
    }
}
 
void OpenSell() {
    int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "SMA Sell", 0, 0, clrRed);
    if (ticket > 0) {
        Print("Sell order opened.");
    }
}
 
void OnTrade() {
    if (OrderProfit() > 0) {
        wins++;
    } else {
        losses++;
    }
    UpdateNetProfit();
}
 
void UpdateNetProfit() {
    netProfit = AccountBalance() - initialBalance;
    if (AccountBalance() > peakBalance) {
        peakBalance = AccountBalance();
    }
}
 
void UpdateDrawdown() {
    currentDrawdown = peakBalance - AccountBalance();
    if (currentDrawdown > largestDrawdown) {
        largestDrawdown = currentDrawdown;
    }
}
 
void OnTesterDeinit() {
    Print("Net Profit: ", netProfit);
    Print("Total Wins: ", wins);
    Print("Total Losses: ", losses);
    Print("Largest Drawdown: ", largestDrawdown);
}

Explanation of the Backtesting Example

1. Strategy:

  • The strategy uses a simple moving average crossover, buying when the fast MA crosses above the slow MA, and selling when it crosses below.

2. Performance Metrics:

  • netProfit: Tracks the total profit or loss.
  • wins and losses: Count the total number of winning and losing trades.
  • largestDrawdown: Tracks the largest peak-to-trough equity decline during the backtest.

3. Functions:

  • OnTesterInit(): Initializes variables before the test.
  • OnTick(): Executes the trading logic on each tick.
  • OnTrade(): Updates win/loss counts and profit after each trade.
  • OnTesterDeinit(): Outputs final performance metrics at the end of the backtest.

This example provides a simple framework to get started with backtesting in MQL4, allowing you to monitor key performance metrics as you refine your strategy.

Tips for Optimising and Improving Backtests

  1. Use Multiple Timeframes: Test the strategy on different timeframes to understand its robustness and adaptability.
  2. Include Slippage and Commission: Adjust your backtest to include realistic transaction costs.
  3. Run Forward Testing: After backtesting, run the strategy in a demo environment to see how it performs in real market conditions.

Conclusion

Backtesting is an essential step in algorithmic trading, allowing traders to evaluate and optimise their strategies before going live. By understanding backtesting metrics and avoiding common pitfalls, traders can gain valuable insights and refine their algorithms. This MQL4 example provides a starting point for backtesting, enabling you to measure and track key performance metrics. In the next article, we’ll explore Machine Learning in Algorithmic Trading, covering basic concepts and how machine learning models are integrated into trading systems.