Dynamic RSI gr

Author: Copyright 2023, Roman Kiverin
Indicators Used
Relative strength indexMoving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
Dynamic RSI gr
ÿþ//+------------------------------------------------------------------+

//|                                                  Dynamic RSI.mq4 |

//|          https://www.mql5.com/ru/users/romankiverin/publications |

//|                                         https://t.me/RomanKiverin|

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

#property version     "1.0"

#property copyright   "Copyright 2023, Roman Kiverin"

#property link        "https://www.mql5.com/ru/users/romankiverin/publications"

#property strict



#property description "Dynamic RSI\n https://t.me/RomanKiverin"

#property indicator_separate_window



#property indicator_buffers 6



// --- settings

input double   DZbuy             =  0.1;                 // Buyers Dynamic Zone Probability

input double   DZsell            =  0.1;                 // Sellers Dynamic Zone Probability

input int      PeriodRSI         =  14;                  // Period RSI

input int      LookBack          =  60;                  // Look Back PeriodRSI

input color    MaxColor          =  clrDarkOrchid;       // Sell Signall Color

input color    MinColor          =  clrDodgerBlue;       // Buy Signall Color

input color    UpColor           =  clrDarkGray;         // Up Line Color

input color    MiddleColor       =  clrGold;             // Middle Line Color

input color    DnColor           =  clrDarkGray;         // Down Line Color

input color    RSIColor          =  clrDarkSeaGreen;     // RSI Line Color

input int      Width             =  2;                   // Width Line

input bool     DrawGraphic       =  true;                // Draw Graphic?

input int      SizeArrow         =  15;                  // Size Arrow (5 .. 50)

input color    UpArrowColor      =  clrDarkOrchid;       // Color Up Arrow

input color    DnArrowColor      =  clrDodgerBlue;       // Color Down Arrow

input uchar    Opaque            =  80;                  // Opaque Arrow

input int      Offset            =  10;                  // Up and Down Offset from Arrow

input string   ID                =  "RSI ID ";           // ID Name



//--- indicator buffers

double         UpLineBuffer[];

double         DnLineBuffer[];

double         RSILineBuffer[];

double         RSIValueBuffer[];

double         MaxLineBuffer[];

double         MinLineBuffer[];

double         WMALineBuffer[];

double         DeltaBuffer[];

double         UpBuffer[];

double         DnBuffer[];

double         LineBuffer[];



uint UpDrawBuffer[], DnDrawBuffer[];

int XDraw, YDraw;

string UpResourceName, DnResourceName;

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

//| Custom indicator initialization function                         |

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

#define  DrawArrow(Name, Rnm, Buff, time, prc, Anchor)                                          \

         if(DrawGraphic)                                                                        \

            if(ObjectFind(Name) < 0)                                                            \

            {                                                                                   \

               ObjectCreate(0, Name, OBJ_BITMAP, 0, 0, 0);                                      \

               ObjectSetInteger(0, Name, OBJPROP_TIME, 0, time);                                \

               ObjectSetDouble(0, Name, OBJPROP_PRICE, 0, prc);                                 \

               ObjectSetInteger(0, Name, OBJPROP_ANCHOR, Anchor);                               \

               ObjectSetInteger(0, Name, OBJPROP_SELECTABLE, false);                            \

               ObjectSetString(0, Name, OBJPROP_BMPFILE, Rnm);                                  \

               ResourceCreate(Rnm, Buff, XDraw, YDraw, 0, 0, 0, COLOR_FORMAT_ARGB_NORMALIZE);   \

           }                                                                                    \

            else                                                                                \

               if(ObjectGetDouble(0, Name, OBJPROP_PRICE) != prc)                               \

                  ObjectMove(Name, 0 , time, prc);



#define  GetName(i)  ID + TimeToString(Time[i])



#define  UpArrow(i) DrawArrow(GetName(i), UpResourceName, UpDrawBuffer, Time[i], High[i], ANCHOR_LOWER)

#define  DnArrow(i) DrawArrow(GetName(i), DnResourceName, DnDrawBuffer, Time[i], Low[i], ANCHOR_UPPER)

#define  Delete(i) if(DrawGraphic) ObjectDelete(0,GetName(i))



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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(0, ID, 0);

  }

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

//|                                                                  |

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

int OnInit()

  {

//--- indicator buffers mapping

   if(DrawGraphic)

     {

      UpResourceName = "::Arrow Up";

      DnResourceName = "::Arrow Down";

      XDraw = SizeArrow * 2 + 1;

      YDraw = SizeArrow * 2 + Offset;

      ArrayResize(UpDrawBuffer, XDraw * YDraw);

      ArrayResize(DnDrawBuffer, XDraw * YDraw);

      ArrayInitialize(UpDrawBuffer, ColorToARGB(clrNONE, 0));

      ArrayInitialize(DnDrawBuffer, ColorToARGB(clrNONE, 0));

      

      double dOpa = double(0.55 * Opaque) / XDraw;



      for(int i = 0; i < XDraw; i ++)

         for(int j = 0; j < XDraw; j++)

            if((j < SizeArrow && ((i > SizeArrow - j && i <= SizeArrow)||(i - SizeArrow < j && i > SizeArrow)))||

               ((j >= SizeArrow && (i >= SizeArrow / 1.5 && i < XDraw - SizeArrow / 1.5))

                && !(i - 3 * SizeArrow / 4 >= XDraw - j && i <= SizeArrow)

                && !(i -  SizeArrow < j - 7 * SizeArrow / 4 && i > SizeArrow)))

              {

               UpDrawBuffer[i + (XDraw - j) * XDraw] =

                  ColorToARGB(UpArrowColor,uchar(Opaque - dOpa * (j + 2.0 * MathAbs(SizeArrow - i))));

               DnDrawBuffer[i + (j + Offset - 1) * XDraw] =

                  ColorToARGB(DnArrowColor,uchar(Opaque - dOpa * (j + 2.0 * MathAbs(SizeArrow - i))));                  

              }

     }



   IndicatorBuffers(11);

   

   for(int i = 0; i < 11; i ++)

      SetIndexDrawBegin(i,LookBack+PeriodRSI);



   SetIndexBuffer(0, UpLineBuffer);

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,Width,UpColor);



   SetIndexBuffer(1, DnLineBuffer);

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,Width,DnColor);



   SetIndexBuffer(2, WMALineBuffer);

   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,Width,MiddleColor);



   SetIndexBuffer(3, RSILineBuffer);

   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,Width,RSIColor);

   SetIndexEmptyValue(3,0);



   SetIndexBuffer(4, MaxLineBuffer);

   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,Width,MaxColor);

   SetIndexEmptyValue(4,0);



   SetIndexBuffer(5,MinLineBuffer);

   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,Width,MinColor);

   SetIndexEmptyValue(5,0);



   SetIndexBuffer(6, RSIValueBuffer);

   SetIndexStyle(6, DRAW_NONE);



   SetIndexBuffer(7, DeltaBuffer);

   SetIndexStyle(7, DRAW_NONE);



   SetIndexBuffer(8, UpBuffer);

   SetIndexStyle(8, DRAW_NONE);



   SetIndexBuffer(9, DnBuffer);

   SetIndexStyle(9, DRAW_NONE);



   SetIndexBuffer(10, LineBuffer);

   SetIndexStyle(10, DRAW_NONE);

//---

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

  {

//---

   for(int i = MathMin(rates_total - prev_calculated, Bars - LookBack - 5); i >= 0; i --)

     {

      RSIValueBuffer[i] = iRSI(NULL, 0, PeriodRSI, PRICE_CLOSE, i);



      UpBuffer[i]=RSIValueBuffer[ArrayMaximum(RSIValueBuffer,LookBack,i)];

      DnBuffer[i]=RSIValueBuffer[ArrayMinimum(RSIValueBuffer,LookBack,i)];

      DeltaBuffer[i]=(UpBuffer[i]-DnBuffer[i]) / 2.0;

      WMALineBuffer[i]=iMAOnArray(DeltaBuffer,0,PeriodRSI,0,MODE_LWMA,i) + iMAOnArray(DnBuffer,0,PeriodRSI,0,MODE_LWMA,i);

      UpLineBuffer[i]=UpBuffer[i] - WMALineBuffer[i] * DZbuy;

      DnLineBuffer[i]=DnBuffer[i] + WMALineBuffer[i] * DZsell;

      LineBuffer[i]=(4 * RSIValueBuffer[i] + 3 * RSIValueBuffer[i+1] + 2 * RSIValueBuffer[i+2] + RSIValueBuffer[i+3]) / 10;



      if(LineBuffer[i] > UpLineBuffer[i])

        {

         MaxLineBuffer[i] = LineBuffer[i];

         RSILineBuffer[i + 1] = MaxLineBuffer[i + 1] = LineBuffer[i + 1];

         MinLineBuffer[i + 1] = RSILineBuffer[i] =  MinLineBuffer[i] = 0;

         UpArrow(i);

        }

      else

         if(LineBuffer[i] < DnLineBuffer[i])

           {

            MinLineBuffer[i] = LineBuffer[i];

            RSILineBuffer[i + 1] = MinLineBuffer[i + 1] = LineBuffer[i + 1];

            MaxLineBuffer[i + 1] = MaxLineBuffer[i] = RSILineBuffer[i] = 0;

            DnArrow(i);

           }

         else

           {

            RSILineBuffer[i] = LineBuffer[i];

            RSILineBuffer[i + 1] = LineBuffer[i + 1];

            MaxLineBuffer[i] = MinLineBuffer[i] = 0;

            Delete(i);

           }

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

     }

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

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