Author: ��������� ��������� ������������

This script is an automated tool designed to dynamically identify and draw trend lines on your trading charts. Instead of requiring you to manually click and drag lines to find the best fit, the script analyzes historical price data to automatically establish "resistance" (upper) and "support" (lower) boundaries.

Core Functionality

The tool works by scanning the price history starting from the point where you drop it onto your chart. It looks for specific price peaks and valleys to construct two primary lines:

  • Upward Trend Line (Blue): Represents the ceiling or resistance of the price action.
  • Downward Trend Line (Red): Represents the floor or support of the price action.

How the Logic Works

The script follows a sophisticated, multi-stage process to ensure the lines are as accurate as possible:

1. Initial Detection & Positioning When first activated, the script identifies the highest and lowest prices in the visible area. It then places preliminary blue and red lines based on these extremes.

2. Dynamic Adjustment (Scanning) The tool doesn't just "set and forget" these lines. It continuously scans the historical candles to see if any price peaks or valleys break the current trend line.

  • If it finds a higher peak than the current blue line, it adjusts the line's coordinates to encompass that new high point.
  • If it finds a lower valley than the current red line, it moves the red line to align with that new low point.

3. Symmetry & Refinement To ensure the lines aren't skewed by outliers, the script uses a "Median" calculation. This helps determine whether to adjust the beginning or the end of the trend line based on where the price violation occurred relative to the center of the viewed data.

4. Continuous Validation Once the primary trends are established, the script enters a secondary validation loop. It continuously monitors the market in real-time. If current live prices exceed the boundaries established by the trend lines, the script performs micro-adjustments (moving the lines by very small increments) to ensure the lines remain relevant to the active market movement.

Summary for the User

In simple terms, this script acts like an automated "trend hunter." You drop it on a chart, and it instantly identifies the current structural boundaries of the market. It then "grows" and "shifts" those lines automatically as new price data arrives, attempting to maintain a perfect fit between the trend lines and the most significant price action on your screen.

13 Views
0 Downloads
0 Favorites
Trend_Line
//+------------------------------------------------------------------+
//|                                                   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);
}

Comments