Download MetaTrader 5

OnChartEvent doesn't fire for another chart opened with ChartOpen

To add comments, please log in or register
Have you made a screenshot in MetaTrader? Share it with friends!
mbingol
26
mbingol 2015.07.26 17:59 

Hi,

 

I am trying to receive CHARTEVENT_MOUSE_MOVE event for a chart that I open with ChartOpen :

int OnInit()

  { 

//Everything is fine for the current chart, I receive the  CHART_EVENT_MOUSE_MOVE events with the following line

ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,0,true)  

 

//I open an EURUSD chart, it opens, I get a big ID number for ChartM1 

long    ChartM1 = ChartOpen("EURUSD",PERIOD_M1); 

//But the this line doesn't make it receive     //But I don't receive CHART_EVENT_MOUSE_MOVE event for EURUSDevent

ChartSetInteger(ChartM1,CHART_EVENT_MOUSE_MOVE,0,true) 

return(INIT_SUCCEEDED);

} 

 

void OnChartEvent(const int id, const long &lparam, const double &dparam, string &sparam) 

{

//CHART_EVENT_MOUSE_MOVE event is caught for current chart (id=0) but not for EURUSD (ID=ChartM1)

int a =0; 

} 

 I also couldn't hide trade levels for EURUSD. That may have a common reason. Any ideas?

 Thanks in advance

Alain Verleyen
Moderator
30707
Alain Verleyen 2015.07.26 18:47  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


Alain Verleyen
Moderator
30707
Alain Verleyen 2015.07.26 19:01  
mbingol:

Hi,

 

I am trying to receive CHARTEVENT_MOUSE_MOVE event for a chart that I open with ChartOpen :

 I also couldn't hide trade levels for EURUSD. That may have a common reason. Any ideas?

 Thanks in advance

Please read the documentation carefully :

CHART_EVENT_MOUSE_MOVE

Send notifications of mouse move and mouse click events (CHARTEVENT_MOUSE_MOVE) to all mql4 programs on a chart

bool

That means the mouse move event is NOT sent on mql4 programs running on an other chart.
mbingol
26
mbingol 2015.07.26 20:00  
Alain Verleyen:

Please read the documentation carefully :

CHART_EVENT_MOUSE_MOVE

Send notifications of mouse move and mouse click events (CHARTEVENT_MOUSE_MOVE) to all mql4 programs on a chart

bool

That means the mouse move event is NOT sent on mql4 programs running on an other chart.

Thanks for editing Alain. This is the first time I am posting in this forum. I didn't know that. 

I now see an MQLX program's scope is a chart not the Metatrader's environment. So I think I will run the same EA on multiple charts to achieve what I want. Thanks for your answer.

Marco vd Heijden
Moderator
4685
Marco vd Heijden 2015.07.26 23:46  

you can open up up many charts then use

ENUM_CHART_PROPERTY_INTEGER

ID

Description

Property Type

CHART_BRING_TO_TOP

Show chart on top of other charts

bool   w/o


Then have each chart check if it is currently on top, and if it is,

//Do Something.

Then you can cycle through and do lots of interesting things.

ChartSetInteger(chart_ID,CHART_BRING_TO_TOP,0,value);
     
ChartGetInteger(chart_ID_BRING_TO_TOP);
JD4
1101
JD4 2015.07.27 00:03  
mbingol:

Thanks for editing Alain. This is the first time I am posting in this forum. I didn't know that. 

I now see an MQLX program's scope is a chart not the Metatrader's environment. So I think I will run the same EA on multiple charts to achieve what I want. Thanks for your answer.

If I am reading Alain's post correctly, I think that you running the same EA on multiple charts will only work if the EA is specifically coded to do so.  If you do it the standard way of opening the charts you want to use, and then adding the EA to each chart individually, the EA copy on that chart will recognize only the items on that specific chart, not items running on any others.
Enrico Lambino
4320
Enrico Lambino 2015.07.27 05:31  

Running the EA on multiple charts to send events may not be necessary. It is possible to broadcast the event to other charts. See the example code here:

https://www.mql5.com/en/docs/eventfunctions/eventchartcustom

//+------------------------------------------------------------------+
//| sends broadcast event to all open charts                         |
//+------------------------------------------------------------------+
void BroadcastEvent(long lparam,double dparam,string sparam)
  {
   int eventID=broadcastEventID-CHARTEVENT_CUSTOM;
   long currChart=ChartFirst();
   int i=0;
   while(i<CHARTS_MAX)                 // We have certainly no more than CHARTS_MAX open charts
     {
      EventChartCustom(currChart,eventID,lparam,dparam,sparam);
      currChart=ChartNext(currChart); // We have received a new chart from the previous
      if(currChart==-1) break;        // Reached the end of the charts list
      i++;// Do not forget to increase the counter
     }
  }
Documentation on MQL5: Working with Events / EventChartCustom
Documentation on MQL5: Working with Events / EventChartCustom
  • www.mql5.com
Working with Events / EventChartCustom - Reference on algorithmic/automated trading language for MetaTrader 5
Alain Verleyen
Moderator
30707
Alain Verleyen 2015.07.28 13:27  
Enrico Lambino:

Running the EA on multiple charts to send events may not be necessary. It is possible to broadcast the event to other charts. See the example code here:

https://www.mql5.com/en/docs/eventfunctions/eventchartcustom

That's not a solution to the problem, as the OP was opening a new chart with no running code on it. He can't catch mouse move event. Running the EA on multiple chart is the only solution.
Enrico Lambino
4320
Enrico Lambino 2015.07.29 00:17  
Alain Verleyen:
That's not a solution to the problem, as the OP was opening a new chart with no running code on it. He can't catch mouse move event. Running the EA on multiple chart is the only solution.

I believe the solution I posted above is closer to what the OP wanted. The OP's EA opens new charts dynamically. If the OP has to run the same EA on those charts too, he'll never finish loading EAs.

Of course the EA can be refactored to only work in one chart. But the OP probably has a reason why he wanted to process events on multiple charts in a single EA. That is why I made the suggestion of broadcasting events. The OP can create something (EA, indicator, or even script that runs indefinitely) that broadcasts events back to the chart where the event-processing EA is loaded (without having to modify that EA). This is not a fully automated solution as the OP wanted, but neither does the solution of running the same EA on charts it dynamically opened.

Alain Verleyen
Moderator
30707
Alain Verleyen 2015.07.30 14:38  
Right.
To add comments, please log in or register