Detected Country () : English
Button Object - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

Button Object

Level Advance [2018-02-19 15:37:54] - by hillzx
Featured Image

Hello fellow traders. This time we will learn to make simple buttons seen on the chart. The button can be used for transactions. On this occasion we will learn to make 3 pieces of buttons which are:

  1. Buy Button
  2. Sell Button
  3. Close Transaction Button

We set Lotsize, stoploss and takeprofit by Inputs :

 

extern double LotSize=0.01;
extern int StopLoss=500;
extern int TakeProfit=500;
int Slippage=5;

 

Next create button in OnInit Module :

 

int OnInit()
  {
//---
int x=10;

CreateButton("button1","Buy",x,20,80,30,Yellow,Green,White,12);
CreateButton("button2","Sell",x,20,80,30,Yellow,Maroon,White,12);
CreateButton("button3","Close "+Symbol(),x,20,140,30,White,Gray,White,12);

//---
   return(INIT_SUCCEEDED);
  }

This means that we create 3 buttons through the CreateButton sub-module, the contents of the CreateButton module code are like this:

 

void CreateButton(string btnName,string btnText,int &x,int y,int w,int h,color clrText,color clrBg,color clrBorder,int fontSize)
  {
   ObjectCreate(0,btnName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,btnName,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,btnName,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,btnName,OBJPROP_XSIZE,w);
   ObjectSetInteger(0,btnName,OBJPROP_YSIZE,h);
   ObjectSetString(0,btnName,OBJPROP_TEXT,btnText);
   ObjectSetInteger(0,btnName,OBJPROP_COLOR,clrText);
   ObjectSetInteger(0,btnName,OBJPROP_BGCOLOR,clrBg);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_COLOR,clrBorder);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,btnName,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,btnName,OBJPROP_STATE,false);
   ObjectSetInteger(0,btnName,OBJPROP_FONTSIZE,fontSize);
   x=x+w+10;
  }

Please note that the parameter x represents ByRef (By Reference) means that the sender variable will also be updated.



Next we add the module to remove Buttons:

 

void DeleteButtons()
  {
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"button")>=0) ObjectDelete(ObjectName(i));
     }
  }
  

 

DeleteButtons function we use to clean the screen after the EA is stopped, write it in OnDeInit module :

 

void OnDeinit(const int reason)
  {
//---
DeleteButtons();
  }

 

Then, we capture the event when the Button is clicked by adding this code:

 

  void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
      if(sparam=="button1") // Buy
        {
         Action_Button1();
         ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
        }
      else if(sparam=="button2") // Sell
        {
         Action_Button2();
         ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
        }
      else if(sparam=="button3") // Close This
        {
         Action_Button3();
         ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
        }
  
  
  }



Meaning The first button will execute module Action_Button1, The second button will execute module Action_Button2 and so on.

 

void Action_Button1(){
   if(MessageBox("Buy "+ DoubleToStr(LotSize,2) + " Lot dengan StopLoss:" + StopLoss + " points dan TakeProfit:" + TakeProfit + " points ?","Order Buy",MB_YESNO)==IDYES)
   {
      OrderBuy();
      PlaySound("ok.wav");
   }
}


void Action_Button2(){
   if(MessageBox("Sell "+ DoubleToStr(LotSize,2) + " Lot dengan Stoploss:" + StopLoss + " points dan TakeProfit:" + TakeProfit + " points ?","Order Sell",MB_YESNO)==IDYES)
   {
      OrderSell();
      PlaySound("ok.wav");
   }
}


void Action_Button3(){
   if(MessageBox("Tutup semua orderan floating pair "+ Symbol() + " ?","Tutup Orderan",MB_YESNO)==IDYES)
   {
      CloseThis();
      PlaySound("ok.wav");
   }
}



Each Action_Button module will call its own function, namely Buy, Sell, and Close:

 

void OrderBuy(){
int ticket;
   for(int i=0;i<3;i++)
   {
      ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"HiLLzX Buy");
      if(ticket>0){break;}    
      RefreshRates();     
   }

}

void OrderSell(){
int ticket;
   for(int i=0;i<3;i++)
   {
      ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"HiLLzX Sell");
      if(ticket>0){break;}    
      RefreshRates();     
   }
}

void CloseThis() {
   bool closed = false;
   for (int i = OrdersTotal(); i >=0; i--) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      while(IsTradeContextBusy()) Sleep(100);
      RefreshRates();
      if (OrderType() == OP_BUY && Symbol() == OrderSymbol() )
       {
        closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
      }
      if (OrderType() == OP_SELL && Symbol() == OrderSymbol())
        {
        closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
      }
   }
return;

}

panelbuy

Done .. So there are 3 buttons on the screen, when we clicked the button, it will show the confirmation before execution. Here's complete source code :

 

//+------------------------------------------------------------------+
//|                                                  ButtonPanel.mq4 |
//|                                                 hillzx@gmail.com |
//|                                                 hillzx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "hillzx@gmail.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

extern double LotSize=0.01;
extern int StopLoss=500;
extern int TakeProfit=500;
int Slippage=5;

int OnInit()
  {
//---
int x=10;

CreateButton("button1","Buy",x,20,80,30,Yellow,Green,White,12);
CreateButton("button2","Sell",x,20,80,30,Yellow,Maroon,White,12);
CreateButton("button3","Close "+Symbol(),x,20,140,30,White,Gray,White,12);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
DeleteButtons();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+

void CreateButton(string btnName,string btnText,int &x,int y,int w,int h,color clrText,color clrBg,color clrBorder,int fontSize)
  {
   ObjectCreate(0,btnName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,btnName,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,btnName,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,btnName,OBJPROP_XSIZE,w);
   ObjectSetInteger(0,btnName,OBJPROP_YSIZE,h);
   ObjectSetString(0,btnName,OBJPROP_TEXT,btnText);
   ObjectSetInteger(0,btnName,OBJPROP_COLOR,clrText);
   ObjectSetInteger(0,btnName,OBJPROP_BGCOLOR,clrBg);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_COLOR,clrBorder);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,btnName,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,btnName,OBJPROP_STATE,false);
   ObjectSetInteger(0,btnName,OBJPROP_FONTSIZE,fontSize);
   x=x+w+10;
  }
  
void DeleteButtons()
  {
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"button")>=0) ObjectDelete(ObjectName(i));
     }
  }
  
  
  void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
      if(sparam=="button1") // Buy
        {
         Action_Button1();
         ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
        }
      else if(sparam=="button2") // Sell
        {
         Action_Button2();
         ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
        }
      else if(sparam=="button3") // Close This
        {
         Action_Button3();
         ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
        }
  
  
  }
  
void Action_Button1(){
   if(MessageBox("Buy "+ DoubleToStr(LotSize,2) + " Lot dengan StopLoss:" + StopLoss + " points dan TakeProfit:" + TakeProfit + " points ?","Order Buy",MB_YESNO)==IDYES)
   {
      OrderBuy();
      PlaySound("ok.wav");
   }
}


void Action_Button2(){
   if(MessageBox("Sell "+ DoubleToStr(LotSize,2) + " Lot dengan Stoploss:" + StopLoss + " points dan TakeProfit:" + TakeProfit + " points ?","Order Sell",MB_YESNO)==IDYES)
   {
      OrderSell();
      PlaySound("ok.wav");
   }
}


void Action_Button3(){
   if(MessageBox("Tutup semua orderan floating pair "+ Symbol() + " ?","Tutup Orderan",MB_YESNO)==IDYES)
   {
      CloseThis();
      PlaySound("ok.wav");
   }
}

void OrderBuy(){
int ticket;
   for(int i=0;i<3;i++)
   {
      ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"HiLLzX Buy");
      if(ticket>0){break;}    
      RefreshRates();     
   }

}

void OrderSell(){
int ticket;
   for(int i=0;i<3;i++)
   {
      ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"HiLLzX Sell");
      if(ticket>0){break;}    
      RefreshRates();     
   }
}

void CloseThis() {
   bool closed = false;
   for (int i = OrdersTotal(); i >=0; i--) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      while(IsTradeContextBusy()) Sleep(100);
      RefreshRates();
      if (OrderType() == OP_BUY && Symbol() == OrderSymbol() )
       {
        closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
      }
      if (OrderType() == OP_SELL && Symbol() == OrderSymbol())
        {
        closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
      }
   }
return;

}


Good luck, do not forget to share and follow our official facebook and youtube, happy trading!



Previous Article

Featured Image

Arrow Object




- Provided by HiLLzSoft Studio -