Download MetaTrader 5

Is this the best way/correct way to code the Exponential Bollinger Bands??

To add comments, please log in or register
Musngi
299
Musngi  

Is this the best way/correct way to code the Exponential Bollinger Bands?? I want to know if my formula is correct.

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrSilver
#property indicator_color2 clrSilver
#property indicator_color3 clrSilver

extern int Bands_Period = 10;
extern int Line_width = 2;

double upper_band[];
double mid_band[];
double lower_band[];

int init()
{
   SetIndexStyle(0, DRAW_LINE, EMPTY, Line_width);
   SetIndexBuffer(0, mid_band);
   SetIndexLabel(0, "Mid Bands EMA");
  
   SetIndexStyle(1, DRAW_LINE, EMPTY, Line_width);
   SetIndexBuffer(1, upper_band);
   SetIndexLabel(1, "Upper Bands");
  
   SetIndexStyle(2, DRAW_LINE, EMPTY, Line_width);
   SetIndexBuffer(2, lower_band);
   SetIndexLabel(2, "Lower Bands");

   return(INIT_SUCCEEDED);
}

int start()
{
     int limit;
     int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted    
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
  //---- main loop
   for(int i=0; i<limit; i++)
   {    
      double ML = iMA(0, PERIOD_M15, Bands_Period, 0, MODE_EMA, PRICE_CLOSE, i);
      double updev = 2*iStdDev(0, PERIOD_M15, Bands_Period, 0, MODE_EMA, PRICE_CLOSE, i);
      double dndev = 2*iStdDev(0, PERIOD_M15, Bands_Period, 0, MODE_EMA, PRICE_CLOSE, i);
      
      mid_band[i] = ML;
      upper_band[i] = ML + updev;
      lower_band[i] = ML - dndev;
   }
      

   return 0;
}


 

Carl Schreiber
6991
Carl Schreiber  

1) I wouldn't use:

     updev = 2*iStdDev(0, PERIOD_M15,...);

as sometimes it has happend that 2 is reagarded as int and iStdDev is converted accordiungly - to be fail safe write 2.0*iStdDev(..)

2) iStdDev(0,..) is wrong! Either use NULL or better _Symbol: iStdDev(_Symbol,...).

3) You'll be in trouble if the chart is not m15! Either write iStdDev(_Symbol, 0,...) (for the timframe of the chart or you have to calculate the correct value of i using iBarshift(),...

Why don't you search (google) for existing code - faster, no errors, better base to learn to code and the mathematics!

Alain Verleyen
Moderator
32012
Alain Verleyen  
Musngi:

Is this the best way/correct way to code the Exponential Bollinger Bands?? I want to know if my formula is correct.


 

No, it's not, your deviation should be based on ML (EMA) values. You are calculating Standard Deviation of Close prices.

Look at "Bands.mq4" in Mql4\Indicators. It's Bollinger bands mql4 source code and you will just have to modify it (understand it before).

To add comments, please log in or register