Help with Button Depressed/Released

 
+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict 
string InpName="Hello";
string InpName1="Bye";

//+------------------------------------------------------------------+ 
//| Create the button                                                | 
//+------------------------------------------------------------------+ 
bool Button(const long   ID=0,               // chart's ID 
            const string name="",                  // button name 
            const int    sub_window=0,             // subwindow index 
            const int    x=100,                    // X coordinate 
            const int    y=10,                     // Y coordinate 
            const string text="")                  // text 
            
            
         {
   if(!ObjectCreate(ID,name,OBJ_BUTTON,sub_window,0,0,0))
 
   ObjectSetInteger(ID,name,OBJPROP_XDISTANCE,0);
   ObjectSetInteger(ID,name,OBJPROP_YDISTANCE,50);
   ObjectSetInteger(ID,name,OBJPROP_XSIZE,80);
   ObjectSetInteger(ID,name,OBJPROP_YSIZE,30);
   ObjectSetInteger(ID,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
 
   ObjectSetString(ID,name,OBJPROP_TEXT,text);
   ObjectSetString(ID,name,OBJPROP_FONT,"Arial"); 
   ObjectSetInteger(ID,name,OBJPROP_FONTSIZE,10); 
 
   ObjectSetInteger(ID,name,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(ID,name,OBJPROP_BGCOLOR,clrBlue); 
   ObjectSetInteger(ID,name,OBJPROP_BORDER_COLOR,clrWhiteSmoke); 
 
   ObjectSetInteger(ID,name,OBJPROP_BACK,true); 
   ObjectSetInteger(ID,name,OBJPROP_STATE,true);             //button is pressed.
   ObjectSetInteger(ID,name,OBJPROP_SELECTABLE,false); 
   ObjectSetInteger(ID,name,OBJPROP_SELECTED,true); 
   ObjectSetInteger(ID,name,OBJPROP_HIDDEN,true); 
   ObjectSetInteger(ID,name,OBJPROP_ZORDER,0); 
 return(true);
  }
        
bool Button1(const long   ID1=0,               // chart's ID 
             const string name1="",                  // button name 
             const int    sub_window1=0,             // subwindow index 
             const int    x1=100,                    // X coordinate 
             const int    y1=10,                     // Y coordinate 
             const string text1="")                  // text                    
  {
   if(!ObjectCreate(ID1,name1,OBJ_BUTTON,sub_window1,0,0,0))
   ObjectSetInteger(ID1,name1,OBJPROP_XDISTANCE,0);
   ObjectSetInteger(ID1,name1,OBJPROP_YDISTANCE,80);
   ObjectSetInteger(ID1,name1,OBJPROP_XSIZE,80);
   ObjectSetInteger(ID1,name1,OBJPROP_YSIZE,30);
   ObjectSetInteger(ID1,name1,OBJPROP_CORNER,CORNER_LEFT_UPPER);
  
   ObjectSetString(ID1,name1,OBJPROP_TEXT,text1);
   ObjectSetString(ID1,name1,OBJPROP_FONT,"Arial"); 
   ObjectSetInteger(ID1,name1,OBJPROP_FONTSIZE,10); 
 
   ObjectSetInteger(ID1,name1,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(ID1,name1,OBJPROP_BGCOLOR,clrBlue); 
   ObjectSetInteger(ID1,name1,OBJPROP_BORDER_COLOR,clrWhiteSmoke); 
  
   ObjectSetInteger(ID1,name1,OBJPROP_BACK,true); 
   ObjectSetInteger(ID1,name1,OBJPROP_STATE,true);             //button is pressed.
   ObjectSetInteger(ID1,name1,OBJPROP_SELECTABLE,false); 
   ObjectSetInteger(ID1,name1,OBJPROP_SELECTED,true); 
   ObjectSetInteger(ID1,name1,OBJPROP_HIDDEN,true); 
   ObjectSetInteger(ID1,name1,OBJPROP_ZORDER,0); 
   return(true);
  }
 //+------------------------------------------------------------------+ 
//| Script program start function                                    | 
//+------------------------------------------------------------------+ 
void OnStart()
  {
   int x=0;
   int y=0;
    ChartRedraw();
    if(!Button(0,InpName,0,x,y,"Sell"))
     {
     }
   int x1=0;
   int y1=0;
    if(!Button1(0,InpName1,0,x1,y1,"Buy"))
  {
  }
    Sleep(8000);
  }
//+------------------------------------------------------------------+
 

I have been trying to find some topics that discuss how to control buttons on current chart. 

 

   if(ObjectGetInteger(0,Name+"Buy Button",OBJPROP_STATE)==true)
     {
     //Do Something
      ObjectSetInteger(0,Name+"Buy Button",OBJPROP_STATE,false);
     }

the above is an example and I place the code in OnChartEvent() so that it doesn't need to wait for  a tick to be executed
 
Keith Watford:

   if(ObjectGetInteger(0,Name+"Buy Button",OBJPROP_STATE)==true)
     {
     //Do Something
      ObjectSetInteger(0,Name+"Buy Button",OBJPROP_STATE,false);
     }

the above is an example and I place the code in OnChartEvent() so that it doesn't need to wait for  a tick to be executed

The OP's code is a script, he has not the OnChartEvent option. However he may use your snippet in a loop.
 
Ex Ovo Omnia:

The OP's code is a script, he has not the OnChartEvent option. However he may use your snippet in a loop.

Your correct, the code is a script. I'm not sure how to code in MetaEditor correctly. I found Keith Watford code snippet gave me a list of warnings and errors. I slowly worked my way through the warning & errors to get to this stage.

I get this: 'OnChartEvent' - function must have a body line 10 column 6‌

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict 
string BuyButton="Buy";
string SellButton="Sell";

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 
                  
  );
//+------------------------------------------------------------------+ 
//| Create the button                                                | 
//+------------------------------------------------------------------+ 
bool Button(const long   ID=0,               // chart's ID 
            const string name="",                  // button name 
            const int    sub_window=0,             // subwindow index 
            const int    x=100,                    // X coordinate 
            const int    y=10)                     // Y coordinate
               
         {
   if(!ObjectCreate(ID,name,OBJ_BUTTON,sub_window,0,0))
 
   ObjectSetInteger(ID,name,OBJPROP_XDISTANCE,0);
   ObjectSetInteger(ID,name,OBJPROP_YDISTANCE,50);
   ObjectSetInteger(ID,name,OBJPROP_XSIZE,80);
   ObjectSetInteger(ID,name,OBJPROP_YSIZE,30);
   ObjectSetInteger(ID,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
 
   ObjectSetString(ID,name,OBJPROP_TEXT,"Sell");
   ObjectSetString(ID,name,OBJPROP_FONT,"Arial"); 
   ObjectSetInteger(ID,name,OBJPROP_FONTSIZE,10); 
 
   ObjectSetInteger(ID,name,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(ID,name,OBJPROP_BGCOLOR,clrBlue); 
   ObjectSetInteger(ID,name,OBJPROP_BORDER_COLOR,clrWhiteSmoke); 
 
   ObjectSetInteger(ID,name,OBJPROP_BACK,true); 
   ObjectSetInteger(ID,name,OBJPROP_STATE,true);             //button is pressed.
   ObjectSetInteger(ID,name,OBJPROP_SELECTABLE,false); 
   ObjectSetInteger(ID,name,OBJPROP_SELECTED,true); 
   ObjectSetInteger(ID,name,OBJPROP_HIDDEN,true); 
   ObjectSetInteger(ID,name,OBJPROP_ZORDER,0); 
 return(true);
  }
        
bool Button1( long   ID1=1,               // chart's ID 
              string name1="",                  // button name 
              int    sub_window1=1,             // subwindow index 
              int    x1=100,                    // X coordinate 
              int    y1=100)                    // Y coordinate 
  {
   if(!ObjectCreate(ID1,name1,OBJ_BUTTON,sub_window1,0,0))
   ObjectSetInteger(ID1,name1,OBJPROP_XDISTANCE,0);
   ObjectSetInteger(ID1,name1,OBJPROP_YDISTANCE,80);
   ObjectSetInteger(ID1,name1,OBJPROP_XSIZE,80);
   ObjectSetInteger(ID1,name1,OBJPROP_YSIZE,30);
   ObjectSetInteger(ID1,name1,OBJPROP_CORNER,CORNER_LEFT_UPPER);
  
   ObjectSetString(ID1,name1,OBJPROP_TEXT,"Buy");
   ObjectSetString(ID1,name1,OBJPROP_FONT,"Arial"); 
   ObjectSetInteger(ID1,name1,OBJPROP_FONTSIZE,10); 
 
   ObjectSetInteger(ID1,name1,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(ID1,name1,OBJPROP_BGCOLOR,clrBlue); 
   ObjectSetInteger(ID1,name1,OBJPROP_BORDER_COLOR,clrWhiteSmoke); 
  
   ObjectSetInteger(ID1,name1,OBJPROP_BACK,true); 
   ObjectSetInteger(ID1,name1,OBJPROP_STATE,true);             //button is pressed.
   ObjectSetInteger(ID1,name1,OBJPROP_SELECTABLE,false); 
   ObjectSetInteger(ID1,name1,OBJPROP_SELECTED,true); 
   ObjectSetInteger(ID1,name1,OBJPROP_HIDDEN,true); 
   ObjectSetInteger(ID1,name1,OBJPROP_ZORDER,0); 
   return(true);
  }
  
 
 
 //+------------------------------------------------------------------+ 
//| Script program start function                                    | 
//+------------------------------------------------------------------+ 
void OnStart()
  {
 
   int x=100;
   int y=10;
    ChartRedraw();
    if(!Button(0,BuyButton,0,x,y))
     {
     }
   int x1=0;
   int y1=0;
    if(!Button1(0,SellButton,0,x1,y1))
  {
  
  if(ObjectGetInteger(0,BuyButton+"Buy",OBJPROP_STATE)==true)
     {
     //Do Something

      ObjectSetInteger(0,BuyButton+"Buy",OBJPROP_STATE,false);
     }
  }
    Sleep(8000);
  }
//+------------------------------------------------------------------+
 

Yes, I didn't realise that it is a script. You cannot use OnChartEvent in a script

if(ObjectGetInteger(0,BuyButton+"Buy",OBJPROP_STATE)==true)


You have to put your button name in‌

Use the code in a loop as Ovo suggests or make an EA or Indicator instead‌

 
Keith Watford:

Yes, I didn't realise that it is a script. You cannot use OnChartEvent in a script

if(ObjectGetInteger(0,BuyButton+"Buy",OBJPROP_STATE)==true)


You have to put your button name in‌

Use the code in a loop as Ovo suggests or make an EA or Indicator instead‌

I was thinking of making the code "Expert Advisors". I am trying to sort my way through a lot of the manuals, references and code snippets. I have just one more question to ask........Does trading codes work on Demo account?
 
GrumpyDuckMan:
Does trading codes work on Demo account?

Yes
 

I am now starting to write an EA code instead of a script.

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
    if(ObjectGetInteger(0,BuyButton+"Buy Button",OBJPROP_STATE)==true)
     {
    

      ObjectSetInteger(0,BuyButton+"Buy Button",OBJPROP_STATE,false);
     }

    {
    }

I have included Timer function and Tester function. Currently still reading what these functions are for and how to use them.

 

I implemented this function in my EA into the OnChartEvent. The problem ist, that it only sometimes unpress the button.

Can anyone tell me what´s wrong here?

The example code is below to see this strange behaviour





string objButton = "|Button";
string objLabel = "|Label";
string objEdit ="|Edit";

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   btnCreate();
   labelCreate();
   editCreate();

 

   ChartRedraw();
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  ObjectDelete(0,objButton);
  ObjectDelete(0,objLabel);
   ObjectDelete(0,objEdit);
 

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Identifikator des Ereignisses    
                  const long& lparam,   // Parameter des Ereignisses des Typs long 
                  const double& dparam, // Parameter des Ereignisses des Typs double 
                  const string& sparam  // Parameter des Ereignisses des Typs string 
  )
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
    {
      if(sparam == objButton)
      {
         Print(__FUNCTION__," > der Button wurde geklickt");
        if(ObjectGetInteger(0,objButton,OBJPROP_STATE)==true)
        {
         Sleep(100);
         Print("in button gedrückt fkt.");
         ObjectSetInteger(0,objButton,OBJPROP_STATE,false);
        } 
      }
      
      else if(sparam == objEdit)
      {
         Print(__FUNCTION__," > das Edit wurde gecklickt");
      }
      
      else if(sparam == objLabel)
      {
         Print(__FUNCTION__," > das Label wurde gecklickt");
      }
    }
    if(ObjectGetInteger(0,objButton,OBJPROP_STATE)==true)
     {
      Print("in button gedrückt fkt.");
      ObjectSetInteger(0,objButton,OBJPROP_STATE,false);
     }
  
  }
   
  
//+------------------------------------------------------------------+

void btnCreate ()
   {
   ObjectCreate(0,objButton,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,objButton,OBJPROP_XDISTANCE,30);
   ObjectSetInteger(0,objButton,OBJPROP_YDISTANCE,30);
   ObjectSetInteger(0,objButton,OBJPROP_XSIZE,100);
   ObjectSetInteger(0,objButton,OBJPROP_YSIZE,30);
   ObjectSetString(0,objButton,OBJPROP_TEXT,"Klick mich");
   }
   
void labelCreate()
   {
   ObjectCreate(0,objLabel,OBJ_LABEL,0,0,0);
   ObjectSetInteger(0,objLabel,OBJPROP_XDISTANCE,30);
   ObjectSetInteger(0,objLabel,OBJPROP_YDISTANCE,80);
   ObjectSetInteger(0,objLabel,OBJPROP_FONTSIZE,15);
   ObjectSetString(0,objButton,OBJPROP_TEXT,"Ich bin ein Label");
   ObjectSetString(0,objLabel,OBJPROP_TOOLTIP,"Das ist ein Tooltip");
   
   }
   
void editCreate()

   {
   ObjectCreate(0,objEdit,OBJ_EDIT,0,0,0);
   ObjectSetInteger(0,objEdit,OBJPROP_XDISTANCE,30);
   ObjectSetInteger(0,objEdit,OBJPROP_YDISTANCE,120);
   ObjectSetInteger(0,objEdit,OBJPROP_XSIZE,100);
   ObjectSetInteger(0,objEdit,OBJPROP_YSIZE,30);
   ObjectSetString(0,objEdit,OBJPROP_TEXT,"Das ist ein Edit");


   }
 

Try using ChartRedraw(0);


    if(ObjectGetInteger(0,objButton,OBJPROP_STATE)==true)
    {
         Sleep(100);
         Print("in button gedrückt fkt.");
         ObjectSetInteger(0,objButton,OBJPROP_STATE,false);         
         ChartRedraw(0);

  
    } 
Reason: