Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
RSI_Of_MA
ÿþ//+------------------------------------------------------------------+

//|                                                    RSI_Of_MA.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

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

#property version   "1.00"

#property description "RSI of MA oscillator"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   1

//--- plot MARSI

#property indicator_label1  "RSI of MA"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrMediumSlateBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input uint                 InpPeriodMA       =  14;            // Source EMA period

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Source applied price

input uint                 InpPeriodRSI      =  21;            // RSI period

input uint                 InpPeriodMR       =  15;            // EMA of RSI period

input double               InpOverbought     =  80.0;          // Overbought

input double               InpOversold       =  20.0;          // Oversold

//--- indicator buffers

double         BufferMARSI[];

double         BufferRSI[];

double         BufferMA[];

double         BufferPosRSI[];

double         BufferNegRSI[];

//--- global variables

double         overbought;

double         oversold;

int            period_rsi;

int            period_ma;

int            period_mr;

int            handle_ma;

//--- includes

#include <MovingAverages.mqh>

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_rsi=int(InpPeriodRSI<1 ? 1 : InpPeriodRSI);

   period_ma=int(InpPeriodMA<1 ? 1 : InpPeriodMA);

   period_mr=int(InpPeriodMR<1 ? 1 : InpPeriodMR);

   overbought=(InpOverbought>100 ? 100 : InpOverbought<0.1 ? 0.1 : InpOverbought);

   oversold=(InpOversold<0 ? 0 : InpOversold>=overbought ? overbought-0.1 : InpOversold);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferMARSI,INDICATOR_DATA);

   SetIndexBuffer(1,BufferMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,BufferRSI,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferPosRSI,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BufferNegRSI,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"RSI of MA ("+(string)period_ma+","+(string)period_rsi+","+(string)period_mr+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_MAXIMUM,100);

   IndicatorSetDouble(INDICATOR_MINIMUM,0);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,overbought);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,oversold);

   IndicatorSetString(INDICATOR_LEVELTEXT,0,"Overbought");

   IndicatorSetString(INDICATOR_LEVELTEXT,1,"Oversold");

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferMARSI,true);

   ArraySetAsSeries(BufferMA,true);

   ArraySetAsSeries(BufferRSI,true);

   ArraySetAsSeries(BufferPosRSI,true);

   ArraySetAsSeries(BufferNegRSI,true);

//--- create MA handle

   ResetLastError();

   handle_ma=iMA(NULL,PERIOD_CURRENT,period_ma,0,MODE_EMA,InpAppliedPrice);

   if(handle_ma==INVALID_HANDLE)

     {

      Print("The iMA(",(string)period_ma,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

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

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(close,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<fmax(period_ma,4)) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-1;

      ArrayInitialize(BufferMARSI,EMPTY_VALUE);

      ArrayInitialize(BufferMA,0);

      ArrayInitialize(BufferRSI,0);

      ArrayInitialize(BufferPosRSI,0);

      ArrayInitialize(BufferNegRSI,0);

     }

//--- >43>B>2:0 40==KE

   int count=(limit>1 ? rates_total : 1);

   int copied=CopyBuffer(handle_ma,0,0,count,BufferMA);

   if(copied!=count) return 0;

   

//---  0AGQB 8=48:0B>@0

   if(RSIOnArray(rates_total,prev_calculated,0,period_rsi,BufferMA,BufferPosRSI,BufferNegRSI,BufferRSI)==0)

      return 0;

   return ExponentialMAOnBuffer(rates_total,prev_calculated,period_rsi,period_mr,BufferRSI,BufferMARSI);



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

   return(rates_total);

  }

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

//| Relative Strength Index on array                                 |

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

template<typename T>

int RSIOnArray(const int rates_total,

               const int prev_calculated,

               const int begin,

               const int period,

               const T &price[],

               double &buffer_pos[],

               double &buffer_neg[],

               double &buffer_rsi[]

               )

  {

   int   i;

   T     diff;

//--- check for rates count

   if(period<1 || rates_total-begin<period) return(0);

//--- save as_series flags

   bool as_series_price=ArrayGetAsSeries(price);

   bool as_series_rsi=ArrayGetAsSeries(buffer_rsi);

   if(as_series_price)

      ArraySetAsSeries(price,false);

   if(as_series_rsi)

     {

      ArraySetAsSeries(buffer_rsi,false);

      ArraySetAsSeries(buffer_pos,false);

      ArraySetAsSeries(buffer_neg,false);

     }

//--- preliminary calculations

   int pos=prev_calculated-1;

   if(pos<=period)

     {

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

      buffer_rsi[0]=0.0;

      buffer_pos[0]=0.0;

      buffer_neg[0]=0.0;

      T SumP=0.0;

      T SumN=0.0;

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

        {

         buffer_rsi[i]=0.0;

         buffer_pos[i]=0.0;

         buffer_neg[i]=0.0;

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

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

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

        }

      //--- calculate first visible value

      buffer_pos[period]=double(SumP/period);

      buffer_neg[period]=double(SumN/period);



      buffer_rsi[period]=100.0-(100.0/(1.0+buffer_pos[period]/(buffer_neg[period]>0 ? buffer_neg[period]: DBL_MIN)));

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

      pos=period+1;

     }

//--- the main loop of calculations

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

     {

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

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

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

      buffer_rsi[i]=100.0-100.0/(1+buffer_pos[i]/(buffer_neg[i]>0 ? buffer_neg[i] : DBL_MIN));

     }

//--- restore as_series flags

   if(as_series_price) ArraySetAsSeries(price,true);

   if(as_series_rsi)

     {

      ArraySetAsSeries(buffer_rsi,true);

      ArraySetAsSeries(buffer_pos,true);

      ArraySetAsSeries(buffer_neg,true);

     }

//---

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