BB RSI Signal

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Bollinger bands indicatorRelative strength index
0 Views
0 Downloads
0 Favorites
BB RSI Signal
ÿþ//+------------------------------------------------------------------+

//|                                                BB RSI Signal.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.001"

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   2

//--- plot BB RSI Signal Up

#property indicator_label1  "BB RSI Signal Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot BB RSI Signal Down

#property indicator_label2  "BB RSI Signal Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input group             "Signals"

input uchar                Inp_Code_Up             = 116;         // Up symbol code from the Wingdings font

input uchar                Inp_Code_Down           = 116;         // Down symbol code from the Wingdings font

input int                  Inp_Shift               = 5;           // Vertical shift of arrows in pixels

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 group             "RSI"

input int                  Inp_RSI_ma_period       = 7;           // RSI: averaging period

input ENUM_APPLIED_PRICE   Inp_RSI_applied_price   = PRICE_CLOSE; // RSI: type of price

input double               Inp_RSI_Level_Down      = 30.0;        // RSI: level Down

input double               Inp_RSI_Level_Up        = 70.0;        // RSI: level Up

//--- indicator buffers

double   BB_RSI_Signal_Up_Buffer[];

double   BB_RSI_Signal_Down_Buffer[];

double   Upper_Buffer[];

double   Lower_Buffer[];

double   Middle_Buffer[];

double   RSI_Buffer[];

//---

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

int      handle_iRSI;         // variable for storing the handle of the iRSI 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,BB_RSI_Signal_Up_Buffer,INDICATOR_DATA);

   SetIndexBuffer(1,BB_RSI_Signal_Down_Buffer,INDICATOR_DATA);

   SetIndexBuffer(2,Upper_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,Lower_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,Middle_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,RSI_Buffer,INDICATOR_CALCULATIONS);

//--- define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_Code_Up);

   PlotIndexSetInteger(1,PLOT_ARROW,Inp_Code_Down);

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-Inp_Shift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,Inp_Shift);

//--- set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,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);

     }

//--- create handle of the indicator iRSI

   handle_iRSI=iRSI(Symbol(),Period(),Inp_RSI_ma_period,Inp_RSI_applied_price);

//--- if the handle is not created

   if(handle_iRSI==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iRSI 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<3)

      return(0);

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

   int values_to_copy;

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

   int calculated_bb=BarsCalculated(handle_iBands);

   if(calculated_bb<=0)

     {

      PrintFormat("BarsCalculated(Bollinger Bands) returned %d, error code %d",calculated_bb,GetLastError());

      return(0);

     }

   int calculated_rsi=BarsCalculated(handle_iRSI);

   if(calculated_rsi<=0)

     {

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

      return(0);

     }

   if(calculated_bb!=calculated_rsi)

     {

      PrintFormat("BarsCalculated(Bollinger Bands) returned %d, BarsCalculated(RSI) returned %d",calculated_bb,calculated_rsi);

      return(0);

     }

   int calculated=calculated_bb;

//--- 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(Middle_Buffer,Upper_Buffer,Lower_Buffer,Inp_Bands_bands_shift,handle_iBands,values_to_copy))

      return(0);

//--- fill the array with values of the iRSI indicator

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

   if(!FillArrayFromBuffer(RSI_Buffer,handle_iRSI,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)

     {

      BB_RSI_Signal_Up_Buffer[0]=0.0;

      BB_RSI_Signal_Down_Buffer[0]=0.0;

      limit=1;

     }

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

     {

      BB_RSI_Signal_Up_Buffer[i]=0.0;

      BB_RSI_Signal_Down_Buffer[i]=0.0;

      BB_RSI_Signal_Up_Buffer[i-1]=0.0;

      BB_RSI_Signal_Down_Buffer[i-1]=0.0;

      if(open[i-1]>Upper_Buffer[i-1])

         if(RSI_Buffer[i-1]>Inp_RSI_Level_Up)

            BB_RSI_Signal_Up_Buffer[i-1]=high[i-1];

      if(open[i-1]<Lower_Buffer[i-1])

         if(RSI_Buffer[i-1]<Inp_RSI_Level_Down)

            BB_RSI_Signal_Down_Buffer[i-1]=low[i-1];

     }

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

  }

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

//| Filling indicator buffers from the iRSI indicator                |

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

bool FillArrayFromBuffer(double &rsi_buffer[],  // indicator buffer of Relative Strength Index values

                         int ind_handle,        // handle of the iRSI indicator

                         int amount             // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,0,amount,rsi_buffer)<0)

     {

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

      PrintFormat("Failed to copy data from the iRSI 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);

   if(handle_iRSI!=INVALID_HANDLE)

      IndicatorRelease(handle_iRSI);

  }

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

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