Donchian Channel Please Convert

 

Hello,

 I would really appreciate it if someone could convert the attached donchian channel indicator to mql5.  I tried for hours now and I really don't get it.  Here is what I have done so far but I'm way too stupid to make it work:

 It gives me a donchian channel but it is way out of sync

//+------------------------------------------------------------------+
//|                                             Donchian Channel.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Mocke.mqh>

#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2


#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_type2   DRAW_LINE
#property indicator_color2  Blue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

double updonch[];
double downdonch[];

input int period = 21;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
  SetIndexBuffer(0,updonch,INDICATOR_DATA);
  SetIndexBuffer(1,downdonch,INDICATOR_DATA);


//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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 = Bars(_Symbol,_Period); i >=0  ;i--)
{

      updonch[i]=iHigh(_Symbol,_Period,iHighest(_Symbol,_Period,"HIGH",period,i));
      downdonch[i]=iLow(_Symbol,_Period,iLowest(_Symbol,_Period,"LOW",period,i));
      
      }
      
      
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 And for the included code:

   
double iHigh(string symbol,ENUM_TIMEFRAMES timeframe,int index)

{
   if(index < 0) return(-1);
   double Arr[];
   if(CopyHigh(symbol,timeframe, index, 1, Arr)>0) 
        return(Arr[0]);
   else return(-1);
}

double iLow(string symbol,ENUM_TIMEFRAMES timeframe,int index)

{
   if(index < 0) return(-1);
   double Arr[];
   if(CopyLow(symbol,timeframe, index, 1, Arr)>0) 
        return(Arr[0]);
   else return(-1);
}

 int iHighest(string symbol,
                 ENUM_TIMEFRAMES timeframe,
                 string type,
                 int count=WHOLE_ARRAY,
                 int start=0)
  {
   if(start<0) return(-1);
 
   if(count<=0) count=Bars(symbol,timeframe);
   if(type=="OPEN")
     {
      double Open[];
      ArraySetAsSeries(Open,true);
      CopyOpen(symbol,timeframe,start,count,Open);
      return(ArrayMaximum(Open,0,count)+start);
     }
   if(type=="LOW")
     {
      double Low[];
      ArraySetAsSeries(Low,true);
      CopyLow(symbol,timeframe,start,count,Low);
      return(ArrayMaximum(Low,0,count)+start);
     }
   if(type=="HIGH")
     {
      double High[];
      ArraySetAsSeries(High,true);
      CopyHigh(symbol,timeframe,start,count,High);
      return(ArrayMaximum(High,0,count)+start);
     }
   if(type=="CLOSE")
     {
      double Close[];
      ArraySetAsSeries(Close,true);
      CopyClose(symbol,timeframe,start,count,Close);
      return(ArrayMaximum(Close,0,count)+start);
     }
   if(type=="VOLUME")
     {
      long Volume[];
      ArraySetAsSeries(Volume,true);
      CopyTickVolume(symbol,timeframe,start,count,Volume);
      return(ArrayMaximum(Volume,0,count)+start);
     }
   if(type=="TIME")
     {
      datetime Time[];
      ArraySetAsSeries(Time,true);
      CopyTime(symbol,timeframe,start,count,Time);
      return(ArrayMaximum(Time,0,count)+start);
      //---
     }
   return(0);
  }
  
  
  
  
  
  
  
  
  int iLowest(string symbol,
                ENUM_TIMEFRAMES timeframe,
                string type,
                int count=WHOLE_ARRAY,
                int start=0)
  {
   if(start<0) return(-1);
   if(count<=0) count=Bars(symbol,timeframe);
   if(type== "OPEN")
     {
      double Open[];
      ArraySetAsSeries(Open,true);
      CopyOpen(symbol,timeframe,start,count,Open);
      return(ArrayMinimum(Open,0,count)+start);
     }
   if(type== "LOW")
     {
      double Low[];
      ArraySetAsSeries(Low,true);
      CopyLow(symbol,timeframe,start,count,Low);
      return(ArrayMinimum(Low,0,count)+start);
     }
   if(type== "HIGH")
     {
      double High[];
      ArraySetAsSeries(High,true);
      CopyHigh(symbol,timeframe,start,count,High);
      return(ArrayMinimum(High,0,count)+start);
     }
   if(type== "CLOSE")
     {
      double Close[];
      ArraySetAsSeries(Close,true);
      CopyClose(symbol,timeframe,start,count,Close);
      return(ArrayMinimum(Close,0,count)+start);
     }
   if(type== "VOLUME")
     {
      long Volume[];
      ArraySetAsSeries(Volume,true);
      CopyTickVolume(symbol,timeframe,start,count,Volume);
      return(ArrayMinimum(Volume,0,count)+start);
     }
   if(type=="TIME")
     {
      datetime Time[];
      ArraySetAsSeries(Time,true);
      CopyTime(symbol,timeframe,start,count,Time);
      return(ArrayMinimum(Time,0,count)+start);
     }
//---
   return(0);
  }
Files:
donchian.mq4  2 kb
 
Try this. Could be fine tuned, but it appears to work. Both MT-4 and MT-5 versions have same results.

Files:
 
Oo thanks wackena!  Really appreciate it
Reason: