Hurst_Channel

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Indicator of the average true rangeMoving average indicator
0 Views
0 Downloads
0 Favorites
Hurst_Channel
ÿþ//+------------------------------------------------------------------+

//|                                                Hurst_Channel.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 "Hurst Channel indicator"

#property indicator_chart_window

#property indicator_buffers 10

#property indicator_plots   4

//--- plot Upper1

#property indicator_label1  "Upper channel 1"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Lower1

#property indicator_label2  "Lower channel 1"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Upper2

#property indicator_label3  "Upper channel 2"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Lower2

#property indicator_label4  "Lower channel 2"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input uint                 InpPeriod1        =  10;            // First channel: Period

input ENUM_APPLIED_PRICE   InpAppliedPrice1  =  PRICE_MEDIAN;  // First channel: Applied price

input double               InpMultiplier1    =  1.0;           // First channel: ATR multiplier

input uint                 InpPeriod2        =  60;            // Second channel: Period

input ENUM_APPLIED_PRICE   InpAppliedPrice2  =  PRICE_MEDIAN;  // Second channel: Applied price

input double               InpMultiplier2    =  3.0;           // Second channel: ATR multiplier

//--- indicator buffers

double         BufferUpper1[];

double         BufferUpper2[];

double         BufferLower1[];

double         BufferLower2[];

double         BufferATR1[];

double         BufferATR2[];

double         BufferMA1[];

double         BufferMA2[];

double         BufferMAP1[];

double         BufferMAP2[];

//--- global variables

double         multiplier1;

double         multiplier2;

int            period1;

int            period2;

int            period_back1;

int            period_back2;

int            period_max;

int            handle_atr1;

int            handle_atr2;

int            handle_ma1;

int            handle_ma2;

int            handle_maP1;

int            handle_maP2;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period1=int(InpPeriod1<1 ? 1 : InpPeriod1);

   period2=int(InpPeriod2<1 ? 1 : InpPeriod2);

   multiplier1=InpMultiplier1;

   multiplier2=InpMultiplier2;

   period_back1=(int)floor((period1-1)/2)+1;

   period_back2=(int)floor((period2-1)/2)+1;

   period_max=fmax(period1,period2);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUpper1,INDICATOR_DATA);

   SetIndexBuffer(1,BufferLower1,INDICATOR_DATA);

   SetIndexBuffer(2,BufferUpper2,INDICATOR_DATA);

   SetIndexBuffer(3,BufferLower2,INDICATOR_DATA);

   SetIndexBuffer(4,BufferATR1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferATR2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferMA1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,BufferMA2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferMAP1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferMAP2,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Hurst Channel ("+(string)period1+","+DoubleToString(multiplier1,1)+","+(string)period2+","+DoubleToString(multiplier2,1)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUpper1,true);

   ArraySetAsSeries(BufferUpper2,true);

   ArraySetAsSeries(BufferLower1,true);

   ArraySetAsSeries(BufferLower2,true);

   ArraySetAsSeries(BufferATR1,true);

   ArraySetAsSeries(BufferATR2,true);

   ArraySetAsSeries(BufferMA1,true);

   ArraySetAsSeries(BufferMA2,true);

   ArraySetAsSeries(BufferMAP1,true);

   ArraySetAsSeries(BufferMAP2,true);

//--- create handles

   ResetLastError();

   handle_atr1=iATR(NULL,PERIOD_CURRENT,period1);

   if(handle_atr1==INVALID_HANDLE)

     {

      Print("The iATR (",(string)period1,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_atr2=iATR(NULL,PERIOD_CURRENT,period2);

   if(handle_atr2==INVALID_HANDLE)

     {

      Print("The iATR (",(string)period2,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_ma1=iMA(NULL,PERIOD_CURRENT,period1,0,MODE_SMA,InpAppliedPrice1);

   if(handle_ma1==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   handle_ma2=iMA(NULL,PERIOD_CURRENT,period2,0,MODE_SMA,InpAppliedPrice1);

   if(handle_ma2==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   handle_maP1=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice1);

   if(handle_maP1==INVALID_HANDLE)

     {

      Print(__LINE__,": The iMA (1) object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_maP2=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice2);

   if(handle_maP2==INVALID_HANDLE)

     {

      Print(__LINE__,": The iMA (1) 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(BufferUpper1,EMPTY_VALUE);

      ArrayInitialize(BufferUpper2,EMPTY_VALUE);

      ArrayInitialize(BufferLower1,EMPTY_VALUE);

      ArrayInitialize(BufferLower2,EMPTY_VALUE);

      ArrayInitialize(BufferATR1,0);

      ArrayInitialize(BufferATR2,0);

      ArrayInitialize(BufferMA1,0);

      ArrayInitialize(BufferMA2,0);

     }

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

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

   copied=CopyBuffer(handle_atr1,0,0,count,BufferATR1);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_atr2,0,0,count,BufferATR2);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_ma1,0,0,count,BufferMA1);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_ma2,0,0,count,BufferMA2);

   if(copied!=count) return 0;

   

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

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

     {

      BufferUpper1[i]=BufferMA1[i]+multiplier1*BufferATR1[i];

      BufferLower1[i]=BufferMA1[i]-multiplier1*BufferATR1[i];

     }

   double v1=(BufferMA1[0]-BufferMA1[period_back1])/period_back1;

   for(int j=period_back1-1;j>0;j--)

     {

      int j2=j*2;

      double v2=0;

      for(int n=0;n>=j2;n++)

         v2+=BufferMAP1[n];

      BufferMA1[0]=(v2/(j2+1))/3+(BufferMA1[1]+v1*(period_back1-j))*2/3;

      BufferUpper1[j]=BufferMA1[j]+multiplier1*BufferATR1[j];

      BufferLower1[j]=BufferMA1[j]-multiplier1*BufferATR1[j];

     }

   

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

     {

      BufferUpper2[i]=BufferMA2[i]+multiplier2*BufferATR2[i];

      BufferLower2[i]=BufferMA2[i]-multiplier2*BufferATR2[i];

     }

   v1=(BufferMA2[0]-BufferMA2[period_back2])/period_back2;

   for(int j=period_back2-1;j>0;j--)

     {

      int j2=j*2;

      double v2=0;

      for(int n=0;n>=j2;n++)

         v2+=BufferMAP2[n];

      BufferMA2[0]=(v2/(j2+1))/3+(BufferMA2[1]+v1*(period_back2-j))*2/3;

      BufferUpper2[j]=BufferMA2[j]+multiplier2*BufferATR2[j];

      BufferLower2[j]=BufferMA2[j]-multiplier2*BufferATR2[j];

     }



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