BollingerBands TrailingStop

 

Hello Gnetelmen,

Bollinger Bands trailing stop coding

 Need someone help me coding this trailing stop

I am trying to use Bollinger bands as a "trailing stop".
 

if I am in a long position and the close price touched the Upper band, I want to exit the position when it comes down to the middle band.
 

I had tried many ways just could code it…

Please see my code below.

What i think is a way to store the value of the upper band . Anyone here know how to do this ?

Much appreciated in advance,

What’s wrong with my code? It always close at the Upper band?

     if(!ts)

         {

         // Signal_Bands Calling

         dBandUB5m = iCustom(Symbol(), PERIOD_M5, "Signal_Bands", 20, 2, 3, 0);

         dBandMB5m = iCustom(Symbol(), PERIOD_M5, "Signal_Bands", 20, 2, 0, 0);

         dBandLB5m = iCustom(Symbol(), PERIOD_M5, "Signal_Bands", 20, 2, 4, 0);

 

if (dBandUB5m>0  &&  dBandMB5m>0 )

            {

               ts = true;

            }

         }

         if(ts && Bid <= iHigh(Symbol(), PERIOD_M5, 0) - BufferS*Point)

         {

            CloseAllLongs();

         }

      }
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
sophiagao:

Hello Gnetelmen,

Bollinger Bands trailing stop coding

 Need someone help me coding this trailing stop

I am trying to use Bollinger bands as a "trailing stop".
 

if I am in a long position and the close price touched the Upper band, I want to exit the position when it comes down to the middle band.
 

I had tried many ways just could code it…

Please see my code below.

What i think is a way to store the value of the upper band . Anyone here know how to do this ?

Much appreciated in advance,

What’s wrong with my code? It always close at the Upper band?

  • What is the indicator "Signal_Bands" ?
  • Where is it checked that "if I am in a long position and the close price touched the Upper band, I want to exit the position when it comes down to the middle band. " ?
 
Alain Verleyen:
  • What is the indicator "Signal_Bands" ?
  • Where is it checked that "if I am in a long position and the close price touched the Upper band, I want to exit the position when it comes down to the middle band. " ?

thank you very much, Alain.

please find attached code

 
sophiagao:

thank you very much, Alain.

please find attached code

Nothing attached.
 
Alain Verleyen:
Nothing attached.

I did follow your instruction to insert the code. how come it did not show?

Let me do it again, click the SRC ... it's here. I can see it. then click the Add your comment?

//+------------------------------------------------------------------+
//|                                                  Signal_Bands.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"


//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 7
#property  indicator_color1  White
#property  indicator_color2  SlateBlue
#property  indicator_color3  SlateBlue
#property  indicator_color4  Crimson
#property  indicator_width4   3
#property  indicator_color5  SteelBlue
#property  indicator_width5   3
#property  indicator_color6  Crimson
#property  indicator_width6   3
#property  indicator_color7  SteelBlue
#property  indicator_width7   3


//---- indicator parameters
extern int BobaPeriod= 20;
extern int BobaDeviations= 2;


//---- indicator buffers
double     BufferBand1[];
double     BufferBand0[];
double     BufferBand2[];
double     BufferSell[];
double     BufferBuy[];
double     BufferSellEntry[];
double     BufferBuyEntry[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   //---- 2 additional buffers are used for counting.
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
   
   // three bollies
   SetIndexBuffer(0,BufferBand0);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,BobaPeriod);

   SetIndexBuffer(1,BufferBand1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,BobaPeriod);
   
   SetIndexBuffer(2,BufferBand2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexDrawBegin(2,BobaPeriod);
   
   // marks for gimmees
   SetIndexBuffer(3,BufferSell);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexDrawBegin(3,BobaPeriod);
   SetIndexArrow(3, 238);
   SetIndexEmptyValue(3, 0);

   SetIndexBuffer(4,BufferBuy);  
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexDrawBegin(4,BobaPeriod);
   SetIndexArrow(4, 236);
   SetIndexEmptyValue(4, 0);
   
   // marks for entries
   SetIndexBuffer(5,BufferSellEntry);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexDrawBegin(5,BobaPeriod);
   SetIndexArrow(5, 167);
   SetIndexEmptyValue(5, 0);

   SetIndexBuffer(6,BufferBuyEntry);  
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexDrawBegin(6,BobaPeriod);
   SetIndexArrow(6, 167);
   SetIndexEmptyValue(6, 0);

   
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars= IndicatorCounted(),
         lastbar;
     
   if (counted_bars>0)
      counted_bars--;
      
   lastbar= Bars - counted_bars;
   
   Signal_Bands(0, lastbar, BufferSell, BufferBuy, BufferSellEntry, BufferBuyEntry, BufferBand1, BufferBand0, BufferBand2, BobaPeriod, BobaDeviations);

   return (0);
}   

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Signal_Bands(int offset, int lastbar, double &sellbuf[], double &buybuf[], double &sellbuf2[], double &buybuf2[], 
                  double &band1buf[], double &band0buf[], double &band2buf[], int period, int deviation)
{
   double band1, band0, band2;
   int markerdist= 15;   
   
   lastbar= MathMin(Bars-period, lastbar);   

   for(int i= lastbar; i>=offset; i--)
   {
   
      sellbuf[i]= 0;
      buybuf[i]=0;
      sellbuf2[i]= 0;
      buybuf2[i]=0;

      band0=iBands(NULL,0, period, deviation, 0,PRICE_CLOSE,MODE_MAIN,i);
      band1=iBands(NULL,0, period, deviation, 0,PRICE_CLOSE,MODE_UPPER,i); 
      band2=iBands(NULL,0, period, deviation, 0,PRICE_CLOSE,MODE_LOWER,i);

      band0buf[i]= band0;
      band1buf[i]= band1;
      band2buf[i]= band2;

        if (Open[i]>band0 && High[i]>band0 && Low[i]<band0 && Close[i]<band0)  
        {
         sellbuf2[i]= High[i] + markerdist*Point;        
        }
        else
        if (High[i]>=band1)  
        {
         sellbuf[i]= High[i] + markerdist*Point;        
        }
        
        if (Open[i]<band0 && High[i]>band0 && Low[i]<band0 && Close[i]>band0)  
        {
         buybuf2[i]= Low[i] - markerdist*Point;
        }
        else
        if (Low[i]<=band2)  
        {
         buybuf[i]= Low[i] - markerdist*Point;
        }

     }

   return(0);
  }

//+------------------------------------------------------------------+
 
Alain Verleyen:
  • What is the indicator "Signal_Bands" ?
  • Where is it checked that "if I am in a long position and the close price touched the Upper band, I want to exit the position when it comes down to the middle band. " ?
Please answer my question if you want help.
 
Alain Verleyen:
Please answer my question if you want help.

it will close my long position as below.

if (dBandUB5m>0)

            {

               ts = true;

            }

         }

         if(ts && Bid <= iHigh(Symbol(), PERIOD_M5, 0) - BufferS*Point)

         {

            CloseAllLongs();

         }
      }
 
sophiagao:

it will close my long position as below.

Where is it checked that "the close price touched the Upper band, I want to exit the position when it comes down to the middle band. " ?
 
Alain Verleyen:
Where is it checked that "the close price touched the Upper band, I want to exit the position when it comes down to the middle band. " ?

Hello Alain,

Not sure where checked means, since I am not a programmer.

My intention was close  long positon if the two conditons true. The first condition is when the price touched the upper band, second conditon is the price come down to the middle band after the close price touched the upper band,

then close the long positons.

Thank you so much for your time.