Download MetaTrader 5

Help with Button Depressed/Released

To add comments, please log in or register
GrumpyDuckMan
499
GrumpyDuckMan  
+------------------------------------------------------------------+
//|                                                      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);
  }
//+------------------------------------------------------------------+
GrumpyDuckMan
499
GrumpyDuckMan  

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

Keith Watford
Moderator
10541
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
Ex Ovo Omnia
3389
Ex Ovo Omnia  
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.
GrumpyDuckMan
499
GrumpyDuckMan  
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);
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
10541
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‌

GrumpyDuckMan
499
GrumpyDuckMan  
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?
honest_knave
Moderator
2293
honest_knave  
GrumpyDuckMan:
Does trading codes work on Demo account?

Yes
GrumpyDuckMan
499
GrumpyDuckMan  

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.

To add comments, please log in or register