Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
RSI Fills
ÿþ//+------------------------------------------------------------------+

//|                                                    RSI 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 "Relative Strength Index Fills"

//--- indicator settings

#property indicator_separate_window

#property indicator_minimum 0

#property indicator_maximum 100

#property indicator_level1  31

#property indicator_level2  50

#property indicator_level3  69

#property indicator_buffers 5

#property indicator_plots   2

//--- plot RSI

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrMagenta

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot Fills

#property indicator_label2  "Fills"

#property indicator_type2   DRAW_FILLING

#property indicator_color2  clrLightBlue,clrBlue

#property indicator_width2  1

//--- input parameters

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

input color    Inp_RSI_Level_Color  = clrDimGray;  // RSI: Color line Levels

input double   Inp_RSI_Level_Down   = 31.0;        // RSI: Value Level Down

input double   Inp_RSI_Level_Middle = 50.0;        // RSI: Value Level Middle

input double   Inp_RSI_Level_Up     = 69.0;        // RSI: Value Level Up

//--- indicator buffers

double   ExtRSIBuffer[];

double   ExtFillsBuffer1[];

double   ExtFillsBuffer2[];

double   ExtPosBuffer[];

double   ExtNegBuffer[];

//--- global variable

int      ExtPeriodRSI;

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//--- check for input

   if(Inp_RSI_ma_period<1)

     {

      ExtPeriodRSI=12;

      Print("Incorrect value for input variable Inp_RSI_ma_period =",Inp_RSI_ma_period,

            "Indicator will use value =",ExtPeriodRSI,"for calculations.");

     }

   else

      ExtPeriodRSI=Inp_RSI_ma_period;

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtRSIBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtFillsBuffer1,INDICATOR_DATA);

   SetIndexBuffer(2,ExtFillsBuffer2,INDICATOR_DATA);

   SetIndexBuffer(3,ExtPosBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtNegBuffer,INDICATOR_CALCULATIONS);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- custom parameters

   IndicatorSetInteger(INDICATOR_LEVELS,3);



   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,Inp_RSI_Level_Color);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,Inp_RSI_Level_Color);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,Inp_RSI_Level_Color);



   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASH);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DOT);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASH);



   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,Inp_RSI_Level_Down);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,Inp_RSI_Level_Middle);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,Inp_RSI_Level_Up);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodRSI);

//--- name for DataWindow and indicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"RSI("+IntegerToString(ExtPeriodRSI)+")");

//--- initialization done

  }

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

//| Relative Strength Index                                          |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

   int    i;

   double diff;

//--- check for rates count

   if(rates_total<=ExtPeriodRSI)

      return(0);

//--- preliminary calculations

   int pos=prev_calculated-1;

   if(pos<=ExtPeriodRSI)

     {

      //--- first RSIPeriod values of the indicator are not calculated

      ExtRSIBuffer[0]=0.0;

      ExtPosBuffer[0]=0.0;

      ExtNegBuffer[0]=0.0;

      double SumP=0.0;

      double SumN=0.0;

      for(i=1; i<=ExtPeriodRSI; i++)

        {

         ExtRSIBuffer[i]=0.0;

         ExtPosBuffer[i]=0.0;

         ExtNegBuffer[i]=0.0;

         diff=price[i]-price[i-1];

         SumP+=(diff>0?diff:0);

         SumN+=(diff<0?-diff:0);

        }

      //--- calculate first visible value

      ExtPosBuffer[ExtPeriodRSI]=SumP/ExtPeriodRSI;

      ExtNegBuffer[ExtPeriodRSI]=SumN/ExtPeriodRSI;

      if(ExtNegBuffer[ExtPeriodRSI]!=0.0)

         ExtRSIBuffer[ExtPeriodRSI]=100.0-(100.0/(1.0+ExtPosBuffer[ExtPeriodRSI]/ExtNegBuffer[ExtPeriodRSI]));

      else

        {

         if(ExtPosBuffer[ExtPeriodRSI]!=0.0)

            ExtRSIBuffer[ExtPeriodRSI]=100.0;

         else

            ExtRSIBuffer[ExtPeriodRSI]=50.0;

        }

      //--- prepare the position value for main calculation

      pos=ExtPeriodRSI+1;

     }

//--- the main loop of calculations

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

     {

      diff=price[i]-price[i-1];

      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(ExtPeriodRSI-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI;

      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI;

      if(ExtNegBuffer[i]!=0.0)

         ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);

      else

        {

         if(ExtPosBuffer[i]!=0.0)

            ExtRSIBuffer[i]=100.0;

         else

            ExtRSIBuffer[i]=50.0;

        }

      //--- fills

      ExtFillsBuffer1[i]=Inp_RSI_Level_Up;

      ExtFillsBuffer2[i]=Inp_RSI_Level_Down;

     }

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

   return(rates_total);

  }

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

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