Bollinger Bands Fills

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

//|                                        Bollinger Bands Fills.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property description "Bollinger Bands Fills"

#include <MovingAverages.mqh>

//---

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   4

//--- plot Bands

#property indicator_label1  "Bands upper"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrMagenta

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//---

#property indicator_label2  "Bands middle"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrLightBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//---

#property indicator_label3  "Bands lower"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrMagenta

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- plot Fills

#property indicator_label4  "Fills"

#property indicator_type4   DRAW_FILLING

#property indicator_color4  clrLightBlue,clrBlue

#property indicator_width4  1

//--- input parametrs

input int      InpBandsPeriod=20;       // Period

input int      InpBandsShift=0;         // Shift

input double   InpBandsDeviations=2.0;  // Deviation

//--- global variables

int      ExtBandsPeriod,ExtBandsShift;

double   ExtBandsDeviations;

int      ExtPlotBegin=0;

//---- indicator buffer

double   ExtUpperBuffer[];

double   ExtMiddleBuffer[];

double   ExtLowerBuffer[];

double   ExtFillsBuffer1[];

double   ExtFillsBuffer2[];

double   ExtStdDevBuffer[];

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//--- check for input values

   if(InpBandsPeriod<2)

     {

      ExtBandsPeriod=20;

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

     }

   else

      ExtBandsPeriod=InpBandsPeriod;

   if(InpBandsShift<0)

     {

      ExtBandsShift=0;

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

     }

   else

      ExtBandsShift=InpBandsShift;

   if(InpBandsDeviations==0.0)

     {

      ExtBandsDeviations=2.0;

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

     }

   else

      ExtBandsDeviations=InpBandsDeviations;

//--- define buffers

   SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtMiddleBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtLowerBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtFillsBuffer1,INDICATOR_DATA);

   SetIndexBuffer(4,ExtFillsBuffer2,INDICATOR_DATA);

   SetIndexBuffer(5,ExtStdDevBuffer,INDICATOR_CALCULATIONS);

//--- set index labels

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

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

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

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,"Bollinger Bands");

//--- 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);

//---- OnInit done

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

//--- variables

   int pos;

//--- 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);

     }

//--- check for bars count

   if(rates_total<ExtPlotBegin)

      return(0);

//--- starting calculation

   if(prev_calculated>1)

      pos=prev_calculated-1;

   else

      pos=0;

//--- main cycle

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

     {

      //--- middle line

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

      //--- calculate and write down StdDev

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

      //--- upper line

      ExtUpperBuffer[i]=ExtMiddleBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i];

      //--- lower line

      ExtLowerBuffer[i]=ExtMiddleBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[i];

      //--- fills

      ExtFillsBuffer1[i]=ExtUpperBuffer[i];

      ExtFillsBuffer2[i]=ExtLowerBuffer[i];

     }

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

   return(rates_total);

  }

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

//| Calculate Standard Deviation                                     |

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

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

  {

//--- variables

   double StdDev_dTmp=0.0;

//--- check for position

   if(position<period)

      return(StdDev_dTmp);

//--- calcualte StdDev

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

      StdDev_dTmp+=MathPow(price[position-i]-MAprice[position],2);

   StdDev_dTmp=MathSqrt(StdDev_dTmp/period);

//--- return calculated value

   return(StdDev_dTmp);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---