Bollinger Bands SMA

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Bollinger Bands SMA
ÿþ//+------------------------------------------------------------------+

//|                                          Bollinger Bands SMA.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "1.000"

#property description "Bollinger Bands SMA"

#include <MovingAverages.mqh>

//---

#property indicator_chart_window

#property indicator_buffers 7

#property indicator_plots   3

//--- Bands Middle SMA

#property indicator_label1  "Bands Middle SMA"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreenYellow

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- Bands Upper SMA

#property indicator_label2  "Bands Upper SMA"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrGreenYellow

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- Bands Lower SMA

#property indicator_label3  "Bands Lower SMA"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrGreenYellow

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parametrs

input int                  Inp_Bands_bands_period  = 20;          // Bands: period for average line calculation

input int                  Inp_Bands_bands_shift   = 0;           // Bands: horizontal shift of the indicator

input double               Inp_Bands_deviation     = 2.0;         // Bands: number of standard deviations

//--- global variables

int      ExtBandsPeriod,ExtBandsShift;

double   ExtBandsDeviations;

int      ExtPlotBegin=0;

//--- indicator buffer

double   MiddleSMABuffer[];

double   UpperSMABuffer[];

double   LowerSMABuffer[];

double   ExtMLBuffer[];

double   ExtTLBuffer[];

double   ExtBLBuffer[];

double   ExtStdDevBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- check for input values

   if(Inp_Bands_bands_period<2)

     {

      ExtBandsPeriod=20;

      PrintFormat("Incorrect value for input variable Inp_Bands_bands_period=%d. Indicator will use value=%d for calculations.",Inp_Bands_bands_period,ExtBandsPeriod);

     }

   else

      ExtBandsPeriod=Inp_Bands_bands_period;

   if(Inp_Bands_bands_shift<0)

     {

      ExtBandsShift=0;

      PrintFormat("Incorrect value for input variable Inp_Bands_bands_shift=%d. Indicator will use value=%d for calculations.",Inp_Bands_bands_shift,ExtBandsShift);

     }

   else

      ExtBandsShift=Inp_Bands_bands_shift;

   if(Inp_Bands_deviation==0.0)

     {

      ExtBandsDeviations=2.0;

      PrintFormat("Incorrect value for input variable Inp_Bands_deviation=%f. Indicator will use value=%f for calculations.",Inp_Bands_deviation,ExtBandsDeviations);

     }

   else

      ExtBandsDeviations=Inp_Bands_deviation;

//--- define buffers

   SetIndexBuffer(0,MiddleSMABuffer,INDICATOR_DATA);

   SetIndexBuffer(1,UpperSMABuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LowerSMABuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtMLBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtTLBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,ExtBLBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,ExtStdDevBuffer,INDICATOR_CALCULATIONS);

//--- set index labels

   PlotIndexSetString(0,PLOT_LABEL,"Bands SMA("+IntegerToString(ExtBandsPeriod)+") Middle");

   PlotIndexSetString(1,PLOT_LABEL,"Bands SMA("+IntegerToString(ExtBandsPeriod)+") Upper");

   PlotIndexSetString(2,PLOT_LABEL,"Bands SMA("+IntegerToString(ExtBandsPeriod)+") Lower");

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,"Bollinger Bands SMA");

//--- indexes draw begin settings

   ExtPlotBegin=ExtBandsPeriod-1;

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBandsPeriod);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBandsPeriod);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtBandsPeriod);

//--- indexes shift settings

   PlotIndexSetInteger(0,PLOT_SHIFT,ExtBandsShift);

   PlotIndexSetInteger(1,PLOT_SHIFT,ExtBandsShift);

   PlotIndexSetInteger(2,PLOT_SHIFT,ExtBandsShift);

//--- number of digits of indicator value

   IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1);

  }

//+------------------------------------------------------------------+

//| Bollinger Bands                                                  |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

   if(rates_total<ExtPlotBegin)

      return(0);

//--- indexes draw begin settings, when we've recieved previous begin

   if(ExtPlotBegin!=ExtBandsPeriod+begin)

     {

      ExtPlotBegin=ExtBandsPeriod+begin;

      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPlotBegin);

      PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPlotBegin);

      PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtPlotBegin);

     }

//--- starting calculation

   int pos;

   if(prev_calculated>1)

      pos=prev_calculated-1;

   else

      pos=0;

//--- main cycle

   for(int i=pos; i<rates_total && !IsStopped(); i++)

     {

      //--- middle line

      ExtMLBuffer[i]=SimpleMA(i,ExtBandsPeriod,price);

      //--- calculate and write down StdDev

      ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod);

      //--- upper line

      ExtTLBuffer[i]=ExtMLBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i];

      //--- lower line

      ExtBLBuffer[i]=ExtMLBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[i];

      //--- Middle SMA Buffer

      MiddleSMABuffer[i]=SimpleMA(i,3,ExtMLBuffer);

      //--- Upper SMA Buffer

      UpperSMABuffer[i]=SimpleMA(i,3,ExtTLBuffer);

      //--- Lower SMA Buffer

      LowerSMABuffer[i]=SimpleMA(i,3,ExtBLBuffer);

     }

//--- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Calculate Standard Deviation                                     |

//+------------------------------------------------------------------+

double StdDev_Func(const int position,const double &price[],const double &ma_price[],const int period)

  {

   double std_dev=0.0;

//--- calcualte StdDev

   if(position>=period)

     {

      for(int i=0; i<period; i++)

         std_dev+=MathPow(price[position-i]-ma_price[position],2.0);

      std_dev=MathSqrt(std_dev/period);

     }

//--- return calculated value

   return(std_dev);

  }

//+------------------------------------------------------------------+

Comments