Dynamic RSI

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
//+------------------------------------------------------------------+
//|                                                  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\nhttps://t.me/RomanKiverin"
#property indicator_separate_window

#property indicator_buffers 11

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

//--- indicator buffers
double         UpLineBuffer[];
double         DnLineBuffer[];
double         RSILineBuffer[];
double         RSIValueBuffer[];
double         MaxLineBuffer[];
double         MinLineBuffer[];
double         WMALineBuffer[];
double         DeltaBuffer[];
double         UpBuffer[];
double         DnBuffer[];
double         LineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   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;
        }
      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;
           }
         else
           {
            RSILineBuffer[i] = LineBuffer[i];
            RSILineBuffer[i + 1] = LineBuffer[i + 1];
           }
      //+------------------------------
     }
//--- 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 ---