OnChartEvent() not triggered

 

Hi

I'm simply trying to create a button that prints "1" when I click it with the following code.

However, after I added the EA to a chart and click the button nothing happens.

Does this mean the OnChartEvent() function not being triggered?

What's wrong in this case?


Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 


Entdecken Sie neue Möglichkeiten des MetaTrader 5 mit MQL5 Gemeinschaft und Services
Entdecken Sie neue Möglichkeiten des MetaTrader 5 mit MQL5 Gemeinschaft und Services
  • 2023.08.04
  • www.mql5.com
MQL5: eine Sprache von Handelsstrategien, eingebaut in die Handelsplattform MetaTrader 5, mit der man eigene Handelsroboter, technische Indikatoren, Skripte und Funktionsbibliotheken
 

Yours is a script and executes only once. 

Use OnInit instead of OnStart.

 
//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {   

   int buttonHandle = CreateButton("Close All Buys", ButtonCornerX, ButtonCornerY, ButtonWidth, ButtonHeight);

   ChartRedraw();

//---

   return(INIT_SUCCEEDED);

  }


I put the CreateButton() function in the OnInit() instead of InStart().

But, still the OnChartEvent() doesn't trigger. Why?
 
Yu Song #:
I put the CreateButton() function in the OnInit() instead of InStart().

But, still the OnChartEvent() doesn't trigger. Why?

You need to register the event before you will receive event calls. - See documentation:


https://www.mql5.com/en/docs/constants/chartconstants/enum_chartevents

https://www.mql5.com/en/docs/event_handlers/onchartevent

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
  • www.mql5.com
Types of Chart Events - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yu Song #:
I put the CreateButton() function in the OnInit() instead of InStart().

But, still the OnChartEvent() doesn't trigger. Why?
Try this
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam){
   if(id==CHARTEVENT_OBJECT_CLICK){
      if(sparam=="Close All Buys") Print(1);
   }
}