RSI_Donchian

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Relative strength index
0 Views
0 Downloads
0 Favorites
RSI_Donchian
ÿþ//+------------------------------------------------------------------+

//|                                                 RSI_Donchian.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 "Donchian channel based on RSI"

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot RSI

#property indicator_label1  "RSI"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDodgerBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Top

#property indicator_label2  "Channel Top"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Middle

#property indicator_label3  "Channel Middle"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Bottom

#property indicator_label4  "Channel Bottom"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrGreen

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input uint     InpPeriodRSI   =  20;      // RSI period

input uint     InpPeriodDCH   =  20;      // DChannel period

input double   InpOverbought  =  70.0;    // RSI overbought

input double   InpOversold    =  30.0;    // RSI oversold

//--- indicator buffers

double         BufferRSI[];

double         BufferTop[];

double         BufferMiddle[];

double         BufferBottom[];

//--- global variables

double         overbought;

double         oversold;

int            period_rsi;

int            period_dch;

int            period_max;

int            handle_rsi;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

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

   period_dch=int(InpPeriodDCH<1 ? 1 : InpPeriodDCH);

   period_max=fmax(period_dch,period_rsi);

   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,BufferRSI,INDICATOR_DATA);

   SetIndexBuffer(1,BufferTop,INDICATOR_DATA);

   SetIndexBuffer(2,BufferMiddle,INDICATOR_DATA);

   SetIndexBuffer(3,BufferBottom,INDICATOR_DATA);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferRSI,true);

   ArraySetAsSeries(BufferTop,true);

   ArraySetAsSeries(BufferMiddle,true);

   ArraySetAsSeries(BufferBottom,true);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"RSI Donchian ("+(string)period_rsi+","+(string)period_dch+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,overbought);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,oversold);

   IndicatorSetString(INDICATOR_LEVELTEXT,0,"Overbought");

   IndicatorSetString(INDICATOR_LEVELTEXT,1,"Oversold");

//--- create handle

   ResetLastError();

   handle_rsi=iRSI(NULL,PERIOD_CURRENT,period_rsi,PRICE_CLOSE);

   if(handle_rsi==INVALID_HANDLE)

     {

      Print("The iRSI (",(string)period_rsi,") 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[])

  {

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<fmax(period_max,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(BufferRSI,EMPTY_VALUE);

      ArrayInitialize(BufferTop,EMPTY_VALUE);

      ArrayInitialize(BufferMiddle,EMPTY_VALUE);

      ArrayInitialize(BufferBottom,EMPTY_VALUE);

     }



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

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

   copied=CopyBuffer(handle_rsi,0,0,count,BufferRSI);

   if(copied!=count) return 0;



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

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      int max_idx=ArrayMaximum(BufferRSI,i,period_dch);

      int min_idx=ArrayMinimum(BufferRSI,i,period_dch);

      if(max_idx==WRONG_VALUE || min_idx==WRONG_VALUE)

         continue;

      BufferTop[i]=BufferRSI[max_idx];

      BufferBottom[i]=BufferRSI[min_idx];

      BufferMiddle[i]=(BufferTop[i]+BufferBottom[i])/2.0;

     }

   

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