//+------------------------------------------------------------------+
//| TheTurtleTradingChannelmq4
//| Copyright © Pointzero-indicator.com
//+------------------------------------------------------------------+
#property copyright "Copyright © Pointzero-indicator.com"
#property link      "http://www.pointzero-indicator.com"

//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_color3 DarkSlateGray
#property indicator_color4 DarkSlateGray
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 1
#property indicator_width4 1
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT

//---- indicator parameters
extern bool CalculateOnBarClose = false;
extern int  TradePeriod         = 10;
extern int  StopPeriod          = 5;

//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

//----- internal variables
int direction = EMPTY_VALUE;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   // Drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
   // Name and labels
   IndicatorShortName("Turtle Channel ("+ TradePeriod +")");
   SetIndexLabel(0,"Upper Channel");
   SetIndexLabel(1,"Lower Channel");
   SetIndexLabel(2,"Phantom Up Channel");
   SetIndexLabel(3,"Phantom Down Channel");
   
   // Buffers
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);

   Comment("Copyright © http://www.pointzero-indicator.com");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
     // Start, limit, etc..
     int start = 0;
     int limit;
     int counted_bars = IndicatorCounted();

     // check for possible errors
     if(counted_bars < 0) 
        return(-1);
        
     // Only check these
     limit = Bars - 1 - counted_bars;
    
     // Check if ignore bar 0
     if(CalculateOnBarClose == true) start = 1;
    
     // Check the signal foreach bar
     for(int i = limit; i >= start; i--)
     {           
         // Highs and lows for this
         double rhigh = getPeriodHigh(TradePeriod, i+1);
         double rlow  = getPeriodLow(TradePeriod, i+1);
         double shigh = getPeriodHigh(StopPeriod, i+1);
         double slow  = getPeriodLow(StopPeriod, i+1);
         
         // Candle value
         double CLOSE = iClose(Symbol(),0, i);
         double OPEN = iOpen(Symbol(),0, i);
         double HIGH = iHigh(Symbol(),0, i);
         double LOW = iLow(Symbol(),0, i);
         
         // Change to uptrend
         if(CLOSE > rhigh && direction != OP_BUY)
         {
            direction = OP_BUY;
         } else if(CLOSE < rlow && direction != OP_SELL) {
            direction = OP_SELL;
         }
         
         // Long
         if(direction == OP_BUY)
         {
            ExtMapBuffer1[i] = rlow;
            ExtMapBuffer3[i] = slow;
            
         // Short
         } else if(direction == OP_SELL) {
         
            ExtMapBuffer2[i] = rhigh;
            ExtMapBuffer4[i] = shigh;
         }
     }
   
   // Bye Bye
   return(0);
  }
 
 /**
 * High of last N Bars
 */
 double getPeriodHigh(int period, int pos)
 {
   int i;
   double buffer = 0;
   for (i=pos;i<=pos+period;i++) {
      if (High[i] > buffer) {
         buffer = High[i];
      }
   }
   return (buffer);
}

/**
* Low of last N Bars
*/
double getPeriodLow(int period, int pos) {
   int i;
   double buffer = 100000;
   for (i=pos;i<=pos+period;i++) {
      if (Low[i] < buffer) {
         buffer = Low[i];
      }
   }
   return (buffer);
}


