//+------------------------------------------------------------------+
//|                                        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