Problem with OBJ_BUTTON state

 

Hi.

I Have 2 Button on my chart and I want calculate and draw something base on the candles high and low when the button state is true or false.

I want use this code in OnCalculate fanction so i can access to high and low of every candles and draw my shapes until the button state is true, but this if never execute.

  int bars = rates_total-1;
   if(prev_calculated>0)
   {bars=rates_total-(prev_calculated-1);}

   for(int i=bars; i>=0; i--)
     {
      if(ObjectGetInteger(0,"BTNONE",OBJPROP_STATE) == true)
      {
       // my code 
      }
      else
       {
        // my code 
       }
     }

even when I use the one global variable as sign for get the state of button in the EventChart Function and use this in OnCalculate that if not execute too.


  if(sparam == "BTNONE")
 {
  if(ObjectGetInteger(0,"BTONE",OBJPROP_STATE) == false)
  {
     sign=0;
   }
  else
   {
     sign=1;
   }
 }                                                                       

How can I fix this problem?

 

Hello in this case you can use the CHARTEVENT_OBJECT_CLICK event.

Please try this piece of code.

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
//--- the left mouse button has been pressed on the chart
   if(id==CHARTEVENT_CLICK)
     {
      Print("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
     }
//--- the mouse has been clicked on the graphic object
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print("The mouse has been clicked on the object with name '"+sparam+"'");
     }
//--- the key has been pressed
   if(id==CHARTEVENT_KEYDOWN)
     {
      Print("The key with code ",int(lparam)," has been pressed");
     }
//--- the object has been deleted
   if(id==CHARTEVENT_OBJECT_DELETE)
     {
      Print("The object with name ",sparam," has been deleted");
     }
//--- the object has been created
   if(id==CHARTEVENT_OBJECT_CREATE)
     {
      Print("The object with name ",sparam," has been created");
     }
//--- the object has been moved or its anchor point coordinates has been changed
   if(id==CHARTEVENT_OBJECT_DRAG)
     {
      Print("The anchor point coordinates of the object with name ",sparam," has been changed");
     }
//--- the text in the Edit of object has been changed
   if(id==CHARTEVENT_OBJECT_ENDEDIT)
     {
      Print("The text in the Edit field of the object with name ",sparam," has been changed");
     }
  }
//+------------------------------------------------------------------+
 
Marco vd Heijden:

Hello in this case you can use the CHARTEVENT_OBJECT_CLICK event.

Please try this piece of code.

thank you, But I Do it before but not working. my code have too many button and all of them work Correctly, However 2 of them as I said not working.

For example if all of your process in the ChartEvent function everything work. like this code:

 if(id == CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="Chart")
        {
         if(ObjectGetInteger(0,"Chart",OBJPROP_STATE) == false)
           {
            ChartSetInteger(0,CHART_FOREGROUND,false);
           }
         else
           {
            ChartSetInteger(0,CHART_FOREGROUND,true);
           }
        }
      }

But if you want calculate something base on the button state, is not working. for example my default button state is false and one of my objects should only draw and edit when the state is true.

I write this if on the OnCalculate function, so when the user click on button and state become true, the objects should draw and edit and if the state is false, all of them should delete from chart. now when the program start the state is false, bit even the user click on the button and state become true, the code execute without error,but my if in OnCalculate never happened.also we can not calculate that objects in the OnChartEvent, because the chart event and button body calculate once and the high,low,close,open and time array only exist in OnCalculate.

if(ObjectGetInteger(0,"BTNONE",OBJPROP_STATE) == true)

How can fix this?

 
If you are checking the button state in OnCalculate(), there must be an incoming tick first.
If the market for the chart symbol is closed, there will not be a new tick.
 
Also if you check for the object click event then sparam will refer to the object that was clicked so it's not necessary to check the button state a second time.  
 
Marco vd Heijden:
Also if you check for the object click event then sparam will refer to the object that was clicked so it's not necessary to check the button state a second time.  

thank you both. If I Correctly understand the only way is define input bool variable and when the user change this parameter to true my objects draw or edit and if the user change to false stop drawing and delete objects.

exactly I use this method right now and the problem solved.

so no way use button for this purpose?

 

You can check the button state in OnTimer() then it would not have to wait for a tick to arrive but the chart event is the best option because it doesn't need to be run in OnCalculate or OnTimer it just responds whenever something is clicked so you do not have to actively monitor objects by checking their state.

Input parameters can also be used there are many ways to do something you can just try various methods and see what works best.

If you need any further assistance please describe what you are trying to do exactly and if possible post the relevant code because at this point it is not totally clear what you want to achieve. 

Reason: