//+------------------------------------------------------------------+
//|                                             Trend Channel v1.mq4 |
//|                                                         George A |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "George A"
#property link      ""

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Blue
#property indicator_color2 Aqua


double TrendLine1[];
double TrendLine2[];

bool uptrend = true;
double g_LowestLow;
double g_HighestHigh;
double g_LowestLow2;
double g_HighestHigh2;

int   g_LowestLowBar;
int   g_HighestHighBar;
int   g_LowestLow2Bar;
int   g_HighestHigh2Bar;

double g_Ratio1;
double g_Ratio2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
   
   string short_name = "Trend Channel";
   
   IndicatorShortName(short_name);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TrendLine1);
   SetIndexLabel(0,short_name);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,TrendLine2);
   SetIndexLabel(1,"Lower trend line");
   
   SetIndexDrawBegin(0,1);
   
   //SetIndexDrawBegin(1,1);
   
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int    counted_bars=IndicatorCounted();

   int WinBars = WindowBarsPerChart();

   int l_FirstBar = WindowFirstVisibleBar();
   int l_LastBar  = l_FirstBar - WindowBarsPerChart();

   l_FirstBar = WinBars;
   l_LastBar  = 0;
   
   //Alert(l_FirstBar);
   //Alert(l_LastBar);

   //Alert(counted_bars);
   //Alert(WinBars);

   int i = 0;
   
   //g_LowestLow       = Close[counted_bars];
   //g_LowestLowBar    = counted_bars;
   g_LowestLow       = Close[l_FirstBar];
   g_LowestLowBar    = l_FirstBar;
   g_HighestHigh     = g_LowestLow;
   g_HighestHighBar  = g_LowestLowBar;
   
   for(i = l_FirstBar;i>=l_LastBar;i--)
   {
      if (Close[i] < g_LowestLow)
      {
         g_LowestLow    = Close[i];
         g_LowestLowBar = i;
      }
      
      if (Close[i] > g_HighestHigh)
      {
         g_HighestHigh = Close[i];
         g_HighestHighBar = i;
      }
   
   }
   
   //Alert(g_LowestLow);
   //Alert(g_HighestHigh);
   
   if (g_HighestHighBar > g_LowestLowBar)
      uptrend = false;
   else
      uptrend = true;
   
   //Alert(uptrend);
   
   /*Alert(g_LowestLowBar);
   Alert(g_HighestHighBar);
   Alert(g_LowestLow);
   Alert(g_HighestHigh);*/
   
   if (uptrend)
   {
      g_LowestLow2    = g_HighestHigh;
      g_LowestLow2Bar = g_HighestHighBar;
      for(i = g_HighestHighBar;i>=l_LastBar;i--)
      {
         if (Close[i] < g_LowestLow2)
         {
            g_LowestLow2    = Close[i];
            g_LowestLow2Bar = i;
         }
      }
      
      g_Ratio1 = (g_LowestLow2 - g_LowestLow ) / (g_LowestLowBar - g_LowestLow2Bar);
      for(i = l_FirstBar;i>=l_LastBar;i--)
         TrendLine1[i] = g_LowestLow2 - ((i-g_LowestLow2Bar)*g_Ratio1);
      
      //g_Ratio2 = g_HighestHigh - Trendline[g_HighestHighBar];
      for(i = l_FirstBar;i>=l_LastBar;i--)
         TrendLine2[i] = g_HighestHigh - (( i - g_HighestHighBar)*g_Ratio1);
   
   }
   else
   {
      g_HighestHigh2    = g_LowestLow;
      g_HighestHigh2Bar = g_LowestLowBar;
      for(i = g_LowestLowBar;i>=l_LastBar;i--)
      {
         if (Close[i] > g_HighestHigh2)
         {
            g_HighestHigh2    = Close[i];
            g_HighestHigh2Bar = i;
         }
      }
   
      g_Ratio1 = (g_HighestHigh2 - g_HighestHigh ) / (g_HighestHighBar - g_HighestHigh2Bar);
      for(i = l_FirstBar;i>=l_LastBar;i--)
         TrendLine1[i] = g_HighestHigh2 - ((i-g_HighestHigh2Bar)*g_Ratio1);
      
      //g_Ratio2 = g_HighestHigh - Trendline[g_HighestHighBar];
      
      for(i = l_FirstBar;i>=l_LastBar;i--)
         TrendLine2[i] = g_LowestLow - (( i - g_LowestLowBar)*g_Ratio1);
   
   }

   return(0);
}
//+------------------------------------------------------------------+-----------------------------+