MQL4 error diagnosis

MQL4 indicator runtime error from MarketInfo after restart

An MQL4 indicator runtime error around MarketInfo after an MT4 restart often means the indicator assumed symbol metadata, quotes, or history were ready during initialization. Read the return and GetLastError together, reject invalid values, and let OnCalculate wait for enough data instead of continuing with zeros or stale cached properties.

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

RestartSafeMarketInfo.mq4.mq4
#property strict
#property indicator_chart_window

int OnInit()
  {
   ResetLastError();
   double point_value = MarketInfo(Symbol(), MODE_POINT);
   int error_code = GetLastError();
   if(point_value <= 0.0 || error_code != 0)
     {
      Print("MarketInfo unavailable during initialization. error=",
            error_code, " point=", point_value);
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if(rates_total < 2)
      return(0);

   double current_spread = MarketInfo(Symbol(), MODE_SPREAD);
   if(current_spread < 0)
      return(prev_calculated);

   return(rates_total);
  }
  • Initialization order can change after restart
  • Check every MarketInfo result
  • Wait for quotes and enough bars
  • Rebuild cached symbol properties

Restart changes the order in which data becomes ready

After a terminal restart, charts, account connection, symbol selection, quotes, and history may become available at different times. Code that succeeded during a warm chart session can read zero or incomplete values during initialization.

Treat MarketInfo as a runtime lookup. Check the returned value and error at the call site instead of assuming a cached value from a prior terminal session still exists.

Separate symbol metadata, quote state, and history state

Digits and Point describe price formatting, while Bid, Ask, and spread depend on current quotes. Bars and indicator series depend on loaded history. One successful property does not prove the others are ready.

StateGuard
Symbol metadataCheck the returned property and GetLastError
Current quoteRequire valid Bid and Ask after connection
HistoryRequire rates_total and the requested lookback
Cached valuesRebuild them in OnInit after each reinitialization

Fail initialization or wait without blocking the terminal

Return INIT_FAILED when a property is required for safe initialization and cannot be obtained. For data that can arrive later, let OnCalculate return without calculation until the minimum state is present.

Avoid long Sleep loops in indicator event handlers. Log a throttled reason and allow later terminal events to retry naturally.

Review the complete example

The indicator validates MODE_POINT in OnInit and waits for at least two bars in OnCalculate. It returns a documented lifecycle value instead of using unavailable symbol data.

A startup guard prevents invalid calculations but may delay indicator output. Test cold starts, reconnects, symbol changes, and history refreshes in the target terminal.

RestartSafeMarketInfo.mq4.mq4
#property strict
#property indicator_chart_window

int OnInit()
  {
   ResetLastError();
   double point_value = MarketInfo(Symbol(), MODE_POINT);
   int error_code = GetLastError();
   if(point_value <= 0.0 || error_code != 0)
     {
      Print("MarketInfo unavailable during initialization. error=",
            error_code, " point=", point_value);
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if(rates_total < 2)
      return(0);

   double current_spread = MarketInfo(Symbol(), MODE_SPREAD);
   if(current_spread < 0)
      return(prev_calculated);

   return(rates_total);
  }
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

    Reproduce from a cold restart

    Record terminal build, account connection, chart symbol, and the first initialization logs.

  2. 2

    Check each MarketInfo call

    Capture the returned value and immediate error with the requested mode and symbol.

  3. 3

    Guard quote and history use

    Require valid prices and enough rates_total before indexing or dividing.

  4. 4

    Rebuild cached state

    Initialize symbol properties again after every terminal-driven reinitialization.

Frequently asked questions

Why does MarketInfo fail only after an MT4 restart?

The symbol, quote stream, or history may not be ready at the same point in initialization as it was during a warm session.

Should an indicator use Sleep while waiting for data?

Avoid blocking event handlers. Return without calculation, log a throttled reason, and recheck on a later event.

Does MODE_POINT prove a current quote exists?

No. Symbol metadata and the current Bid or Ask are different states and should be validated separately.

When should OnInit return INIT_FAILED?

Use it when a required invariant cannot be established and running the indicator would be unsafe or meaningless.

Can Pineify reproduce a terminal restart?

No. Pineify can revise the guards and run static MetaEditor diagnostics, but restart timing and data readiness must be tested in the target MT4 terminal.

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