How to Backtest and Debug an MT5 Expert Advisor Before Using It Live
Last checked: 12 October 2026. This guide draws on the MetaTrader 5 Help pages on the Strategy Tester and on MQL5 articles about tick generation modes. A backtest is a simulation, not a promise. Nothing here is financial advice.
Running an Expert Advisor live without testing it first is how people lose money to bugs, not to the market. MetaTrader 5 includes a Strategy Tester that replays history through your code. This guide explains the four testing modes, how to debug with the Journal and Print, the mistakes that make backtests look better than reality, and a checklist to follow before you let any EA near a live account.
The four modelling modes
The MetaTrader 5 help describes the tester’s tick generation modes. They trade speed for accuracy:
- Open prices only. The tester calls OnTick only at the opening price of each bar on the chosen timeframe. It is fast and rough, suited to strategies that decide only at a bar’s open.
- 1 minute OHLC. Ticks are built only from the open, high, low and close of each M1 bar. It is faster than every-tick modelling, but MetaQuotes warns that skipping intermediate ticks creates rigid determinism in how price develops.
- Every tick. The tester generates all ticks from M1 data. It is more accurate but slower.
- Every tick based on real ticks. The tester uses the tick history provided by your broker. It is the most accurate mode and is recommended for scalping or strategies that depend on precise tick order.
The MQL5 documentation also advises using faster modes for rough optimisation and then confirming with every tick or, better, real ticks before trusting a result.

Setting up a clean test
- Open View, then Strategy Tester (Ctrl+R).
- Choose your EA, the symbol, the timeframe and a date range of at least a year, ideally covering different market conditions.
- Choose Every tick based on real ticks for the final check, and make sure the deposit, leverage and currency match your real account.
- Set a realistic spread and execution delay if your broker’s server allows it.
- Run once without optimisation first.
The help also notes the tester needs at least 100 bars on the selected timeframe before modelling can begin, so a very short date range can fail for that reason alone.
Debugging with Print and the Journal
The simplest debugging tool is Print. Add messages at decision points so the Journal tab shows what your EA saw and why it acted:
void OnTick()
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// ...
if(buySignal)
PrintFormat("BUY signal at %s bid=%.5f fast=%.5f slow=%.5f",
TimeToString(TimeCurrent()), bid, fastVal, slowVal);
if(!tradeAllowed)
Print("Skipped: trade not allowed. Last error ", GetLastError());
}
For deeper debugging, MetaEditor can run your EA on history with the debugger (Debug, then Start on history data). You can set breakpoints, step through code and watch variables. Use it when the Journal output does not explain a wrong decision.
Always check return values
Many bugs come from functions that fail silently. CopyBuffer returns the number of values copied, which can be less than you asked for. Trade functions return a result you must inspect. Call ResetLastError before a risky call and read GetLastError after it, and log the code whenever something fails.
Why a backtest can lie
- Look-ahead bias. Using the forming bar or a higher-timeframe bar that had not closed makes history look smarter than live trading. The repainting article in this series covers this.
- Over-optimisation. If you tune ten inputs until the curve looks great, you have fitted the past, not found a rule. Keep inputs few and test on data you did not optimise on.
- Unrealistic costs. Real spread, commission and slippage reduce results. Historical spread is also hard to reproduce accurately in the tester.
- Too little data. Thirty trades prove nothing. Look for hundreds across varied conditions.
- Tester-only functions. Features like SendNotification and WebRequest do not execute in the tester, so those paths need separate live checks.
Forward test on a demo account
After a clean backtest, run the EA on a demo account in real time for several weeks. Compare its trades with what the backtest would have done over the same period. Differences point to bugs, data problems or costs you did not model.
A pre-live checklist
- No compile warnings, and all inputs have sensible defaults.
- Tested on real ticks across at least a year.
- Result survives a small change in inputs. If tiny changes ruin it, it is fragile.
- Lot size comes from a risk formula with checks, not a fixed guess.
- Stop losses are always set, and the EA handles requotes, rejections and disconnects.
- Forward tested on demo with matching results.
- Start live with the smallest size your broker allows.
What we could not confirm
- How closely any tester result will match your future live results. No test can guarantee that.
- Whether your broker supplies real tick history for your chosen symbol and period.
Frequently asked questions
Which mode should I use first? Open prices only for a quick sanity check if your EA trades at bar open, and real ticks for the final test.
Why does my EA not trade in the tester? Common causes are too little history, a symbol not available on the server, or a condition that is never true. Check the Journal.
Key takeaways
Match the modelling mode to the strategy, log decisions with Print, check every return value, distrust perfect curves and forward test on demo before going live.
Sources checked
- MetaTrader 5 Help, “How the Tester Downloads Historical Data” and “Tick Generation”, metatrader5.com
- MQL5 Book, “Generating ticks in tester”, mql5.com
- MQL5 article, “The Fundamentals of Testing in MetaTrader 5” and related tick-mode comparison, mql5.com
Educational content only — not financial advice. Income and savings results vary and are never guaranteed. How we write
Enjoyed this story?
Get the best stories delivered weekly.


