how can prevent button click event between click on and off

 

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.


int OnInit()
  {
createButton("button1",clrOrange,clrOrange,clrWhite,100,30,30,30,"BUY");
 ReflectSwitchState("button1",clrGreenYellow,clrCrimson);
 }

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=="button1"){
   ReflectSwitchState("button1",clrGreenYellow,clrCrimson);
   TradeEntry(0);}
  }

void createButton(string bName, color bgClr, color bClr, color tClr, int width, int height,int x, int y, string text){
      ObjectCreate(0,bName,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,bName,OBJPROP_BGCOLOR,bgClr);
      ObjectSetInteger(0,bName,OBJPROP_BORDER_COLOR,bClr);
      ObjectSetInteger(0,bName,OBJPROP_COLOR,tClr);
      ObjectSetInteger(0,bName,OBJPROP_YSIZE,height);
      ObjectSetInteger(0,bName,OBJPROP_XSIZE,width);
      ObjectSetInteger(0,bName,OBJPROP_CORNER,CORNER_LEFT_UPPER);
      ObjectSetInteger(0,bName,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,bName,OBJPROP_XDISTANCE,x);
      ObjectSetString(0,bName,OBJPROP_TEXT,text);
     
}


void ReflectSwitchState(string Name,color color_on,color color_off){
   if (ObjectGetInteger(ChartID(),Name,OBJPROP_STATE))
   {
       ObjectSetInteger(ChartID(), Name, OBJPROP_BGCOLOR,color_on);
   }
   else
   {
        ObjectSetInteger(ChartID(), Name, OBJPROP_BGCOLOR,color_off);
   }

 
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);
         }
      }
   }
}
//+------------------------------------------------------------------+
 
LONNV:

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)

  1. if an object is clicked
  2. and it is my button
  3. i get its new state (on(true)/off(false))
  4. 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