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

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 #:
Try this I put the CreateButton() function in the OnInit() instead of InStart().
But, still the OnChartEvent() doesn't trigger. Why?
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); } }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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?