OnChartEvent does not catch CHARTEVENT_OBJECT_CREATE events

 

Hi!

The following code demonstrates the issue. Objects are created but OnChartEvent function is never called. If somebody knows why it should not work or how to make it work please help me.

long ID;
int count=0;
double Price1;
datetime Time1 = D'2017.10.26 00:00:00';

int OnInit()
  {

   ID = ChartFirst();
   
        while(ID>0){
          ChartSetInteger(ID,CHART_EVENT_OBJECT_CREATE, true);
          
          int tf = ChartPeriod(ID);
          Print("Chart event obj create ",ChartSymbol(ID)," tf=", tf);
          Price1 = MarketInfo(ChartSymbol(ID), MODE_BID);
          bool status = ObjectCreate(ID, "ID_"+count, OBJ_TEXT, 0, Time1, Price1);
          if( !status ) Print("Error=", GetLastError() );
          ID = ChartNext(ID);  
          count++;
          
        };

   return(INIT_SUCCEEDED);
  }

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//--
 Print("Name=", sparam);
 if(id == CHARTEVENT_OBJECT_CREATE)
   { 
      ObjectSetText(sparam, "txt_"+ sparam, 10, "Arial", EMPTY);
        ObjectSet(sparam, OBJPROP_COLOR, Red);
        }
  }
//+------------------------------------------------------------------+

 
This code works (tested with an EA on a live chart).
 
loylick:

Hi!

The following code demonstrates the issue. Objects are created but OnChartEvent function is never called. If somebody knows why it should not work or how to make it work please help me.


It looks to me like you are setting up several charts; but OnChartEvent() runs on a single chart, i.e. it will not catch  all events from all charts, but the events from the chart the EA is loaded to

 
Demos Stogios:

It looks to me like you are setting up several charts; but OnChartEvent() runs on a single chart, i.e. it will not catch  all events from all charts, but the events from the chart the EA is loaded to

Thanks, it makes clear the issue. On current chart it works for me too without problems. Now that it has become clear I have another question. How to change some properties of objects created in other charts (not current)? As in the example I'd like to set some text to OBJ_TEXT objects situated in some charts with known IDs.
 
loylick:
Thanks, it makes clear the issue. On current chart it works for me too without problems. Now that it has become clear I have another question. How to change some properties of objects created in other charts (not current)? As in the example I'd like to set some text to OBJ_TEXT objects situated in some charts with known IDs.


Hello

In that case, you can use ObjectSetString(), with the property OBJPROP_TEXT 

regards

 
Alain Verleyen:
This code works (tested with an EA on a live chart).
Yes, it works but not the way I'd expect. The code should create an object in every chart windows you have opened. Then it should change text for each object created. But the only object with changed text is the one situated in the current chart, objects in other charts remain with "Text" label as they had initially.
 
loylick:
Yes, it works but not the way I'd expect. The code should create an object in every chart windows you have opened. Then it should change text for each object created. But the only object with changed text is the one situated in the current chart, objects in other charts remain with "Text" label as they had initially.
It works as documented. You should not expect something which was never provided.
 
Demos Stogios:

In that case, you can use ObjectSetString(), with the property OBJPROP_TEXT

regards

Oh, thanks again! You have saved me a lot of time digging in the documentation.
 
loylick:
Oh, thanks again! You have saved me a lot of time digging in the documentation.

Your welcome :) But I may say that MQL documentation looks ok :)

regards

Reason: