TD_Points&Line_Auto

Download script
Author: Vladislav Goshkov (VG)
TD_Points&Line_Auto

This script is an automated technical analysis tool designed for the MetaTrader platform. It identifies specific price trends and potential reversal points by calculating "supply" and "demand" zones based on historical price action.

Core Logic Overview

The tool operates by scanning recent market history to identify significant high and low points, which it interprets as supply and demand levels. Here is a breakdown of the logical steps it performs:

1. Identification of Market Extremes The script continuously monitors price bars to find notable peaks (Supply) and troughs (Demand). It specifically looks for instances where price reaches a new local high or low compared to its immediate surroundings. These points are used as anchors to determine the current "trend" of the market's boundaries.

2. Trendline Construction Based on these identified supply and demand points, the script automatically draws two dynamic trendlines on your chart:

  • Supply Line: Connects recent high points to visualize downward pressure or resistance.
  • Demand Line: Connects recent low points to visualize upward support.

3. Price Intersection & Breakout Detection The logic then calculates the "velocity" or slope of these trendlines. It looks for specific moments where the current price crosses through these trendlines. When a price breaks through a line, the script interprets this as a potential shift in market sentiment (a breakout).

4. Signal Generation and Target Calculation When a breakout is detected, the script performs a secondary analysis to calculate potential "Target" prices. It does this by:

  • Checking specific conditions regarding candle closes and opening prices to ensure the move has sufficient momentum.
  • Calculating three different mathematical targets based on the distance of previous price extremes.
  • Providing an average target price to help traders identify where the price might stabilize after a breakout.

5. Visual Feedback To make this data actionable for the user, the script provides several visual cues:

  • Trendlines: Real-time lines showing the current supply and demand boundaries.
  • Arrows: Dynamic symbols appear on the chart to highlight exactly where a significant "Breakout Signal" has occurred.
  • Horizontal Lines: The script automatically projects horizontal lines at various target prices (represented in different colors and styles) based on the most recent breakout calculation.

Summary for the User

In essence, this tool automates the process of drawing support/resistance trendlines and identifying "breakout" points. Instead of you having to manually calculate where a price might go after it breaks a trend, the script identifies the break, calculates three potential mathematical targets, and draws them on your screen automatically.

9 Views
0 Downloads
0 Favorites
TD_Points&Line_Auto
//+------------------------------------------------------------------+
//|                                        TD_Points&Lines_mgtd1.mq4 |
//|                                           Vladislav Goshkov (VG) |
//|                                                      4vg@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Vladislav Goshkov (VG)"
#property link      "4vg@mail.ru"

#property indicator_chart_window

extern int StepBack=0;
extern bool Qw1=True;
extern bool Qw2=True;
extern bool Qw3=True;
extern color UpLineColor = Blue;
extern int   UpLnWidth = 1;
extern color DnLineColor = Red;
extern int   DnLnWidth = 1;
extern color MarkColor   = Blue;
extern int   MarkNumber  = 217;

int i=1,NP=0,D=0,
    iB_Up=0,iB_Dn=0,
    S1=0,
    S2=0,
    UpLev=0,
    DownLev=0,
    iP_Up=0,
    iP_Dn=0,
    value=0,
    CurPeriod=0,
    shift=0;

datetime  nTime=0;

double UpV=0,
       DownV=0,
       iP=0,
       target = 0,
       UpP[2]={0,0},
       DownP[2]={0,0},
       PP1=0,PP2=0,PP3=0;

int    DownBT[2]={0,0}, // Bar Time
       UpBT[2]={0,0},
       UpB[2]={0,0},    // Bar Num
       DownB[2]={0,0};
string buff_str = "";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
Comment(" ");   
ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
int Target_Style[4] = {STYLE_DASH,STYLE_DASH,STYLE_DASH,STYLE_SOLID},
    Target_LW[4]    = { 1, 1, 1, 2 },
    i=0,ic=0;
color Target_Color[4] = {DimGray,MediumSpringGreen,SeaGreen,Blue},
      Target_Up_Color[4] = {DimGray,MediumSpringGreen,SeaGreen,Blue},
      Target_Dn_Color[4] = {Magenta,Orange,Yellow,Red};
double target[4];
//---- TODO: add your code here
if ( (nTime!=Time[0]) || (CurPeriod!=Period()) ) {
   UpP[0] = 0;
   UpP[1] = 0;

//=================================================
//******** Ïîèñê îïîðíûõ òî÷åê ïðåäëîæåíèÿ ********
//=================================================
   for(i=2+StepBack,D=2,NP=0; (NP<D)&&(i<Bars); i++) {//Begin
       if (High[i]!= High[i+1]) { 
          if( (High[i]>High[i+1] && High[i]>High[i-1] && High[i]>Close[i+2] ) 
              && High[i]> UpP[0] ) {
	     	 UpB[NP]  = i;
		    UpBT[NP] = Time[i];
		    UpP[NP]  = High[i];
		    NP++;
		    }
		  }

    if (High[i]== High[i+1])  { 
       if ( (High[i]>High[i+2] && High[i]>High[i-1] && High[i]>Close[i+3] ) && High[i]> UpP[0] ) {
	     	 UpB[NP]  = i;
		    UpBT[NP] = Time[i];
		    UpP[NP]  = High[i];
		    NP++;
		    }
		  }
     if(i == (Bars-2) ) {
	     	 UpB[NP]  = i;
		    UpBT[NP] = Time[i];
		    UpP[NP]  = High[i];
		    break;
		    }
     }//for(i=2+StepBack,D=2,NP=0; NP<D; ) {//End;

//=================================================
//********** Ïîèñê îïîðíûõ òî÷åê ñïðîñà ***********
//=================================================
   DownP[0] = 1000000000;
   DownP[1] = 1000000000;
   for(i=2+StepBack,D=2,NP=0; (NP<D)&&(i<Bars); i++) {//Begin
    if (Low[i]!= Low[i+1])  { 
       if ( (Low[i]<Low[i+1] && Low[i]<Low[i-1] && Low[i]<Close[i+2] ) && Low[i]< DownP[0] ){
	  	    DownB[NP] = i;
		    DownBT[NP]= Time[i];
		    DownP[NP] = Low[i];
		    NP++;
		    }
		  //i++;
		  }
    if (Low[i]== Low[i+1])  { 
       if ( (Low[i]<Low[i+2] && Low[i]<Low[i-1] && Low[i]<Close[i+3] ) && Low[i]< DownP[0] ){
	  	    DownB[NP] = i;
		    DownBT[NP]= Time[i];
		    DownP[NP] = Low[i];
		    NP++;
		    }
		  //i++;
		  }
     if (i == (Bars-2) ) { 
	  	    DownB[NP] = i;
		    DownBT[NP]= Time[i];
		    DownP[NP] = Low[i];
		    break;
		    }
     }//End;
     

UpV = (UpP[1]-UpP[0])/(UpB[0]-UpB[1]);
DownV = (DownP[1]-DownP[0])/(DownB[0]-DownB[1]);

//=================================================
//****       Ðèñóåì  TD-ëèíèè                  ****
//=================================================
   buff_str = "TD_Up";
   if(ObjectFind(buff_str) == -1) {
      ObjectCreate(buff_str, OBJ_TREND, 0, UpBT[1], UpP[1],UpBT[0], UpP[0]);
      ObjectSet(buff_str, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(buff_str, OBJPROP_COLOR, UpLineColor);
      ObjectSet(buff_str, OBJPROP_WIDTH, UpLnWidth);
      }
   else {
      ObjectDelete(buff_str);
      ObjectCreate(buff_str, OBJ_TREND, 0, UpBT[1], UpP[1],UpBT[0], UpP[0]);
      ObjectSet(buff_str, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(buff_str, OBJPROP_COLOR, UpLineColor);
      ObjectSet(buff_str, OBJPROP_WIDTH, UpLnWidth);
      }

   buff_str = "TD_Dn";
   if(ObjectFind(buff_str) == -1) {
      ObjectCreate(buff_str, OBJ_TREND, 0, DownBT[1], DownP[1],DownBT[0], DownP[0]);
      ObjectSet(buff_str, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(buff_str, OBJPROP_COLOR, DnLineColor);
      ObjectSet(buff_str, OBJPROP_WIDTH, DnLnWidth);
      }
   else {
      ObjectDelete(buff_str);
      ObjectCreate(buff_str, OBJ_TREND, 0, DownBT[1], DownP[1],DownBT[0], DownP[0]);
      ObjectSet(buff_str, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(buff_str, OBJPROP_COLOR, DnLineColor);
      ObjectSet(buff_str, OBJPROP_WIDTH, DnLnWidth);
      }


   CurPeriod = Period();
   nTime = Time[0];
   }//if (nTime <> Time[0]) {
//----
   for(i=0;i<4;i++) target[i] = 0;
	for( i=UpB[0]; i>=StepBack; i--){
	   if( (Close[i]>UpP[0]+UpV*(UpB[0]-i)) && (Close[i+1]<=UpP[0]+UpV*(UpB[0]-(i+1))) ) {
		    iB_Up=i; 
		    iP=UpP[0]+UpV*(UpB[0]-i);
		    iP_Up = iP; 
		    S1=Lowest(NULL,0,MODE_LOW,UpB[1]-i,i);
		    S2=Lowest(NULL,0,MODE_CLOSE,UpB[1]-i,i);

	       PP1=iP+((UpP[1]+UpV*(UpB[1]-S1))-Low[S1]);
	       PP2=iP+((UpP[1]+UpV*(UpB[1]-S2))-Low[S2]);
          PP3=iP+((UpP[1]+UpV*(UpB[1]-S1))-Close[S1]);
		    if((Qw1 && Close[i+2]>Close[i+1])
          || (Qw2 && Open[i]> iP)
          || (Qw3 && (Close[i+1]+(Close[i+1]-MathMin(Low[i+1],Close[i+2])))<iP) ) {
             target[0] = PP1;
             target[1] = PP2;
             target[2] = PP3;
             target[3] = (PP1+PP2+PP3)/3;
             for(ic=0;ic<4;ic++) Target_Color[ic] = Target_Up_Color[ic];
             ObjectDelete("Break_Sig_Dn");
             buff_str = "Break_Sig_Up";
             if(ObjectFind(buff_str) == -1) {
                ObjectCreate(buff_str, OBJ_ARROW,0, Time[iB_Up], iP+2*Point );
                ObjectSet(buff_str, OBJPROP_ARROWCODE, 164);
                ObjectSet(buff_str, OBJPROP_COLOR, Red);
                }
             else {
                ObjectMove(buff_str, 0, Time[iB_Up], iP+2*Point );
                }
             }
		    }
	   }

   for( i=DownB[0]; i>=StepBack;i--) {
	   if( (Close[i]<DownP[0]+DownV*(DownB[0]-i)) && (Close[i+1]>=DownP[0]+DownV*(DownB[0]-(i+1))) ) {
	       iB_Dn=i; 
		    iP=DownP[0]+DownV*(DownB[0]-i);
		    iP_Dn = iP;
		    S1=Highest(NULL,0,MODE_HIGH,DownB[1]-i,i);
		    S2=Highest(NULL,0,MODE_CLOSE,DownB[1]-i,i);

          PP1=iP-(High[S1]-(DownP[1]+DownV*(DownB[1]-S1)));
	       PP2=iP-(High[S2]-(DownP[1]+DownV*(DownB[1]-S2)));
	       PP3=iP-(Close[S1]-(DownP[1]+DownV*(DownB[1]-S1)));
    		 if((Qw1 && Close[i+2]<Close[i+1] )
          || (Qw2 && Open[i] < iP )
		    || (Qw3 && (Close[i+1]+(Close[i+1]-MathMax(High[i+1],Close[i+2])))>iP) ) {
             target[0] = PP1;
             target[1] = PP2;
             target[2] = PP3;
             target[3] = (PP1+PP2+PP3)/3;
             for(ic=0;ic<4;ic++) Target_Color[ic] = Target_Dn_Color[ic];
             ObjectDelete("Break_Sig_Up");
             buff_str = "Break_Sig_Dn";
             if(ObjectFind(buff_str) == -1) {
                ObjectCreate(buff_str, OBJ_ARROW,0, Time[iB_Dn], iP+2*Point );
                ObjectSet(buff_str, OBJPROP_ARROWCODE, 164);
                ObjectSet(buff_str, OBJPROP_COLOR, Blue);
                }
             else {
                ObjectMove(buff_str, 0, Time[iB_Dn], iP+2*Point );
                }
             }
 		    }
		}//for( i=DownB[1]; i>=StepBack;i--) {

   for(i=3;i>=0;i--) {
      buff_str = "Target"+i;
      if(ObjectFind(buff_str) == -1) {
         ObjectCreate(buff_str, OBJ_HLINE,0, Time[0], target[i] );
         ObjectSet(buff_str, OBJPROP_STYLE, Target_Style[i]);
         ObjectSet(buff_str, OBJPROP_COLOR, Target_Color[i]);
         ObjectSet(buff_str, OBJPROP_WIDTH, Target_LW[i]);
         }
      else {
         ObjectMove(buff_str, 0, Time[0], target[i] );
         }
      }


//----
string buff_str = "TD_LatestCulcBar";
   if(ObjectFind(buff_str) == -1) {
      ObjectCreate(buff_str, OBJ_ARROW,0, Time[StepBack], Low[StepBack]-2*Point );
      ObjectSet(buff_str, OBJPROP_ARROWCODE, MarkNumber);
      ObjectSet(buff_str, OBJPROP_COLOR, MarkColor);
      }
   else {
      ObjectMove(buff_str, 0, Time[StepBack], Low[StepBack]-2*Point );
      }
   return(0);
  }
  
//====================================================================
//  Code End. Regards
//====================================================================


Comments