Nicholishen replies to: Help with EA buttons, please

I have a problem I’m not sure how to address or describe well, sorry, but I’ll try.

I’ve written an EA with on chart buttons to assist me in my manual trading, when the EA first loads on the chart, everything is fine, I can click the buttons and expect to get the results I want, however, after some time (random), the buttons that use the false/true OBJPROP_STATE return to their default, which is “false” even if they look “true” (clicked) on the chart itself.

I took the buttons code from an EA someone was paid to code for me, maybe it wasn’t written well, maybe I don’t know how to use it well, I don’t know but this is driving me crazy while debugging it, everything works as long as I look at it, I go away for an hour (some times longer or shorter than that), I look at the chart and while everything looks fine, nothing works until I re-click all the clickable sunk/unsunk (sorry, not sure how it is called) buttons.

Thanks.

Inserted Code

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() { ObjectCreate(ChartID(),"Longs Disabled",OBJ_BUTTON,0,0,0); ObjectSet("Longs Disabled",OBJPROP_BGCOLOR,clrRed); ObjectSet("Longs Disabled",OBJPROP_CORNER,1); ObjectSet("Longs Disabled",OBJPROP_YDISTANCE,60); ObjectSet("Longs Disabled",OBJPROP_XDISTANCE,120); ObjectSet("Longs Disabled",OBJPROP_XSIZE,120); ObjectSet("Longs Disabled",OBJPROP_YSIZE,30); ObjectSetString(ChartID(),"Longs Disabled",OBJPROP_TEXT,"Longs Disabled"); ObjectSet("Longs Disabled",OBJPROP_COLOR,White); ObjectCreate(ChartID(),"Shorts Disabled",OBJ_BUTTON,0,0,0); ObjectSet("Shorts Disabled",OBJPROP_BGCOLOR,clrRed); ObjectSet("Shorts Disabled",OBJPROP_CORNER,1); ObjectSet("Shorts Disabled",OBJPROP_YDISTANCE,90); ObjectSet("Shorts Disabled",OBJPROP_XDISTANCE,120); ObjectSet("Shorts Disabled",OBJPROP_XSIZE,120); ObjectSet("Shorts Disabled",OBJPROP_YSIZE,30); ObjectSetString(ChartID(),"Shorts Disabled",OBJPROP_TEXT,"Shorts Disabled"); ObjectSet("Shorts Disabled",OBJPROP_COLOR,White); ObjectCreate(ChartID(),"Close All",OBJ_BUTTON,0,0,0); ObjectSet("Close All",OBJPROP_BGCOLOR,clrRed); ObjectSet("Close All",OBJPROP_CORNER,1); ObjectSet("Close All",OBJPROP_YDISTANCE,120); ObjectSet("Close All",OBJPROP_XDISTANCE,120); ObjectSet("Close All",OBJPROP_XSIZE,120); ObjectSet("Close All",OBJPROP_YSIZE,30); ObjectSetString(ChartID(),"Close All",OBJPROP_TEXT,"Close All"); ObjectSet("Close All",OBJPROP_COLOR,White); ObjectCreate(ChartID(),"Disabled",OBJ_BUTTON,0,0,0); ObjectSet("Disabled",OBJPROP_BGCOLOR,clrRed); ObjectSet("Disabled",OBJPROP_CORNER,1); ObjectSet("Disabled",OBJPROP_YDISTANCE,150); ObjectSet("Disabled",OBJPROP_XDISTANCE,120); ObjectSet("Disabled",OBJPROP_XSIZE,120); ObjectSet("Disabled",OBJPROP_YSIZE,30); ObjectSetString(ChartID(),"Disabled",OBJPROP_TEXT,"Disabled"); ObjectSet("Disabled",OBJPROP_COLOR,White); ObjectCreate(ChartID(),"Buy",OBJ_BUTTON,0,0,0); ObjectSet("Buy",OBJPROP_BGCOLOR,clrGreen); ObjectSet("Buy",OBJPROP_CORNER,1); ObjectSet("Buy",OBJPROP_YDISTANCE,180); ObjectSet("Buy",OBJPROP_XDISTANCE,120); ObjectSet("Buy",OBJPROP_XSIZE,120); ObjectSet("Buy",OBJPROP_YSIZE,30); ObjectSetString(ChartID(),"Buy",OBJPROP_TEXT,"Buy"); ObjectSet("Buy",OBJPROP_COLOR,White); ObjectCreate(ChartID(),"Sell",OBJ_BUTTON,0,0,0); ObjectSet("Sell",OBJPROP_BGCOLOR,clrGreen); ObjectSet("Sell",OBJPROP_CORNER,1); ObjectSet("Sell",OBJPROP_YDISTANCE,210); ObjectSet("Sell",OBJPROP_XDISTANCE,120); ObjectSet("Sell",OBJPROP_XSIZE,120); ObjectSet("Sell",OBJPROP_YSIZE,30); ObjectSetString(ChartID(),"Sell",OBJPROP_TEXT,"Sell"); ObjectSet("Sell",OBJPROP_COLOR,White); return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{ if (id==CHARTEVENT_OBJECT_CLICK) { if (sparam=="Longs Disabled") { if (AllowLongs) { AllowLongs=false; ObjectSetInteger(ChartID(), "Longs Disabled",OBJPROP_STATE,false); ObjectSetInteger(ChartID(), "Longs Disabled", OBJPROP_BGCOLOR, clrRed); ObjectSetString(ChartID(), "Longs Disabled", OBJPROP_TEXT, "Longs Disabled"); } else { AllowLongs=true; ObjectSetInteger(ChartID(), "Longs Disabled",OBJPROP_STATE,true); ObjectSetInteger(ChartID(), "Longs Disabled", OBJPROP_BGCOLOR, clrGreen); ObjectSetString(ChartID(), "Longs Disabled", OBJPROP_TEXT, "Longs Enabled"); } } if (sparam=="Shorts Disabled") { if (AllowShorts) { AllowShorts=false; ObjectSetInteger(ChartID(), "Shorts Disabled",OBJPROP_STATE,false); ObjectSetInteger(ChartID(), "Shorts Disabled", OBJPROP_BGCOLOR, clrRed); ObjectSetString(ChartID(), "Shorts Disabled", OBJPROP_TEXT, "Shorts Disabled"); } else { AllowShorts=true; ObjectSetInteger(ChartID(), "Shorts Disabled",OBJPROP_STATE,true); ObjectSetInteger(ChartID(), "Shorts Disabled", OBJPROP_BGCOLOR, clrGreen); ObjectSetString(ChartID(), "Shorts Disabled", OBJPROP_TEXT, "Shorts Enabled"); } } if (sparam=="Close All") { CloseAll(); ObjectSetInteger(ChartID(), "Close All", OBJPROP_STATE,false); } if (sparam=="Disabled") { if (NotDisabled) { NotDisabled=false; ObjectSetInteger(ChartID(), "Disabled",OBJPROP_STATE,false); ObjectSetInteger(ChartID(), "Disabled", OBJPROP_BGCOLOR, clrRed); ObjectSetString(ChartID(), "Disabled", OBJPROP_TEXT, "Disabled"); } else { NotDisabled=true; ObjectSetInteger(ChartID(), "Disabled",OBJPROP_STATE,true); ObjectSetInteger(ChartID(), "Disabled", OBJPROP_BGCOLOR, clrGreen); ObjectSetString(ChartID(), "Disabled", OBJPROP_TEXT, "Enabled"); } } if (sparam=="Buy") { if (NotDisabled) { UpCycle(); } ObjectSetInteger(ChartID(), "Buy", OBJPROP_STATE,false); } if (sparam=="Sell") { if (NotDisabled) { DownCycle(); } ObjectSetInteger(ChartID(), "Sell", OBJPROP_STATE,false); } }
}
//+------------------------------------------------------------------+

Leave a Reply

Your email address will not be published. Required fields are marked *