//+------------------------------------------------------------------+
//|                                                      Channel.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © GPL General Public License"
#property link      "open source fx community"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  Blue
#property indicator_color2  Red
#property indicator_color3  DarkSlateGray

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame     = PERIOD_CURRENT;
extern int             ChannelPeriod = 21; 

//
//
//
//
//

double Up[];
double Dn[];
double mid[];
string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
//
//

int init()
{
   SetIndexBuffer(0,Up);
   SetIndexBuffer(1,Dn);
   SetIndexBuffer(2,mid);
   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame==-99;
   TimeFrame         = MathMax(TimeFrame,_Period);
   IndicatorShortName(timeFrameToString(TimeFrame)+" Donchian Channel ");
return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
//
//

int deinit() { return(0); }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
//
//

int start()
{
   int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars)  { Up[0] = limit+1; return(0); } 
             

   
   //
   //
   //
   //
   //
   
   if (TimeFrame == Period())
   {
     for (i=limit;i>=0;i--)
     {
         Up[i]  = High[iHighest(NULL,0,MODE_HIGH,ChannelPeriod,i+1)];            
         Dn[i]  =  Low[iLowest(NULL, 0,MODE_LOW, ChannelPeriod,i+1)];
         mid[i] = (Up[i]+Dn[i])*0.5;
     }
    return(0);
    }
    
    //
    //
    //
    //
    //
   
    limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
    for (i=limit;i>=0;i--)
    {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         Up[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ChannelPeriod,0,y);
         Dn[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ChannelPeriod,1,y);
         mid[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ChannelPeriod,2,y);
    }
return(0);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}