deMark_TrendLines_&_Alert

Download script
deMark_TrendLines_&_Alert

This script is designed for the MetaTrader platform to automatically identify and display dynamic trendlines on your trading charts. Its primary purpose is to visualize price movement patterns and provide real-time notifications when the current market trend shifts.

How the Logic Works

1. Identifying Key Market Points The script scans historical price data to find two specific types of coordinates: "Offer" points (peaks) and "Demand" points (troughs). Instead of simply picking any high or low, it uses a specific set of criteria to ensure these points are significant:

  • Peaks: It looks for price highs that are higher than the candles immediately preceding and following them.
  • Troughs: It looks for price lows that are lower than the candles immediately preceding and following them.

2. Dynamic Trendline Construction Once these significant points are identified, the script connects them to form two distinct trendlines:

  • Falling Trendline: Connects major peaks to show where the price is struggling to move upward.
  • Rising Trendline: Connects major troughs to show where the price is struggling to move downward.

The script automatically updates these lines as new price data arrives, ensuring that the visual representation of the trend is always based on the most recent significant market behavior.

3. Automated Breakout Detection Beyond just drawing lines, the script actively monitors whether the current price "breaks" through these established trends. It watches for specific transitions:

  • Short Opportunity: If the price closes below a Rising Trendline (where it was previously holding steady), the script recognizes a potential trend reversal to the downside.
  • Long Opportunity: If the price closes above a Falling Trendline (where it was previously held down), the script recognizes a potential trend reversal to the upside.

4. Real-Time Notifications When a trend break is confirmed at the close of a candle, the script can trigger an immediate alert on your screen. This is intended to give traders a timely signal to look for new entry positions (Long or Short) based on the shift in market momentum.

Price Data Components
Series array that contains close prices for each bar
8 Views
0 Downloads
0 Favorites
deMark_TrendLines_&_Alert
//+------------------------------------------------------------------+
//|                                        deMark_Trendlines.mq4     |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern int StepBack=0;
extern color UpLineColor = Magenta;
extern int   UpLnWidth = 1;
extern color DnLineColor = Aqua;
extern int   DnLnWidth = 1;
extern bool  UseAlert = true;


int i=1,NP=0,D=0,
    CurPeriod=0;

datetime  nTime=0;

double iP=0,
       UpP[2]={0,0},
       DownP[2]={0,0};
       
double   RisingTrend,FallingTrend,LastClose;       
double   PriorRisingTrend,PriorFallingTrend,PriorLastClose;       

int    DownBT[2]={0,0}, // Bar Time
       UpBT[2]={0,0},
       UpB[2]={0,0},    // Bar Num
       DownB[2]={0,0};
       
string buff_str = "";

bool NewBar()
{
   static datetime lastbar = 0;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return (false);
   }
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

int deinit()
  {

ObjectDelete("FallingTrend");
ObjectDelete("RisingTrend");

//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {

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;
		    }
     }
     
//=================================================
//********** Ïîèñê îïîðíûõ òî÷åê ñïðîñà ***********
//=================================================

   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++;
		    }
		  }
    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++;
		    }
		  }
     if (i == (Bars-2) ) { 
	  	    DownB[NP] = i;
		    DownBT[NP]= Time[i];
		    DownP[NP] = Low[i];
		    break;
		    }
     }
     
//=================================================
//****       Ðèñóåì  TD-ëèíèè                  ****
//=================================================

   buff_str = "FallingTrend";
   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 = "RisingTrend";
   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];
 
//====================================================================
//  Alert if last bar closes outside of trendline
//====================================================================

RisingTrend=ObjectGetValueByShift("RisingTrend",1);
FallingTrend=ObjectGetValueByShift("FallingTrend",1);
LastClose=iClose(NULL,0,1);
PriorRisingTrend=ObjectGetValueByShift("RisingTrend",2);
PriorFallingTrend=ObjectGetValueByShift("FallingTrend",2);
PriorLastClose=iClose(NULL,0,2);


if (NewBar()==true && (UseAlert) && LastClose<RisingTrend && PriorLastClose>=PriorRisingTrend) Alert(Symbol()+" "+Period()+": Up Trend broken @ "+DoubleToStr(RisingTrend,Digits)+" - Look to go SHORT");
if (NewBar()==true && (UseAlert) && LastClose>FallingTrend && PriorLastClose<=PriorRisingTrend) Alert(Symbol()+" "+Period()+": Down Trend broken @ "+DoubleToStr(FallingTrend,Digits)+" - Look to go LONG");

   }
  }
 
//====================================================================
//  Code End
//====================================================================


Comments