Detected Country () : English
EA Bollinger Band Automatic Buy Sell - 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:

EA Bollinger Band Automatic Buy Sell

Samples [2018-05-27 22:11:26] - by hillzx
Featured Image

Bollinger Bands (BB) indicator is widely used by traders in the world. BB is basically an indicator that shows 3 pieces of lines namely:

  • Upper Line
  • Middle Line
  • Lower Line

Bollinger Band is usually used as a strategy in trading in case of overlapping prices that penetrate the upper (Over Bought) or lower (Over Sold) line.

Let's try to make a simple EA using this Bollinger Bands indicator. Robot can make Buy / Sell transactions when Overlapping occurs, with Take Profit target in center line.

 

OK, the first stage, we create the variable:

extern int Corner=1;
extern int XPos=20;
extern int YPos=10;
extern int          period =20;          // averaging period
extern double       deviation=2;        // standard deviations
extern int          bands_shift=0;      // bands shift
extern ENUM_APPLIED_PRICE          applied_price;    // applied price
extern int TakeProfit=0;
extern int StopLoss=0;
extern double LotSize=0.01;

int mode;             // line index
int shift;             // shift
double priceUpper;
double priceMiddle;
double priceLower;
int oldTime;
int Slippage=3;
int MagicNumber=1234;

 

Next OnInit module, OnDeinit, DeleteObjects:

  • DeleteObjects works to clear the screen
  • UpdateBBPrice works for BB price updates
  • WriteText serves to display the BB price on the screen
  • AutomaticOrder serves to conduct Buy / Sell transactions
  • Oldtime is used as a marker that the transaction is only executed every new candle turnover
int OnInit()
  {
//---
   DeleteObjects();
   UpdateBBPrice();
   WriteText();
   AutomaticOrder();
   oldTime=Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
DeleteObjects();
  }
  
void DeleteObjects()
  {
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"HiLLzX_BB")>=0) ObjectDelete(ObjectName(i));
     }
  }



Code for automatic Buy & Sell:

void AutomaticOrder(){

if ( (Close[1]>priceUpper) && (Open[1]<Close[1]) ){
   OrderSell();
} else if ( (Close[1]<priceLower) && (Open[1]>Close[1]) ){
   OrderBuy();
}

}

 

Code to display text on screen:

void WriteText(){
ObjectCreate("HiLLzX_BB_1",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_BB_1",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_BB_1",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_BB_1",OBJPROP_YDISTANCE,0 + YPos);
ObjectSetText("HiLLzX_BB_1","Latest Upper BB : " + DoubleToStr(priceUpper,Digits) ,16,"Rockwell",White);

ObjectCreate("HiLLzX_BB_2",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_BB_2",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_BB_2",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_BB_2",OBJPROP_YDISTANCE,20 + YPos);
ObjectSetText("HiLLzX_BB_2","Latest Middle BB : " + DoubleToStr(priceMiddle,Digits) ,16,"Rockwell",Silver);

ObjectCreate("HiLLzX_BB_3",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_BB_3",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_BB_3",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_BB_3",OBJPROP_YDISTANCE,40 + YPos);
ObjectSetText("HiLLzX_BB_3","Latest Lower BB : " + DoubleToStr(priceLower,Digits) ,16,"Rockwell",White);
}

 

Code for BB price update:

void UpdateBBPrice(){
   priceUpper=iBands(NULL,0,period,deviation,bands_shift,applied_price,MODE_UPPER,1);
   priceMiddle=iBands(NULL,0,period,deviation,bands_shift,applied_price,MODE_MAIN,1);
   priceLower=iBands(NULL,0,period,deviation,bands_shift,applied_price,MODE_LOWER,1);
}   

 

Code to conduct Buy / Sell transactions and Set Take Profit:

void OrderBuy() {
double tp;
double sl;

   if (TakeProfit==0){
      tp=priceMiddle;
   } else {
      tp=Ask+TakeProfit*Point;
   }

   if (StopLoss==0){
      sl=0;
   } else {
      sl=Ask-StopLoss*Point;
   }


   int ticket;
   for(int i=0;i<3;i++)
   {
      string errorMsg;
      
      Print("Order: Buy");
      Print("SL: " + sl);
      Print("TP: " + tp);
      ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,sl,tp,"HiLLzX BB Buy",MagicNumber,0,CLR_NONE);
         
      if(ticket>0){break;}
      RefreshRates();
   }

}

void OrderSell() {
double tp;
double sl;

   if (TakeProfit==0){
      tp=priceMiddle;
   } else {
      tp=Bid-TakeProfit*Point;
   }

   if (StopLoss==0){
      sl=0;
   } else {
      sl=Bid+StopLoss*Point;
   }


   int ticket;
   for(int i=0;i<3;i++)
   {
      string errorMsg;
      Print("Order: Sell");
      Print("SL: " + sl);
      Print("TP: " + tp);
      ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,sl,tp,"HiLLzX BB Sell",MagicNumber,0,CLR_NONE);
   
      if(ticket>0){break;}
      RefreshRates();
   }

}


void ModifyTakeProfit(double idTakeProfit)
  {
   bool fmTP;
   fmTP=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),idTakeProfit,0,CLR_NONE);
  }
  
 void SetTP(){
 
   int itotal;
   itotal=OrdersTotal();

   for(int cnt=itotal-1;cnt>=0;cnt--)
   {
      bool tmpselect=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {
         ModifyTakeProfit(priceMiddle);
        }

      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {
         ModifyTakeProfit(priceMiddle);
        }
   }
 }



 

Finally, the main code is OnTick:

void OnTick()
  {
//---
   if(oldTime != Time[0] )
   {
      oldTime = Time[0];
      UpdateBBPrice();
      WriteText();
      AutomaticOrder();
      SetTP();
   } 
  }

 

Views like this:

BB

 

Done, if you need the full code please copy and paste the code below:

//+------------------------------------------------------------------+
//|                                           HiLLzX BB Buy Sell.mq4 |
//|                                                 hillzx@gmail.com |
//|                                                 hillzx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "hillzx@gmail.com"
#property version   "1.00"
#property strict
extern int Corner=1;
extern int XPos=20;
extern int YPos=10;
extern int          period =20;          // averaging period
extern double       deviation=2;        // standard deviations
extern int          bands_shift=0;      // bands shift
extern ENUM_APPLIED_PRICE          applied_price;    // applied price
extern int TakeProfit=0;
extern int StopLoss=0;
extern double LotSize=0.01;

int mode;             // line index
int shift;             // shift
double priceUpper;
double priceMiddle;
double priceLower;
int oldTime;
int Slippage=3;
int MagicNumber=1234;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   DeleteObjects();
   UpdateBBPrice();
   WriteText();
   AutomaticOrder();
   oldTime=Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
DeleteObjects();
  }
  
void DeleteObjects()
  {
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"HiLLzX_BB")>=0) ObjectDelete(ObjectName(i));
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(oldTime != Time[0] )
   {
      oldTime = Time[0];
      UpdateBBPrice();
      WriteText();
      AutomaticOrder();
      SetTP();
   } 
      
      
  }
//+------------------------------------------------------------------+

void AutomaticOrder(){

if ( (Close[1]>priceUpper) && (Open[1]<Close[1]) ){
   OrderSell();
} else if ( (Close[1]<priceLower) && (Open[1]>Close[1]) ){
   OrderBuy();
}

}



void WriteText(){
ObjectCreate("HiLLzX_BB_1",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_BB_1",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_BB_1",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_BB_1",OBJPROP_YDISTANCE,0 + YPos);
ObjectSetText("HiLLzX_BB_1","Latest Upper BB : " + DoubleToStr(priceUpper,Digits) ,16,"Rockwell",White);

ObjectCreate("HiLLzX_BB_2",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_BB_2",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_BB_2",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_BB_2",OBJPROP_YDISTANCE,20 + YPos);
ObjectSetText("HiLLzX_BB_2","Latest Middle BB : " + DoubleToStr(priceMiddle,Digits) ,16,"Rockwell",Silver);

ObjectCreate("HiLLzX_BB_3",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_BB_3",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_BB_3",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_BB_3",OBJPROP_YDISTANCE,40 + YPos);
ObjectSetText("HiLLzX_BB_3","Latest Lower BB : " + DoubleToStr(priceLower,Digits) ,16,"Rockwell",White);
}




void UpdateBBPrice(){
   priceUpper=iBands(NULL,0,period,deviation,bands_shift,applied_price,MODE_UPPER,1);
   priceMiddle=iBands(NULL,0,period,deviation,bands_shift,applied_price,MODE_MAIN,1);
   priceLower=iBands(NULL,0,period,deviation,bands_shift,applied_price,MODE_LOWER,1);
}   



void OrderBuy() {
double tp;
double sl;

   if (TakeProfit==0){
      tp=priceMiddle;
   } else {
      tp=Ask+TakeProfit*Point;
   }

   if (StopLoss==0){
      sl=0;
   } else {
      sl=Ask-StopLoss*Point;
   }


   int ticket;
   for(int i=0;i<3;i++)
   {
      string errorMsg;
      
      Print("Order: Buy");
      Print("SL: " + sl);
      Print("TP: " + tp);
      ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,sl,tp,"HiLLzX BB Buy",MagicNumber,0,CLR_NONE);
         
      if(ticket>0){break;}
      RefreshRates();
   }

}

void OrderSell() {
double tp;
double sl;

   if (TakeProfit==0){
      tp=priceMiddle;
   } else {
      tp=Bid-TakeProfit*Point;
   }

   if (StopLoss==0){
      sl=0;
   } else {
      sl=Bid+StopLoss*Point;
   }


   int ticket;
   for(int i=0;i<3;i++)
   {
      string errorMsg;
      Print("Order: Sell");
      Print("SL: " + sl);
      Print("TP: " + tp);
      ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,sl,tp,"HiLLzX BB Sell",MagicNumber,0,CLR_NONE);
   
      if(ticket>0){break;}
      RefreshRates();
   }

}


void ModifyTakeProfit(double idTakeProfit)
  {
   bool fmTP;
   fmTP=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),idTakeProfit,0,CLR_NONE);
  }
  
 void SetTP(){
 
   int itotal;
   itotal=OrdersTotal();

   for(int cnt=itotal-1;cnt>=0;cnt--)
   {
      bool tmpselect=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {
         ModifyTakeProfit(priceMiddle);
        }

      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {
         ModifyTakeProfit(priceMiddle);
        }
   }
 }
 

Let us try, do not forget to share and follow our facebook and our youtube channel, happy trading!





- Provided by HiLLzSoft Studio -