MQL4 error diagnosis

MQL4 MarketInfo runtime error after an MT4 restart

A MarketInfo call in an MQL4 indicator can fail after an MT4 restart because OnInit runs before symbol properties, quotes, or history are ready. Read the returned value and GetLastError together, reject an unusable result, and let OnCalculate wait for enough bars instead of using zero or a value cached from the previous session.

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

RestartSafeMarketInfo.mq4MQL4
#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

MarketInfo can run before data is ready after restart

After MT4 restarts, the chart, account connection, selected symbol, quotes, and history can become available at different times. A call that succeeded on an already open chart can return 0 during OnInit.

Read MarketInfo when you need the property. A value saved before the restart does not show that the same property is available now.

Check symbol properties, quotes, and history separately

MODE_POINT and MODE_DIGITS describe price formatting. MODE_BID, MODE_ASK, and MODE_SPREAD need a current quote. Bar counts and indicator series need loaded history. One successful property does not show that 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

Return INIT_FAILED, or wait in OnCalculate

Return INIT_FAILED when OnInit requires a property it cannot read. That result ends the initialization. It does not schedule another calculation by itself.

Sleep is not allowed in a custom indicator. When initialization succeeded but bars or quotes are still missing, return from OnCalculate without calculating and check again on a later call. Log a throttled reason instead of blocking the terminal.

Review the complete example

This diagnostic skeleton checks MODE_POINT during initialization and rates_total in OnCalculate; it does not draw an indicator. INIT_FAILED is a failed initialization, not a request to retry on the next calculation event. The spread-only check cannot prove fresh quotes or distinguish a valid zero spread from an API failure; add result/error and quote-time validation before using prices.

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.mq4MQL4
#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?

No. MQL4 prohibits Sleep in custom indicators. After successful initialization, return without calculation and recheck on a later event; do not treat INIT_FAILED as a scheduled retry.

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.

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