Chart stalls for good (every chart call ~3 s, error 4102) when an Expert and indicators exchange chart events - build 6249

Steps (two plain-MQL5 files attached):

  1. Put StallReproIndicatorV1.mq5 in MQL5\Indicators and StallReproExpertV1.mq5 in MQL5\Experts, compile both.
  2. Attach StallReproExpertV1 to EURUSD H1 with default inputs. It adds 4 copies of the indicator to the chart. Every 20 ms each program moves its label, calls ChartRedraw and sends EventChartCustom to the other side.
Result: after 13-16 s the chart stalls for good. Every chart call (ObjectSetInteger, ChartRedraw, ChartGetInteger, EventChartCustom) takes ~3 s and fails with 4102; the Experts tab prints STALL lines. Same on builds 6230 and 6328.

Hint: posting message 0x1400 (wParam 0x1B) to the chart's view window ends the stall at once, as if the chart waits for a wake message that never comes.
Documentation on MQL5: ChartRedraw / Chart Operations
Documentation on MQL5: ChartRedraw / Chart Operations
  • www.mql5.com
This function calls a forced redrawing of a specified chart. Parameters chart_id=0 [in]  Chart ID. 0 means the current chart. Note Usually it...

Hello Amir, thank you for the compact repro.

This pattern puts a heavy load on the chart. Every 20 ms the Expert and all four indicators move a label, call ChartRedraw and post EventChartCustom. The Expert also calls ChartGetInteger on each timer, and both sides redraw again when the other side's event arrives.

EventChartCustom is placed in the chart's event queue. ChartGetInteger is documented as synchronous and waits for every command already in that queue. 4102 is ERR_CHART_NO_REPLY, "Chart does not respond". The four indicators share one thread, so a wait in any of them holds up every indicator on the symbol, and that symbol's ticks and history.

The stall becoming permanent after 13–16 s, and clearing at once when you post 0x1400 (wParam 0x1B) to the view window, does not look like ordinary back-pressure. That is worth a Service Desk report with these two files. The ~3 s wait is your measurement; the docs only say the call waits for the queue, and that 4102 means the chart did not respond.

A practical workaround is to update objects as needed, coalesce ChartRedraw and EventChartCustom to at most every 150 ms, not post a custom event back from the handler that just received one, and cache CHART_WIDTH_IN_PIXELS on CHARTEVENT_CHART_CHANGE rather than calling ChartGetInteger in the timer.

//+------------------------------------------------------------------+
//| reply-521814.mq5                                                 |
//| Coalesce ChartRedraw / EventChartCustom and cache ChartGetInteger |
//| Forum example for https://www.mql5.com/en/forum/521814           |
//+------------------------------------------------------------------+
#property copyright "Public forum example"
#property version   "1.00"

input int InpMinFlushMs = 150;  // Minimum interval between redraw / notify

#define EVENT_NEED_UPDATE 1

string g_label      = "ThrottleDemo";
ulong  g_last_flush = 0;
bool   g_dirty      = false;
bool   g_notify     = false;
long   g_width      = 0;

//+------------------------------------------------------------------+
//| Cache a synchronous chart property, not in the hot path          |
//+------------------------------------------------------------------+
bool CacheChartWidth()
  {
   long width = 0;
   ResetLastError();
   if(!ChartGetInteger(0, CHART_WIDTH_IN_PIXELS, 0, width))
     {
      PrintFormat("ChartGetInteger failed, error=%d", GetLastError());
      return false;
     }
   g_width = width;
   return true;
  }

//+------------------------------------------------------------------+
//| Mark that objects changed; optionally notify the other side      |
//+------------------------------------------------------------------+
void MarkDirty(const bool notify)
  {
   g_dirty = true;
   if(notify)
      g_notify = true;
  }

//+------------------------------------------------------------------+
//| Flush at most once every InpMinFlushMs                           |
//+------------------------------------------------------------------+
void FlushChartWork()
  {
   if(!g_dirty)
      return;
   const ulong now = GetTickCount64();
   if(g_last_flush != 0 && now - g_last_flush < (ulong)InpMinFlushMs)
      return;

   g_last_flush = now;
   g_dirty      = false;
   ChartRedraw(0);

   if(g_notify)
     {
      g_notify = false;
      ResetLastError();
      if(!EventChartCustom(0, EVENT_NEED_UPDATE, g_width, 0.0, "flush"))
         PrintFormat("EventChartCustom failed, error=%d", GetLastError());
     }
  }

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!ObjectCreate(0, g_label, OBJ_LABEL, 0, 0, 0))
      return INIT_FAILED;
   ObjectSetInteger(0, g_label, OBJPROP_XDISTANCE, 10);
   ObjectSetInteger(0, g_label, OBJPROP_YDISTANCE, 20);
   ObjectSetString(0, g_label, OBJPROP_TEXT, "throttle");
   CacheChartWidth();
   MarkDirty(false);
   FlushChartWork();
   if(!EventSetMillisecondTimer(InpMinFlushMs))
      return INIT_FAILED;
   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
//| Expert deinitialization                                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
   ObjectDelete(0, g_label);
  }

//+------------------------------------------------------------------+
//| Timer: update objects, flush if the interval has elapsed         |
//+------------------------------------------------------------------+
void OnTimer()
  {
   ObjectSetInteger(0, g_label, OBJPROP_XDISTANCE, 10 + (int)(GetTickCount64() % 300));
   ObjectSetString(0, g_label, OBJPROP_TEXT, "width=" + IntegerToString(g_width));
   MarkDirty(true);
   FlushChartWork();
  }

//+------------------------------------------------------------------+
//| Chart change: refresh the cached width.                          |
//| Custom event: apply the update, do not send one back.            |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
  {
   if(id == CHARTEVENT_CHART_CHANGE)
     {
      CacheChartWidth();
      return;
     }
   if(id != CHARTEVENT_CUSTOM + EVENT_NEED_UPDATE)
      return;
   // lparam carries the other side's cached width in this example
   if(lparam > 0)
      g_width = lparam;
   ObjectSetString(0, g_label, OBJPROP_TEXT, sparam);
   MarkDirty(false);
   FlushChartWork();
  }
//+------------------------------------------------------------------+
Documentation on MQL5: EventChartCustom / Working with Events
Documentation on MQL5: EventChartCustom / Working with Events
  • www.mql5.com
The function generates a custom event for the specified chart. Parameters chart_id [in] Chart identifier. 0 means the current chart...