Dynamic RSI

Author: Copyright 2023, MetaQuotes Ltd.
Indicators Used
Relative strength index
0 Views
0 Downloads
0 Favorites
Dynamic RSI
//+------------------------------------------------------------------+
//|                                                  Dynamic RSI.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                              https://www.mql5.com/ru/users/s22aa |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com/ru/users/s22aa"
#property version   "1.00"
#property indicator_separate_window

#property indicator_plots 4
#property indicator_buffers 7

#property indicator_label1  "RSI"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrDarkOrchid,clrDodgerBlue,clrDarkSeaGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_label2  "UP"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDarkGray
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

#property indicator_label3  "Middle"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrGold
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

#property indicator_label4  "DN"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrDarkGray
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2

#include <MovingAverages.mqh>

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 ENUM_APPLIED_PRICE appPriceRSI = PRICE_CLOSE;
input int      LookBack          =  60;                  // Look Back PeriodRSI
input ENUM_MA_METHOD ma_method   =  MODE_LWMA;

double         RSILineBuffer[];
double         ColorBuffer[];
double         RSIValueBuffer[];
double         UpLineBuffer[];
double         DnLineBuffer[];
double         WMALineBuffer[];
double         DeltaBuffer[];

int handle;
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, RSILineBuffer,  INDICATOR_DATA);
   SetIndexBuffer(1, ColorBuffer,    INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, UpLineBuffer,   INDICATOR_DATA);
   SetIndexBuffer(3, WMALineBuffer,  INDICATOR_DATA);
   SetIndexBuffer(4, DnLineBuffer,   INDICATOR_DATA);
   SetIndexBuffer(5, DeltaBuffer,    INDICATOR_CALCULATIONS);
   SetIndexBuffer(6, RSIValueBuffer, INDICATOR_CALCULATIONS);

   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, LookBack + PeriodRSI * 2);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, LookBack + PeriodRSI * 2);
   PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, LookBack + PeriodRSI * 2);
   PlotIndexSetInteger(3, PLOT_DRAW_BEGIN, LookBack + PeriodRSI * 2);

   handle = iRSI(_Symbol, _Period, PeriodRSI, appPriceRSI);
   if(handle == INVALID_HANDLE)
     {
      PrintFormat("Failed  RSI ", GetLastError());
      return(INIT_FAILED);
     }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
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 limit = prev_calculated - 1;
   int to_copy = rates_total - prev_calculated + 1;

   if(prev_calculated <= 0)
     {
      limit = LookBack + 5;
      to_copy = rates_total;
     }

   if(CopyBuffer(handle, 0, 0, to_copy, RSIValueBuffer) <= 0)
     {
      Print("copy RSI Error ", GetLastError());
      return(0);
     }

   for(int i = limit; i < rates_total; i ++)
     {
      double max = -DBL_MAX;
      double min = DBL_MAX;
      int j = i;
      while(i - j < LookBack)
        {
         if(max < RSIValueBuffer[j])
            max = RSIValueBuffer[j];
         if(min > RSIValueBuffer[j])
            min = RSIValueBuffer[j];
         j--;
        }

      DeltaBuffer[i] = (max + min) / 2.0;

      switch(ma_method)
        {
         case MODE_SMA:
            WMALineBuffer[i] = SimpleMA(i, PeriodRSI, DeltaBuffer);
            break;
         case  MODE_EMA :
            WMALineBuffer[i] = ExponentialMA(i, PeriodRSI, DeltaBuffer[i - 1], DeltaBuffer);
            break;
         case MODE_SMMA :
            WMALineBuffer[i] = SmoothedMA(i, PeriodRSI, DeltaBuffer[i - 1], DeltaBuffer);
            break;
         case MODE_LWMA :
            WMALineBuffer[i] = LinearWeightedMA(i, PeriodRSI, DeltaBuffer);
            break;
        }

      UpLineBuffer[i] = max - WMALineBuffer[i] * DZbuy;
      DnLineBuffer[i] = min + WMALineBuffer[i] * DZsell;
      RSILineBuffer[i] = (4 * RSIValueBuffer[i] + 3 * RSIValueBuffer[i - 1] + 2 * RSIValueBuffer[i - 2] + RSIValueBuffer[i - 3]) / 10;
      ColorBuffer[i] = RSILineBuffer[i] > UpLineBuffer[i] ? 0 : RSILineBuffer[i] < DnLineBuffer[i] ? 1 : 2;
     }
   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 ---