//+------------------------------------------------------------------+
//|                                                    G Channel.mq4 |
//|                                          Copyright 2021,Kaustubh |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021,Kaustubh"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   7
//--- plot Upper
#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMagenta
#property indicator_style1  STYLE_DASH
#property indicator_width1  1
//--- plot Lower
#property indicator_label2  "Lower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrAqua
#property indicator_style2  STYLE_DASH
#property indicator_width2  1
//--- plot Middle
//--- plot a
#property indicator_label3  "a"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrLime
#property indicator_style3  STYLE_SOLID
#property indicator_width3  3

//--- plot b
#property indicator_label4  "b"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrOrangeRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  3

//--- indicator buffers
double         UpperBuffer[];
double         LowerBuffer[];
double         aBuffer[];
double         bBuffer[];
double         UptrendBuffer[];
double         DowntrendBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UpperBuffer);
   SetIndexBuffer(1,LowerBuffer);
   SetIndexBuffer(2,aBuffer);
   SetIndexBuffer(3,bBuffer);
   SetIndexArrow(4, 233); SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, 5, Lime);
   SetIndexArrow(5, 234); SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, 5, Red);
   SetIndexBuffer(4,UptrendBuffer);
   SetIndexBuffer(5,DowntrendBuffer);


   for(int i = 0; i <= 7; i++)
      SetIndexEmptyValue(i, 0.0);

   for(int i = 3; i <= 7; i++)
      SetIndexLabel(i, "");
//---
   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[])
  {

   for(int i=3600; i>=0; i--)
   {
         UpperBuffer[i] = iCustom(NULL,0,"G Channel",55,PRICE_CLOSE,0,i);
         LowerBuffer[i] = iCustom(NULL,0,"G Channel",55,PRICE_CLOSE,1,i);
      
         int   Bx=0, By=0, BStart, BEnd;
         UptrendBuffer[i] = EMPTY_VALUE; DowntrendBuffer[i] = EMPTY_VALUE;
         double Up_arrow_value = iCustom(Symbol(), Period(), "G-Channel_Trend_Detection",55,PRICE_CLOSE,true,3600, 3, i);
         double Down_arrow_value = iCustom(Symbol(), Period(), "G-Channel_Trend_Detection",55,PRICE_CLOSE,true,3600, 2, i);

         if(Up_arrow_value != EMPTY_VALUE && Up_arrow_value != 0){UptrendBuffer[i] = Up_arrow_value;  Bx = i;}
         if(Down_arrow_value != EMPTY_VALUE && Down_arrow_value != 0){DowntrendBuffer[i] = Down_arrow_value; By = i;}    

         if (Bx!=0  && By!=0 && Bx> By){BStart=Bx;BEnd=Bx-By;}
         if (Bx!=0  && By==0){BStart=Bx;BEnd=0;}
           for(int h=BStart; h>=BEnd; h--)
            {

              aBuffer[h] = iCustom(NULL,0,"G Channel",55,PRICE_CLOSE,2,h); bBuffer[h] = EMPTY_VALUE; }
         if (By!=0  && Bx!=0 && By> Bx){BStart=By;BEnd=By-Bx;}
         if (By!=0  && Bx==0){BStart=By;BEnd=0;}
           for(int j=BStart; j>=BEnd; j--)
            {
              bBuffer[j] = iCustom(NULL,0,"G Channel",55,PRICE_CLOSE,2,j); aBuffer[j] = EMPTY_VALUE; }
   }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
