Bollinger Bands Fills Color N bars

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Bollinger bands indicator
0 Views
0 Downloads
0 Favorites
Bollinger Bands Fills Color N bars
ÿþ//+------------------------------------------------------------------+

//|                           Bollinger Bands Fills Color N bars.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 version   "1.000"

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_plots   4

//--- plot Bands_upper

#property indicator_label1  "Bands upper"

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_color1  clrBlue,clrYellowGreen,clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot Bands_middle

#property indicator_label2  "Bands middle"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrLightBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Bands_lower

#property indicator_label3  "Bands lower"

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_color3  clrBlue,clrYellowGreen,clrRed

#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_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input group             "Bands"

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

input ENUM_APPLIED_PRICE   Inp_Bands_applied_price = PRICE_CLOSE; // Bands: type of price

input uchar                Inp_Bands_trend_n_bars  = 3;           // Bands: trend N Bars

//--- indicator buffers

double   Bands_upperBuffer[];

double   Bands_upperColors[];

double   Bands_middleBuffer[];

double   Bands_lowerBuffer[];

double   Bands_lowerColors[];

double   FillsBuffer1[];

double   FillsBuffer2[];



double   UpperBuffer[];

double   LowerBuffer[];

//---

int   handle_iBands;          // variable for storing the handle of the iBands indicator

int   bars_calculated=0;      // we will keep the number of values in the Bollinger Bands indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,Bands_upperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,Bands_upperColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,Bands_middleBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,Bands_lowerBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,Bands_lowerColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(5,FillsBuffer1,INDICATOR_DATA);

   SetIndexBuffer(6,FillsBuffer2,INDICATOR_DATA);

   SetIndexBuffer(7,UpperBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,LowerBuffer,INDICATOR_CALCULATIONS);

//--- create handle of the indicator iBands

   handle_iBands=iBands(Symbol(),Period(),Inp_Bands_bands_period,

                        Inp_Bands_bands_shift,Inp_Bands_deviation,Inp_Bands_applied_price);

//--- if the handle is not created

   if(handle_iBands==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iBands indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   if(rates_total<=Inp_Bands_bands_period+Inp_Bands_trend_n_bars)

      return(0);

//--- number of values copied from the iBands indicator

   int values_to_copy;

//--- determine the number of values calculated in the indicator

   int calculated=BarsCalculated(handle_iBands);

   if(calculated<=0)

     {

      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());

      return(0);

     }

//--- if it is the first start of calculation of the indicator or if the number of values in the iBands indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the size of indicator buffers is greater than the number of values in the iBands indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the array with values of the Bollinger Bands indicator

//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation

   if(!FillArraysFromBuffers(Bands_middleBuffer,UpperBuffer,LowerBuffer,Inp_Bands_bands_shift,handle_iBands,values_to_copy))

      return(0);

//--- memorize the number of values in the Bollinger Bands indicator

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=Inp_Bands_trend_n_bars;

   for(int i=limit; i<rates_total; i++)

     {

      int trend_upper=0;

      int trend_lower=0;

      for(int j=i-Inp_Bands_trend_n_bars; j<i; j++)

        {

         if(UpperBuffer[j]<UpperBuffer[j+1])

            trend_upper++;

         else

            trend_upper--;

         //---

         if(LowerBuffer[j]<LowerBuffer[j+1])

            trend_lower++;

         else

            trend_lower--;

        }

      /*

      #property indicator_color3  clrBlue,clrYellowGreen,clrRed

      */

      Bands_upperColors[i]=1.0;

      Bands_lowerColors[i]=1.0;

      if(trend_upper==Inp_Bands_trend_n_bars)

         Bands_upperColors[i]=0.0;

      else

         if(-trend_upper==Inp_Bands_trend_n_bars)

            Bands_upperColors[i]=2.0;

      if(trend_lower==Inp_Bands_trend_n_bars)

         Bands_lowerColors[i]=0.0;

      else

         if(-trend_lower==Inp_Bands_trend_n_bars)

            Bands_lowerColors[i]=2.0;

      Bands_upperBuffer[i]=UpperBuffer[i];

      Bands_lowerBuffer[i]=LowerBuffer[i];

           //--- fills

      FillsBuffer1[i]=UpperBuffer[i];

      FillsBuffer2[i]=LowerBuffer[i];

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| Filling indicator buffers from the iBands indicator              |

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

bool FillArraysFromBuffers(double &base_values[],     // indicator buffer of the middle line of Bollinger Bands

                           double &upper_values[],    // indicator buffer of the upper border

                           double &lower_values[],    // indicator buffer of the lower border

                           int shift,                 // shift

                           int ind_handle,            // handle of the iBands indicator

                           int amount                 // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the MiddleBuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,-shift,amount,base_values)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- fill a part of the UpperBuffer array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,1,-shift,amount,upper_values)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- fill a part of the LowerBuffer array with values from the indicator buffer that has index 2

   if(CopyBuffer(ind_handle,2,-shift,amount,lower_values)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iBands!=INVALID_HANDLE)

      IndicatorRelease(handle_iBands);

  }

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

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 ---