Bollinger Bands Cross Arrow

Download script
Author: Copyright © 2020, Vladimir Karputov

This script is a specialized technical analysis tool designed for the MetaTrader 5 platform. It automates the visual identification of specific price actions by combining Bollinger Bands with an automated "arrow" signaling system. Instead of requiring you to manually watch for every price movement, the script calculates these levels in the background and marks important moments directly on your chart.

Core Functionality

The primary purpose of this tool is to detect when the market price "pierces" or crosses the boundaries of the Bollinger Bands. It identifies two specific types of movements:

  • Down to Up Crosses: The script looks for instances where the price moves from below the lower boundary back into the band, or vice versa, depending on your sensitivity settings.
  • Top to Down Crosses: Similarly, it monitors when the price moves from above the upper boundary back down into the band.

Key Features & Customization

The script is designed to be flexible, allowing you to adjust how "sensitive" the detection is through several user-adjustable parameters:

  • Intersection Logic (Strict vs. Soft):
    • Strict Mode: Requires the High or Low price of a candle to specifically break the boundary.
    • Soft Mode: Looks at the Open and Close prices, providing a more generalized view of the movement.
  • Visual Signaling: When a crossover is detected, the script automatically places an arrow on the chart. You can customize the appearance of these arrows (using Wingdings fonts) and adjust their vertical position (shift) so they do not overlap with your candles or other indicators.
  • Bollinger Band Customization: You can define the standard period (how much historical data is averaged), the deviation (how wide the bands are), and which price type (Close, Open, etc.) the calculation should base itself on.

Technical Behavior

For the end-user, the logic flows as follows:

  1. Data Synchronization: The script continuously pulls real-time data from the Bollinger Bands indicator.
  2. Buffer Management: It organizes this data into "buffers"—internal memory slots that keep track of the Upper, Middle, and Lower band values simultaneously.
  3. Real-time Comparison: Every time a new price tick occurs, the script compares the current candle's high/low/open/close against the corresponding band values from the previous candle.
  4. Visual Output: If the mathematical conditions for a "cross" are met, it triggers the appearance of an arrow; otherwise, it keeps the area clear, ensuring your chart remains clean and only shows confirmed signals.
Price Data Components
Indicators Used
Bollinger bands indicator
15 Views
0 Downloads
0 Favorites
Bollinger Bands Cross Arrow
ÿþ//+------------------------------------------------------------------+

//|                                  Bollinger Bands Cross Arrow.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.003"

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

//| Enum Intersection                                                |

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

enum ENUM_iNTERSECTION

  {

   intersection_strict=0,  // Strict (High and Low)

   intersection_soft=1,    // Soft (Open and Close)

  };

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   5

//--- plot BandsUpper

#property indicator_label1  "BandsUpper"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrLightSeaGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot BandsMiddle

#property indicator_label2  "BandsMiddle"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrLightSeaGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot BandsLower

#property indicator_label3  "BandsLower"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrLightSeaGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot BandsCrossDownUp

#property indicator_label4  "BandsCrossDownUp"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrLightSeaGreen

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot BandsCrossTopDown

#property indicator_label5  "BandsCrossTopDown"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrLightSeaGreen

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- input parameters

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

//--- Arrows

input uchar                InpCodeDownUp           = 241;               // Arrow code for 'Cross Down Up' (font Wingdings)

input uchar                InpCodeTopDown          = 242;               // Arrow code for 'Cross Top Down' (font Wingdings)

input int                  InpShift                = 10;                // Vertical shift of arrows in pixel

//---

input ENUM_iNTERSECTION    InpIntersection         = intersection_soft; // Type intersection:

//--- indicator buffers

double   BandsUpperBuffer[];

double   BandsMiddleBuffer[];

double   BandsLowerBuffer[];

double   BandsCrossDownUpBuffer[];

double   BandsCrossTopDownBuffer[];

//---

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,BandsUpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,BandsMiddleBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,BandsLowerBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,BandsCrossDownUpBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,BandsCrossTopDownBuffer,INDICATOR_DATA);

//--- set index labels

   string intersection=(InpIntersection==intersection_strict)?"strict":"soft";

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

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

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

//--- number of digits of indicator value

   IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(3,PLOT_ARROW,InpCodeDownUp);

   PlotIndexSetInteger(4,PLOT_ARROW,InpCodeTopDown);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,InpShift);

   PlotIndexSetInteger(4,PLOT_ARROW_SHIFT,-InpShift);

//--- an empty value for plotting, for which there is no drawing

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);

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

      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(BandsMiddleBuffer,BandsUpperBuffer,BandsLowerBuffer,Inp_Bands_bands_shift,handle_iBands,values_to_copy))

      return(0);

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

   bars_calculated=calculated;

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=Inp_Bands_bands_period;

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

     {

      bool intersection_down_up=(

                                   (InpIntersection==intersection_strict && high[i-1]<BandsLowerBuffer[i-1]) ||

                                   (InpIntersection==intersection_soft && open[i-1]<BandsLowerBuffer[i-1] && close[i-1]<BandsLowerBuffer[i-1])

                                );

      if((low[i]<BandsLowerBuffer[i] && high[i]>BandsLowerBuffer[i]) && (intersection_down_up))

         BandsCrossDownUpBuffer[i]=low[i];

      else

         BandsCrossDownUpBuffer[i]=0.0;





      bool intersection_top_down=(

                                    (InpIntersection==intersection_strict && low[i-1]>BandsUpperBuffer[i-1]) ||

                                    (InpIntersection==intersection_soft && open[i-1]>BandsUpperBuffer[i-1] && close[i-1]>BandsUpperBuffer[i-1])

                                 );

      if((high[i]>BandsUpperBuffer[i] && low[i]<BandsUpperBuffer[i]) && (intersection_top_down))

         BandsCrossTopDownBuffer[i]=high[i];

      else

         BandsCrossTopDownBuffer[i]=0.0;

     }

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

//--- clear the chart after deleting the indicator

   Comment("");

  }

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

Comments