Overview

The Strategy Tester in MetaTrader 5 lets you run your Expert Advisors (EAs) on historical data to verify logic, performance, and risk—without using real money. You can backtest on different symbols and timeframes, review reports and logs, and even optimize input parameters to search for better settings. Mastering the tester is a crucial step before any live deployment.

Ad

What You Will Be Able to Do

  • Run an EA in the Strategy Tester with chosen symbol, timeframe, and period.
  • Use input parameters to configure tests and prepare for optimization.
  • Enable Visual mode to watch trades on the chart as they happen.
  • Read reports, equity curves, and logs to diagnose behavior and performance.

Code Example

This minimal EA uses an input parameter (fast MA period) so you can adjust it in the tester and later optimize it.


//+------------------------------------------------------------------+
//|                                               TesterDemoEA.mq5   |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

input int FastMAPeriod = 10;          // adjustable in Strategy Tester
input ENUM_TIMEFRAMES MAPeriodTF = PERIOD_CURRENT;

int handleMA;

int OnInit()
  {
   handleMA = iMA(_Symbol, MAPeriodTF, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
   if(handleMA == INVALID_HANDLE)
     {
      Print("Failed to create MA handle");
      return(INIT_FAILED);
     }
   Print("TesterDemoEA initialized. FastMAPeriod = ", FastMAPeriod);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   if(handleMA != INVALID_HANDLE) IndicatorRelease(handleMA);
   Print("TesterDemoEA stopped.");
  }

void OnTick()
  {
   double ma[];
   if(CopyBuffer(handleMA, 0, 0, 2, ma) != 2) return;

   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   // Toy condition: log when price crosses above the MA
   if(bid > ma[0] && ma[1] >= bid)
      Print("Cross above detected at ", TimeToString(TimeCurrent(), TIME_SECONDS),
            " | Bid=", DoubleToString(bid, _Digits), " | MA=", DoubleToString(ma[0], _Digits));
  }
Ad

Execution Steps

  1. Compile the EA above as TesterDemoEA.mq5 (F7).
  2. Open the Strategy Tester panel (Ctrl+R).
  3. Select Expert = TesterDemoEA, choose the Symbol, Timeframe, and date range.
  4. Adjust Inputs (e.g., set FastMAPeriod to 8, 10, 14 for quick checks).
  5. (Optional) Enable Visual mode to see trades and logs on the chart as the test runs.
  6. Click Start. When finished, review Results, Graph, Report, and Journal tabs.
  7. (Optional) Switch to Optimization mode to sweep multiple values of FastMAPeriod and compare outcomes.

Key Point

Backtesting validates logic and reveals performance characteristics before risking capital. Use inputs to make scenarios reproducible and optimization-ready, and always confirm findings on out-of-sample data to avoid overfitting.

Ad

Next Section

→ Next: Working with Parameters