MQL4 error diagnosis

MQL4 EA works in backtest but not live: compare environments

An MQL4 EA can work in backtest but not live because Strategy Tester and a broker terminal supply different quotes, spreads, server times, trading sessions, symbol properties, permissions, execution delays, and external-service access. Capture the same runtime snapshot in both environments, then compare the first state where behavior diverges.

Open the Coding Agent, then choose MQL4 from the language menu.

RuntimeEnvironmentSnapshot.mq4.mq4
#property strict

void OnTick()
  {
   static datetime last_log = 0;
   if(TimeCurrent() - last_log < 60)
      return;
   last_log = TimeCurrent();

   Print("Runtime snapshot. testing=", IsTesting(),
         " connected=", IsConnected(),
         " trade_allowed=", IsTradeAllowed(),
         " server_time=", TimeToString(TimeCurrent()),
         " spread_points=", MarketInfo(Symbol(), MODE_SPREAD),
         " stop_level=", MarketInfo(Symbol(), MODE_STOPLEVEL),
         " bars=", Bars);
  }
  • Compare inputs before outcomes
  • Log broker symbol properties
  • Use server time consistently
  • Expect tester restrictions

Compare the first differing input

Do not begin with profit or trade count. Compare the first bar, quote, spread, time, history count, signal value, and entry gate that differs. Later outcomes follow from that earlier state.

TimeCurrent is simulated from historical data in Strategy Tester, while live execution uses the latest known server time. A session filter can therefore behave differently even when local computer time looks the same.

Broker and execution rules are runtime inputs

Live symbols can have different Digits, Point, spread, stop level, freeze level, lot limits, trading hours, contract size, and margin requirements. Historical modeling also cannot reproduce every quote, rejection, partial service outage, or latency pattern.

  • Log symbol properties at initialization and before an order
  • Use server time rather than local assumptions
  • Record the exact Bid and Ask used by execution
  • Keep retries bounded and observable

External dependencies can disappear in the tester

WebRequest cannot run in Strategy Tester, and file paths use Tester/Files instead of the live MQL4/Files directory. External indicators, DLLs, and private libraries add more differences that a single-file static check cannot reproduce.

Replace external inputs with deterministic fixtures for backtests, then verify the live adapter separately on a demo account.

Review the complete example

The EA writes a once-per-minute snapshot of tester mode, connection, permission, server time, spread, stop level, and history. It does not trade, which makes it suitable for comparing environments.

Do not move from a backtest directly to live trading. Recompile, run the comparison log, and test order behavior on the target broker’s demo environment.

RuntimeEnvironmentSnapshot.mq4.mq4
#property strict

void OnTick()
  {
   static datetime last_log = 0;
   if(TimeCurrent() - last_log < 60)
      return;
   last_log = TimeCurrent();

   Print("Runtime snapshot. testing=", IsTesting(),
         " connected=", IsConnected(),
         " trade_allowed=", IsTradeAllowed(),
         " server_time=", TimeToString(TimeCurrent()),
         " spread_points=", MarketInfo(Symbol(), MODE_SPREAD),
         " stop_level=", MarketInfo(Symbol(), MODE_STOPLEVEL),
         " bars=", Bars);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Freeze configuration

    Record source version, inputs, symbol, timeframe, account, terminal build, and test dates.

  2. 2

    Log matching snapshots

    Emit the same prices, properties, time, history, signal, and permission fields in both environments.

  3. 3

    Find the first divergence

    Compare logs in event order and stop at the earliest different input or branch.

  4. 4

    Test the adapter

    Keep strategy rules fixed while correcting environment-specific data or execution handling on demo.

Frequently asked questions

Why does an EA trade in Strategy Tester but not live?

The live terminal may differ in permission, session, spread, stop rules, symbol properties, history, or execution state. Logs must identify the first blocked gate.

Does a profitable backtest prove live execution?

No. It is evidence under a chosen historical model, not proof of current broker fills, latency, costs, or availability.

Can TimeCurrent differ in testing?

Yes. Strategy Tester simulates TimeCurrent from historical data, so time filters should be compared using logged server-time values.

Can WebRequest run in Strategy Tester?

No. MQL4 documents that WebRequest cannot run in Strategy Tester. Use deterministic test fixtures for external data.

Can Pineify prove live and backtest parity?

No. Pineify can make environment assumptions and logs explicit in a self-contained file, but parity needs observed results from both target environments.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent