RSI Custom Smoothing_Levels

Author: Copyright 2021, Mitrofanov Nikolay
0 Views
0 Downloads
0 Favorites
RSI Custom Smoothing_Levels
ÿþ//+------------------------------------------------------------------+

//|                                                      ProjectName |

//|                                      Copyright 2020, CompanyName |

//|                                       http://www.companyname.net |

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

#property copyright "Copyright 2021, Mitrofanov Nikolay"

#property link      "https://www.mql5.com"

#property version   "1.00"

//--- indicator settings

#include <MovingAverages.mqh>

#include <Arrays\ArrayDouble.mqh>

#include <Arrays\ArrayLong.mqh>

#property indicator_separate_window

#property indicator_minimum 0

#property indicator_maximum 100

#property indicator_buffers 4

#property indicator_plots   1

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDodgerBlue

#property indicator_width1  5

//--- input parameters

input int               Inp_RSI_Period       = 14;             // rsi period

input double            Inp_RSI_Level_Up     = 70.0;           // level up value

input double            Inp_RSI_Level_Down   = 30.0;           // level down value



sinput group "--- smoothing ---"

enum MA_METHOD

  {

   None,

   Simple,

   Exponential,

   Smoothed,

   LinearWeighed

  };

input MA_METHOD    InpSmoothMethod      = Exponential;       // type

input int          InpSmootPeriod       = 3;              // period



sinput group "--- lines ---"

input int InpCandlesCalculationNumber = 1500; // number of candles for levels calculations

int mInpMinCandlesNumber;

input int InpLevelsNumber = 5; // number of levels

sinput color InpLevelSupColor = clrDeepSkyBlue; // color up

sinput color InpLevelResColor = clrOrange; // color down

sinput ENUM_LINE_STYLE InpLevelStyle = STYLE_SOLID; // style

sinput int InpLevelWidth = 5; // width

string prefix = "dzhigurda_";







CArrayDouble *LevelsRes;

CArrayLong *TimesRes;

CArrayDouble *LevelsSup;

CArrayLong *TimesSup;



static int p;







//--- indicator buffers

double    ExtRSIBufferSmoothing[];

double    ExtRSIBuffer[];

double    ExtPosBuffer[];

double    ExtNegBuffer[];

//--- global variable

int       ExtPeriodRSI;





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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//--- check for input

   if(Inp_RSI_Period<1)

     {

      ExtPeriodRSI=12;

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

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

     }

   else

      ExtPeriodRSI=Inp_RSI_Period;

//--- indicator buffers mapping

   switch(InpSmoothMethod)

     {

      case None:

         SetIndexBuffer(0,ExtRSIBuffer,INDICATOR_DATA);

         SetIndexBuffer(1,ExtRSIBufferSmoothing,INDICATOR_CALCULATIONS);

         break;

      default:

         SetIndexBuffer(0,ExtRSIBufferSmoothing,INDICATOR_DATA);

         SetIndexBuffer(1,ExtRSIBuffer,INDICATOR_CALCULATIONS);

         break;

     }



   SetIndexBuffer(2,ExtPosBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtNegBuffer,INDICATOR_CALCULATIONS);



//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- custom parameters

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,Inp_RSI_Level_Down);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,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

   string text="";



   switch(InpSmoothMethod)

     {

      case Exponential:

         text="EMA("+IntegerToString(InpSmootPeriod)+")";

         break;

      case Smoothed:

         text="SMMA("+IntegerToString(InpSmootPeriod)+")";

         break;

      case LinearWeighed:

         text="LWMA("+IntegerToString(InpSmootPeriod)+")";

         break;

      case Simple:

         text="SMA("+IntegerToString(InpSmootPeriod)+")";

     }



   IndicatorSetString(INDICATOR_SHORTNAME,"RSI Extrenum LVL("+string(ExtPeriodRSI)+") "+text);



   LevelsRes = new CArrayDouble();

   TimesRes = new CArrayLong();



   LevelsSup = new CArrayDouble();

   TimesSup = new CArrayLong();



//--- initialization done

  }

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

//| Relative Strength Index                                          |

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

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[])

  {

   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=close[i]-close[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=close[i]-close[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;

        }

     }



   switch(InpSmoothMethod)

     {

      case Exponential:

         ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,ExtRSIBuffer,ExtRSIBufferSmoothing);

         break;

      case Smoothed:

         SmoothedMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,ExtRSIBuffer,ExtRSIBufferSmoothing); // original

         break;

      case LinearWeighed:

         LinearWeightedMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,ExtRSIBuffer,ExtRSIBufferSmoothing);

         break;

      case Simple:

         SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSmootPeriod,ExtRSIBuffer,ExtRSIBufferSmoothing);

         break;

     }





   p = rates_total - InpCandlesCalculationNumber;

   if(pos <= p)

     {

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

        {

         if(p <= i)

           {

            switch(InpSmoothMethod)

              {

               case None:

                  evalLevels(ExtRSIBuffer, i, high, low, time);

                  break;

               default:

                  evalLevels(ExtRSIBufferSmoothing, i, high, low, time);

                  break;

              }

           }

        }



      for(int a = LevelsSup.Total()-1; a >= 0; a--)

        {

         drawLine(prefix + "_sup_" + string(a), TimesSup.At(a), NormalizeDouble(LevelsSup.At(a), _Digits), InpLevelSupColor, InpLevelWidth, InpLevelStyle);

        }

      for(int a = LevelsRes.Total()-1; a >= 0; a--)

        {

         drawLine(prefix + "_res_" + string(a), TimesRes.At(a), NormalizeDouble(LevelsRes.At(a), _Digits), InpLevelResColor, InpLevelWidth, InpLevelStyle);

        }

     }

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

   return(rates_total);

  }

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







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

//|                                                                  |

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

void evalLevels(double &sourceData[], int indexValue, const double &high[], const double &low[], const datetime &time[])

  {

   if(sourceData[indexValue-1] > Inp_RSI_Level_Up)

     {

      int index = TimesRes.Total()-1;

      datetime lastTime = (datetime)TimesRes.At(index);

      if(lastTime != time[indexValue-2])

        {

         if(LevelsRes.Total() < InpLevelsNumber)

           {

            LevelsRes.Add(high[indexValue-1]);

            TimesRes.Add(time[indexValue-1]);

           }

         else

           {

            LevelsRes.Delete(0);

            LevelsRes.Add(high[indexValue-1]);

            TimesRes.Delete(0);

            TimesRes.Add(time[indexValue-1]);

           }

        }

      else

        {

         if(high[indexValue-1] > LevelsRes.At(index))

           {

            LevelsRes.Update(index, high[indexValue-1]);

            TimesRes.Update(index, time[indexValue-1]);

           }

         else

           {

            TimesRes.Update(index, time[indexValue-1]);

           }

        }

     }

   else

      if(sourceData[indexValue-1] < Inp_RSI_Level_Down)

        {

         int index = TimesSup.Total()-1;

         datetime lastTime = (datetime)TimesSup.At(index);

         if(lastTime != time[indexValue-2])

           {

            if(LevelsSup.Total() < InpLevelsNumber)

              {

               LevelsSup.Add(low[indexValue-1]);

               TimesSup.Add(time[indexValue-1]);

              }

            else

              {

               LevelsSup.Delete(0);

               LevelsSup.Add(low[indexValue-1]);

               TimesSup.Delete(0);

               TimesSup.Add(time[indexValue-1]);

              }

           }

         else

           {

            if(low[indexValue-1] < LevelsSup.At(index))

              {

               LevelsSup.Update(index, low[indexValue-1]);

               TimesSup.Update(index, time[indexValue-1]);

              }

            else

              {

               TimesSup.Update(index, time[indexValue-1]);

              }

           }



        }

  }





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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {



   ObjectsDeleteAll(0, prefix, 0, OBJ_TREND);

   ChartRedraw(0);





   delete LevelsRes;

   delete TimesRes;

   delete LevelsSup;

   delete TimesSup;



  }





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

//|                                                                  |

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

void drawLine(string lineName, datetime startTime, double price, color lineColor = clrWhite, int lineWidth = 3, ENUM_LINE_STYLE lineStyle = STYLE_SOLID)

  {

   if(!isObjectExist(lineName))

     {

      ObjectCreate(0, lineName, OBJ_TREND, 0, startTime, price, startTime + PeriodSeconds(PERIOD_MN1), price);

      ObjectSetInteger(0,lineName,OBJPROP_COLOR, lineColor);

      ObjectSetInteger(0,lineName,OBJPROP_WIDTH, lineWidth);

      ObjectSetInteger(0,lineName,OBJPROP_STYLE, lineStyle);

      ObjectSetInteger(0,lineName,OBJPROP_SELECTABLE, true);

      ObjectSetInteger(0,lineName,OBJPROP_SELECTED, false);

      ObjectSetInteger(0,lineName,OBJPROP_RAY_RIGHT, true);

     }

   else

     {

      ObjectMove(0, lineName, 0, startTime, price);

      ObjectMove(0, lineName, 1, startTime  + PeriodSeconds(PERIOD_MN1), price);

     }



   ChartRedraw(0);

  }



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

//|                                                                  |

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

bool isObjectExist(string objName)

  {

   return (ObjectFind(0, objName) != -1);

  }

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

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