//+------------------------------------------------------------------+
//|                                                   Trend_Line.mq4 |
//|                                 Брызгалов Александр Владимирович |
//|                                              sanyooooook@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Брызгалов Александр Владимирович"
#property link      "sanyooooook@mail.ru"
//#property show_inputs

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
double   bHi=WindowPriceMax(0);
double   eHi=WindowPriceMax(0);
datetime Hbegin=WindowTimeOnDropped();
datetime Hend=Time[0];

double bLo=WindowPriceMin(0);
double eLo=WindowPriceMin(0);
datetime Lbegin=WindowTimeOnDropped();
datetime Lend=Time[0];
int i=iBarShift(NULL,0,WindowTimeOnDropped(),false);
int Median=MathRound((i+1)/2);
ObjectCreateTrend("UP"+Period(),OBJ_TREND,0,Hbegin,bHi,Hend,eHi,Blue);
ObjectCreateTrend("DN"+Period(),OBJ_TREND,0,Lbegin,bLo,Lend,eLo,Red);
datetime TrendTimeH;
double TrendPriceH;
datetime TrendTimeL;
double TrendPriceL;
int p=0;
int H=0,L=0;
while (!IsStopped()&&(H!=1||L!=1))
{
   if (L<1)
   {
      bLo=bLo+Point;
      eLo=eLo+Point;
   }
   if (H<1)
   {
      bHi=bHi-Point;
      eHi=eHi-Point;
   }
   ObjectCreateTrend("UP"+Period(),OBJ_TREND,0,Hbegin,bHi,Hend,eHi,Blue);
   ObjectCreateTrend("DN"+Period(),OBJ_TREND,0,Lbegin,bLo,Lend,eLo,Red);
   i=iBarShift(NULL,0,WindowTimeOnDropped(),false);
   while(i>=0&&H<1&&!IsStopped())
   {
      if (High[i]>=ObjectGetValueByShift("UP"+Period(),i)&&H<1)
      {
          if (i<=Median)
          {
             ObjectSet("UP"+Period(),OBJPROP_TIME2,Time[i]);
             ObjectSet("UP"+Period(),OBJPROP_PRICE2,High[i]);
             Hend=Time[i];
             TrendTimeH=Time[i];
             TrendPriceH=High[i];
             H=1;
          }
          else
          {
             if (i>Median)
             {
                ObjectSet("UP"+Period(),OBJPROP_TIME1,Time[i]);
                ObjectSet("UP"+Period(),OBJPROP_PRICE1,High[i]);
                TrendTimeH=Time[i];
                TrendPriceH=High[i];
                Hbegin=Time[i];
                H=1;
             }
          }
      }
      i--;
   }
   i=iBarShift(NULL,0,WindowTimeOnDropped(),false);
   while(i>=0&&L<1&&!IsStopped())
   {
      if (Low[i]<=ObjectGetValueByShift("DN"+Period(),i)&&L<1)
      {
          if (i<=Median)
          {
             ObjectSet("DN"+Period(),OBJPROP_TIME2,Time[i]);
             ObjectSet("DN"+Period(),OBJPROP_PRICE2,Low[i]);
             TrendTimeL=Time[i];
             TrendPriceL=Low[i];
             Lend=Time[i];
             L=1;
          }
          else
          {
             if (i>Median)
             {
                ObjectSet("DN"+Period(),OBJPROP_TIME1,Time[i]);
                ObjectSet("DN"+Period(),OBJPROP_PRICE1,Low[i]);
                TrendTimeL=Time[i];
                TrendPriceL=Low[i];
                Lbegin=Time[i];
                L=1;
             }
          }
      }
      i--;
      //Sleep(100);
   }
   WindowRedraw();
}
p=0;
H=0;L=0;
while (!IsStopped()&&(H!=1||L!=1))
{
   if (L<1)
   {
      if (TrendTimeL<ObjectGet("DN"+Period(),OBJPROP_TIME2))
      {
         eLo=eLo+Point;
      }
      else
      {
         bLo=bLo+Point;
      }
   }
   if (H<1)
   {
      if (TrendTimeH<ObjectGet("UP"+Period(),OBJPROP_TIME2))
      {
         eHi=eHi-Point;
      }
      else
      {
         bHi=bHi-Point;
      }
   }
   ObjectCreateTrend("UP"+Period(),OBJ_TREND,0,Hbegin,bHi,Hend,eHi,Blue);
   ObjectCreateTrend("DN"+Period(),OBJ_TREND,0,Lbegin,bLo,Lend,eLo,Red);
   i=iBarShift(NULL,0,ObjectGet("UP"+Period(),OBJPROP_TIME1))-1;
   while(i>iBarShift(NULL,0,ObjectGet("UP"+Period(),OBJPROP_TIME2))&&H<1&&!IsStopped())
   {
      if (High[i]>=ObjectGetValueByShift("UP"+Period(),i)&&H<1)
      {
         H=1;
      }
      i--;
   }
   i=iBarShift(NULL,0,ObjectGet("DN"+Period(),OBJPROP_TIME1))-1;
   while(i>iBarShift(NULL,0,ObjectGet("DN"+Period(),OBJPROP_TIME2))&&L<1&&!IsStopped())
   {
      if (Low[i]<=ObjectGetValueByShift("DN"+Period(),i)&&L<1)
      {
         L=1;
      }
      i--;
      //Sleep(100);
   }
   WindowRedraw();
}
return(0);
}
//+------------------------------------------------------------------+
void ObjectCreateTrend(string iObjectName,int Type,int iWindow,int X1Time,double Y1Price,int X2Time,double Y2Price,color iColor)
{
	if (ObjectFind(iObjectName)<0) {
		ObjectCreate(iObjectName,Type,iWindow,0,0);
		ObjectSet(iObjectName,OBJPROP_COLOR,iColor);
		ObjectSet(iObjectName,OBJPROP_RAY,true);
		//Alert("");
	}
	if (X1Time!=0)
	   ObjectSet(iObjectName,OBJPROP_TIME1,X1Time);
	ObjectSet(iObjectName,OBJPROP_PRICE1,Y1Price);
	if (X2Time!=0)
	   ObjectSet(iObjectName,OBJPROP_TIME2,X2Time);
	if (Y2Price!=0)
	   ObjectSet(iObjectName,OBJPROP_PRICE2,Y2Price);
}