Features of the mql5 language, subtleties and tricks - page 333

 
Stefano Cerbioni #:
Subject: Feature Request: Implement OnChartSwitch event handler for monitoring the whole terminal

Description:
Currently in MQL5, an Expert Advisor is strictly bound to the chart it is running on. There is no native, event-driven way to detect when a user switches focus (clicks) between different chart tabs in the terminal, unless the EA is present on every chart.

While a high-frequency OnTimer (e.g. 50ms) could poll CHART_BRING_TO_TOP, this is inefficient and resource consuming when managing a large number of charts.

Proposed solution:
Introduce a new event handler: void OnChartSwitch(long focusedChartID, string symbol, ENUM_TIMEFRAME period).

This event should:

Trigger every time the user clicks on another chart tab in the terminal.

Be available for any EA running in the terminal, regardless of which chart the EA is physically bound to (or at least provide a global chart property accessible via OnChartEvent).

Example Usage:

Multi-Chart Dashboards: Allows a single EA controller to synchronise external instruments (via DLL/Named Pipes) or GUI dashboards instantly when a trader navigates his Market Watch or open charts.

Dynamic Analysis: Synchronise external Order Management Systems (OMS) to the current symbol without having to pin the EA to dozens of open windows.

Benefits:
Significant reduction in CPU load compared to timer-based polling and a much more responsive "Push" architecture for modern trading setups.

1 - You can save the template after installing the EA and then apply the template to other charts

2 - You can create a service to monitor window activity

 
Vladimir Pastushak #:

1 - You can save the template after installing the EA and then apply the template to other charts

2 - You can create a service to monitor window activity

Vladimir, thank you for taking the time to reply.  

However, I believe there is a misunderstanding about the core requirement. The feature request is not about how to deploy an EA on multiple charts, nor about running background code – it's about **receiving an event the moment the user switches focus between chart tabs**, without having to run the EA on every chart and without resorting to inefficient polling.

Let me explain why the suggested workarounds don't solve the real need:

1. **Templates** – Saving a template with the EA and applying it to other charts simply installs the EA on each chart. That does **not** give a single, central EA any knowledge of which chart the user has just clicked on. You would still need a way to communicate the focus change between instances (e.g., via global variables or files), which adds complexity and still doesn't make the detection event‑driven.

2. **Service** – A service runs independently, but it cannot directly know which chart is in the foreground without continuously polling all open charts using `ChartGetInteger(…, CHART_BRING_TO_TOP)`. This is exactly the same overhead as polling from an EA. Moreover, the service would then have to communicate that information back to an EA (e.g., via global variables), introducing additional latency and complexity. It’s a heavy, roundabout solution that still relies on polling.

Yes, I am aware that one can implement a polling loop with `OnTimer` and `CHART_BRING_TO_TOP`. But that’s exactly what the feature request aims to avoid: **polling is resource‑intensive**, especially when a trader keeps many charts open, and it’s far less responsive than a true push‑based event. The request for a native `OnChartSwitch` event would give us an efficient, real‑time notification directly from the terminal, with zero CPU waste.

I hope this clarifies why the existing workarounds are not adequate for the described use case. A built‑in event would benefit anyone building multi‑chart dashboards, external integrations, or any tool that needs to react instantly to the user’s current chart focus – without adding unnecessary CPU load.

Thank you again for your input, and I hope MetaQuotes will consider implementing this event in a future update.

 

One of the reasons for deinitialisation is REASON_CHARTCHANGE.

In OnDeinit during REASON_CHARTCHANGE before deinitialisation is completed, is it possible to know whatcharacter and period the chart changes to?

 
Andrei Iakovlev REASON_CHARTCHANGE.

In OnDeinit during REASON_CHARTCHANGE before deinitialisation is completed, is it possible to know whatcharacter and period the chart changes to?

Before the function completes and check,


I'm deleting handles in a loop.

I have long forgotten about this function, I need to check what REASON_REMOVE means when changing the period, I have been pulling graphs with the robot for a long time, it still remains the same.

void OnDeinit(const int reason)
  {
 
   if(reason==REASON_REMOVE)
     {
      //--- Delete indicator handles
      for(int s=TRADE_SYMBOLS-1; s>=0; s--) 
        {
         IndicatorRelease(array_Handles[s]);
         IndicatorRelease(spy_Handles[s]);
         IndicatorRelease(Handle01[s]);
         IndicatorRelease(Handle02[s]);
         IndicatorRelease(Handle03[s]);
         //---
         IndicatorRelease(HandlePeriod_01[s]);
         IndicatorRelease(HandlePeriod_02[s]);
         IndicatorRelease(HandlePeriod_03[s]);
        }
     }
  }
 
Andrei Iakovlev REASON_CHARTCHANGE.

In OnDeinit during REASON_CHARTCHANGE before deinitialisation is completed, is it possible to know whatcharacter and period the chart changes to?

You can write the reason of deinitialisation into a global variable named ChartID in OnDeinit. And in OnInit read this flag.
 
fxsaber #:
You can write the reason for deinitialisation to a global variable named ChartID in OnDeinit. And read this flag in OnInit.

I want to do certain actions before deinitialisation is completed, and for this I need to know what the newsymbol and period will be.

Apparently it is impossible to know them at the moment.

 
Andrei Iakovlev #:

One of the reasons for deinitialisation is REASON_CHARTCHANGE.

In OnDeinit during REASON_CHARTCHANGE before deinitialisation is completed, is it possible to know whatcharacter and period the chart changes to?

Not possible. What to do ?
 
Alain Verleyen #:
What to do?

Please pass on to MQ a request with the addition of such functionality, maybe it is possible to implement it. I think it is possible.

Before deinitialisation is complete, I want to change some graphical objects on the chart depending on it's new character and period.

I can of course do it in the subsequent OnInit, but I want to do it also in OnDeinit to see the changes in the graphical objects on the chart faster.

 
Andrei Iakovlev #:

Please pass on to MQ a request with the addition of such functionality, maybe it is possible to implement it. I think it is possible.

Before deinitialisation is complete, I want to change some graphical objects on the chart depending on it's new character and period.

I can of course do it in the subsequent OnInit, but I want to do it also in OnDeinit to see the changes in the graphical objects on the chart faster.

Doesn't make sense to me sorry.