bool On_Off = false; string bName = "button1"; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { if (ObjectFind(0, bName) < 0) ObjectCreate(0, bName, OBJ_BUTTON, 0, 0, 0); ObjectSetInteger(0, bName, OBJPROP_BGCOLOR, clrOrange); ObjectSetInteger(0, bName, OBJPROP_BORDER_COLOR, clrOrange); ObjectSetInteger(0, bName, OBJPROP_COLOR, clrWhite); ObjectSetInteger(0, bName, OBJPROP_YSIZE, 30); ObjectSetInteger(0, bName, OBJPROP_XSIZE, 100); ObjectSetInteger(0, bName, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetInteger(0, bName, OBJPROP_YDISTANCE, 30); ObjectSetInteger(0, bName, OBJPROP_XDISTANCE, 30); ObjectSetString(0, bName, OBJPROP_TEXT, "BUY"); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectDelete(0, bName); } //+------------------------------------------------------------------+ void OnChartEvent(const int id, // Event ID const long& lparam, // Parameter of type long event const double& dparam, // Parameter of type double event const string& sparam) // Parameter of type string events { if(id == CHARTEVENT_OBJECT_CLICK) { if(sparam == bName) { if (On_Off == false) { On_Off = true; ObjectSetInteger(0, sparam, OBJPROP_BGCOLOR, clrGray); } else { On_Off = false; ObjectSetInteger(0, sparam, OBJPROP_BGCOLOR, clrOrange); } } } } //+------------------------------------------------------------------+
hello friend ,
I need help , i make button that open order ,
the problem is when click on , the order is open and when click off the order open again .
how can i prevent it ,
i want only open in click on ,
click off is initial state.
Hello
You can check the state of the button with
button_state=ObjectGetInteger(ChartID(),name,OBJPROP_STATE);
then you follow this sequence (as you already have some of it)
- if an object is clicked
- and it is my button
- i get its new state (on(true)/off(false))
- if the new state is on i open trade
ReflectSwitchState("button1",clrGreenYellow,clrCrimson); //so if the new state is on if(ObjectGetInteger(ChartID(),"button1",OBJPROP_STATE){ TradeEntry(0); } //if not you do nothing }
even better , you can evolve the reflect switch function to also tell you what the switch is ,like this
bool ReflectSwitchState(string Name,color color_on,color color_off){ if (ObjectGetInteger(ChartID(),Name,OBJPROP_STATE)) { ObjectSetInteger(ChartID(), Name, OBJPROP_BGCOLOR,color_on); return(true);//tell the caller this is on } else { ObjectSetInteger(ChartID(), Name, OBJPROP_BGCOLOR,color_off); } return(false);//tell the caller this is off }
and then request the state immediately while displaying the state at the same time . In human language it would look like this :
if the reflect function would be "Check if ice cream exists , if yes smile , if not cry"
you could wrap it like so if(CheckForIceCream()){EatIceCream!} because if the ice cream exists you can eat it
Similarly
//you display and check at the same time if(ReflectSwitchState("button1",clrGreenYellow,clrCrimson)){ /* This means i will reflect the switch on the object and if its on i will also open a trade */ TradeEntry(0); } //if not you do nothing
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
hello friend ,
I need help , i make button that open order ,
the problem is when click on , the order is open and when click off the order open again .
how can i prevent it ,
i want only open in click on ,
click off is initial state.