Author: GreenDog
Demark

This script is a technical analysis tool designed for the MetaTrader platform. It automates the identification and visualization of specific price action patterns based on DeMark Theory, a methodology used to predict potential price reversals and trend continuations.

Instead of simple moving averages or basic oscillators, this tool focuses on identifying "turning points" in the market by analyzing the relationship between current price bars and historical price extremes.

Core Logic & Functionality

1. DeMark Point Identification The engine first scans historical price data to locate specific "DeMark Highs" and "DeMark Lows." These are not just any highs or lows; they are significant peaks or troughs identified by a recursive logic that ensures the price at a specific point is higher (or lower) than its surrounding neighbors over a defined range. These points serve as the primary anchors for all subsequent calculations.

2. Dynamic Trend Line Construction Once these anchor points are found, the script automatically calculates and draws trend lines:

  • UpTrend Lines: Formed by connecting significant lows to predict future price floors.
  • DownTrend Lines: Formed by connecting significant highs to predict future price ceilings.

3. Advanced Geometric Analysis Beyond simple lines, the script performs several automated calculations to provide a comprehensive "map" of the current market structure:

  • Breakout Levels: It calculates specific horizontal price levels where a trend line might be breached, providing a clear target for potential reversals.
  • Channels: The tool can project parallel lines based on the trend's trajectory, creating a visual "corridor" that represents the current market's path of least resistance.
  • Take Profit (TP) Targets: Using mathematical proportions (specifically looking for harmonic-style ratios like 1.618), the script identifies potential "exit" zones or profit targets based on the distance between recent price action and the established trend lines.

4. Automated Breakout Detection & Classification One of the more sophisticated features of this script is its ability to analyze past behavior. It looks backward through the chart to see if a trend line was broken in the past. If it finds a breach, it doesn't just mark it; it classifies the "quality" of that break (e.g., whether the price closed decisively beyond the level or simply "wicked" through it). This helps users distinguish between a genuine trend change and a temporary price fluctuation.

Visual Output

For the user, this translates into a highly informative dashboard on the chart:

  • Color-Coded Indicators: Different trend types are assigned distinct colors for easy visual separation.
  • Real-time Commentary: The script generates a text summary on the screen that provides the current status of the trend lines, channel visibility, and specific price levels for breakouts and targets.
  • Visual Markers: It places arrows or symbols at specific points where significant breaches have occurred, allowing for quick historical review.

Summary for the Trader

Essentially, this tool removes the manual "guesswork" of drawing trend lines and finding support/resistance levels. By automating DeMark Theory, it provides a structured geometric map of the market, highlighting where trends are likely to hold, where they might break, and exactly what price levels you should watch for potential entries or exits.

Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
Demark
//------------------------------------------------------------------/
// Èíäèêàòîð-ñîâåòíèê, îñíîâàíûé íà òåîðèè Äåìàðêà ñ ìîèìè äîïîëíåíèÿìè
// ×àñòü èäåè âçÿòà èç èíäèêàòîðà Ind-TD-DeMark-3-1.mq4 [Kara ©]
//------------------------------------------------------------------/
#property copyright "GreenDog" 
#property link      "krot@inbox.ru" // v2.3.1

#property indicator_chart_window 
#property indicator_buffers 2
#property indicator_color1 Red 
#property indicator_color2 Blue 

extern int showBars=200; // åñëè = 0, òî òî÷êè äåìàðêà îòîáðàæàåòñÿ äëÿ âñåãî ãðàôèêà
extern int LevDP=3; // óðîâåíü òî÷åê äåìàðêà; 2 = öåíòðàëüíûé áàð áóäåò âûøå(íèæå) 2õ áàðîâ ñëåâà è 2õ áàðîâ ñïðàâà
extern int qSteps=1; // êîëè÷åñòâî îòîáðàæàåìûõ øàãîâ, íå áîëåå 3õ
extern int BackStep=0; // êîëè÷åñòâî øàãîâ íàçàä
extern int startBar=0; // åñëè 0, òî ðåêîìåíäàöèè äëÿ òåêóùåãî áàðà, åñëè 1 - òî äëÿ ïðåäïîëàãàåìîãî ñëåäóþùåãî áàðà
extern bool TrendLine=true; // false = ëèíèé òðåíäà íå áóäåò
extern bool HorizontLine=false; // true = òî áóäóò ïðîðèñîâàíû óðîâíè ïðîáîÿ
extern bool ChannelLine=true; // true = ñòðîèòü ïàðàëåëüíî ëèíèÿì òðåíäà êàíàëû 
extern bool TakeLines=true; // true = òî ðèñóåì óðîâíè òåéêà
extern int Trend=0; // 1 = òîëüêî äëÿ UpTrendLines, -1 = òîëüêî äëÿ DownTrendLines, 0 = äëÿ âñåõ TrendLines

double Buf1[]; 
double Buf2[]; 

string Col[6]={"Êðàñíàÿ","Ñèíÿÿ","Ðîçîâàÿ","Ãîëóáàÿ","Êîðè÷íåâàÿ","Ñàëàòíàÿ"};
int ColNum[6]={Red,DarkBlue,Coral,DodgerBlue,SaddleBrown,MediumSeaGreen};

int qBars; double qTime=0; // ïåðåìåííûå äëÿ ëèêâèäàöèè ãëþêîâ ïðè çàãðóçêå

int init() 
  {
   qBars=Bars;
   qSteps=MathMin(3,qSteps);
   int code=161; string Rem="DemarkLines © GameOver";
   IndicatorShortName(Rem); 
   SetIndexStyle(0,DRAW_ARROW); 
   SetIndexStyle(1,DRAW_ARROW); 
   SetIndexArrow(0,code); 
   SetIndexArrow(1,code); 
   SetIndexBuffer(0,Buf1); 
   SetIndexBuffer(1,Buf2); 
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexLabel(0,Rem); 
   SetIndexLabel(1,Rem); 
   return(0);
  } 

int deinit() {
   Comment("");
   ArrayInitialize(Buf1,0.0);
   ArrayInitialize(Buf2,0.0);
   for(int i=1;i<=LevDP;i++){
      ObjectDelete("HA_"+i);ObjectDelete("LA_"+i);
      ObjectDelete("HL_"+i);ObjectDelete("LL_"+i);
      ObjectDelete("HHL_"+i);ObjectDelete("HLL_"+i);
      ObjectDelete("HCL_"+i);ObjectDelete("LCL_"+i);
      for(int j=0;j<4;j++) {ObjectDelete("HTL_"+i+j);ObjectDelete("LTL_"+i+j);}
   }
}
  
int start(){
   
   if (qBars!=Bars){
      deinit(); Comment("Demark, âåðñèÿ © GameOver\nÏîäîæäèòå, èäåò çàãðóçêà áàðîâ..."); 
      Sleep(1000); qBars=Bars; qTime=0; return(0);
   }

   if (qTime==Time[0]) return(0); qTime=Time[0]; // çàïóñêàåöà òîêà íà 1ì òèêå
   if (showBars==0) showBars=Bars-LevDP; // çàïîëíèëè è îòîáðàçèëè òî÷êè äåìàðêà
   for (int cnt=showBars;cnt>LevDP;cnt--){
      Buf1[cnt]=DemHigh(cnt,LevDP);
      Buf2[cnt]=DemLow(cnt,LevDP);
   }
   
   string Comm;
   Comm="Ëèíèÿ òðåíäà["+On(TrendLine)+"]; Êàíàë ["+On(ChannelLine)+
   "]; Óðîâåíü ïðîáîÿ ["+On(HorizontLine)+"]; Öåëè ["+On(TakeLines)+"]\n";
   for(cnt=1;cnt<=qSteps;cnt++) Comm=Comm+(TDMain(cnt));
   Comm=Comm+"———— © GameOver ————";
   Comment(Comm);
   return(0); 
}

string TDMain(int Step){
   int H1,H2,L1,L2,qExt,i,col;
   double tmp,qTL,qLevel,HT[4],LT[4];
   bool isHitch;
   string Comm="»—»—» Øàã "+Step+" èç "+qSteps+" (BackStep "+BackStep+")\n",Text,Rem,qp;
   
   // äëÿ DownTrendLines
   if (Trend<=0){
      Comm=Comm+"» "+Col[Step*2-2]+" DownTrendLine ";
      col=ColNum[Step*2-2];
      H1=GetTD(Step+BackStep,Buf1);
      H2=GetNextHighTD(H1);
      qTL=(High[H2]-High[H1])/(H2-H1);
      qExt=Lowest(NULL,0,MODE_LOW,H2-H1-1,H1+1); // ëîêàëüíûé ìèíèìóì ìåæäó òî÷êàìè
      qLevel=High[H2]-qTL*(H2); if (Step+BackStep==1) qLevel=qLevel-qTL*startBar;
      if (H1<0 || H2<0) Comm=Comm+"íà ãðàôèêå íåäîñòàòî÷íî òî÷åê äëÿ ïîñòðîåíèÿ\n";
      else {
         Comm=Comm+"["+DoubleToStr(High[H2],Digits)+"»"+DoubleToStr(High[H1],Digits)+"]";
         Comm=Comm+"; Level "+DoubleToStr(qLevel,Digits);
         if (Step+BackStep==1) {
            if (startBar>0) Comm=Comm+"; Future Bar "+UpHitch(-1,qLevel);
            else Comm=Comm+"; Last Bar "+UpHitch(startBar,qLevel);
         }
         Comm=Comm+"\n";
         // Àíàëèç - áûë ëè ïðîáîé òðåíäîâîé ëèíèè
         i=H1;isHitch=false;Text="";
         while(i>0 && isHitch==false){
            tmp=High[H2]-qTL*(H2-i);
            Rem="HA_"+Step;
            if (High[i]>tmp){
               qp=UpHitch(i,tmp);
               if (qp!=""){
                  isHitch=true;
                  Text=Text+"Èñò. (óð "+DoubleToStr(tmp,Digits)+") "+qp;
                  ObjectCreate(Rem,OBJ_ARROW,0,Time[i],Low[i]-Point);
                  ObjectSet(Rem,OBJPROP_COLOR,col); ObjectSet(Rem,OBJPROP_ARROWCODE,241);
                  while(i>0){ // ïðîáîé îòìåíåí, åñëè ïîñëå ïðîáîÿ áûë íîâûé ëîó èëè çàêðûòèå íèæå
                     i--;
                     if (Low[i]<Low[qExt] || Close[i]<(Low[qExt]+(High[H1]-Low[qExt])*0.236)){
                        Text=Text+" (îòìåíåí)";
                        ObjectSet(Rem,OBJPROP_PRICE1,Low[i]-Point); ObjectSet(Rem,OBJPROP_TIME1,Time[i]); ObjectSet(Rem,OBJPROP_ARROWCODE,251);
                        break;
                     }
                  }
               }
               else { Text=Text+"Ëîæ. (óð "+DoubleToStr(tmp,Digits)+"); "; ObjectDelete(Rem);}
            }
            i--;
         }
         if (Text=="") Text="Ïðîáîÿ íå áûëî.";
         Comm=Comm+Text+"\n";
         // end analysis
         Rem="HL_"+Step; // ñîáñíî ëèíèÿ òðåíäà
         if (TrendLine){
            ObjectCreate(Rem,OBJ_TREND,0,Time[H2],High[H2],Time[H1],High[H1]);
            ObjectSet(Rem,OBJPROP_COLOR,col); ObjectSet(Rem,OBJPROP_WIDTH,3-MathMin(2,Step));
         }    
         Rem="HHL_"+Step; // óðîâåíü ïðîáîÿ ëèíèè òðåíäà
         if (HorizontLine && (Step+BackStep)==1){
            ObjectCreate(Rem,OBJ_HLINE,0,0,qLevel);
            ObjectSet(Rem,OBJPROP_COLOR,col);
         }
         Rem="HCL_"+Step; // ëèíèÿ êàíàëà
         if (ChannelLine){
            ObjectCreate(Rem,OBJ_TREND,0,Time[qExt],Low[qExt],Time[0],Low[qExt]-qTL*qExt);
            ObjectSet(Rem,OBJPROP_COLOR,col);
         }
         Rem="HTL_"+Step; // ëèíèè öåëåé
         if (TakeLines){
            HT[3]=Low[qExt]+(High[H1]-Low[qExt])*1.618-qLevel; //  äîï óðîâåíü
            HT[0]=High[H2]-qTL*(H2-qExt)-Low[qExt];
            HT[1]=High[H2]-qTL*(H2-qExt)-Close[qExt];
            qExt=Lowest(NULL,0,MODE_CLOSE,H2-H1,H1);
            HT[2]=High[H2]-qTL*(H2-qExt)-Close[qExt];
            Comm=Comm+"Öåëè: ";
            for(i=0;i<4;i++){
               qTL=NormalizeDouble(qLevel+HT[i],Digits);
               ObjectCreate(Rem+i,OBJ_HLINE,0,0,qTL,0,0); 
               ObjectSet(Rem+i,OBJPROP_STYLE,STYLE_DOT); ObjectSet(Rem+i,OBJPROP_COLOR,col);
               Comm=Comm+DoubleToStr(qTL,Digits)+" ("+DoubleToStr(HT[i]/Point,0)+"ï.) ";
             }
             Comm=Comm+"\n";
         }
      }
   }

   // äëÿ UpTrendLines
   if (Trend>=0){
      Comm=Comm+"» "+Col[Step*2-1]+" UpTrendLine ";
      col=ColNum[Step*2-1];
      L1=GetTD(Step+BackStep,Buf2);
      L2=GetNextLowTD(L1);
      qTL=(Low[L1]-Low[L2])/(L2-L1);
      qExt=Highest(NULL,0,MODE_HIGH,L2-L1-1,L1+1); // ëîêàëüíûé ìèíèìóì ìåæäó òî÷êàìè
      qLevel=Low[L2]+qTL*L2; if (Step+BackStep==1) qLevel=qLevel+qTL*startBar;
      if (L1<0 || L2<0) Comm=Comm+"íà ãðàôèêå íåäîñòàòî÷íî òî÷åê äëÿ ïîñòðîåíèÿ\n";
      else {
         Comm=Comm+"["+DoubleToStr(Low[L2],Digits)+"»"+DoubleToStr(Low[L1],Digits)+"]";
         Comm=Comm+"; Level "+DoubleToStr(qLevel,Digits);
         if (Step+BackStep==1) {
            if (startBar>0) Comm=Comm+"; Future Bar "+DownHitch(-1,qLevel);
            else Comm=Comm+"; Last Bar "+DownHitch(startBar,qLevel);
         }
         Comm=Comm+"\n";
         // Àíàëèç - áûë ëè ïðîáîé òðåíäîâîé ëèíèè
         i=L1;isHitch=false;Text="";
         while(i>0 && isHitch==false){
            tmp=Low[L2]+qTL*(L2-i);
            Rem="LA_"+Step;
            if (Low[i]<tmp){
               qp=DownHitch(i,tmp);
               if (qp!=""){
                  isHitch=true;
                  Text=Text+"Èñò. (óð "+DoubleToStr(tmp,Digits)+") "+qp;
                  ObjectCreate(Rem,OBJ_ARROW,0,Time[i],Low[i]-Point);
                  ObjectSet(Rem,OBJPROP_COLOR,col); ObjectSet(Rem,OBJPROP_ARROWCODE,242);
                  while(i>0){ // ïðîáîé îòìåíåí, åñëè ïîñëå ïðîáîÿ áûë íîâûé õàé èëè çàêðûòèå âûøå
                     i--;
                     if (High[i]>High[qExt] || Close[i]>High[qExt]-(High[qExt]-Low[L1])*0.236){
                        Text=Text+" (îòìåíåí)";
                        ObjectSet(Rem,OBJPROP_PRICE1,Low[i]-Point); ObjectSet(Rem,OBJPROP_TIME1,Time[i]); ObjectSet(Rem,OBJPROP_ARROWCODE,251);
                        break;
                     }
                  }
               }
               else { Text=Text+"Ëîæ. (óð "+DoubleToStr(tmp,Digits)+"); "; ObjectDelete(Rem);}
            }
            i--;
         }
         if (Text=="") Text="Ïðîáîÿ íå áûëî.";
         Comm=Comm+Text+"\n";
         // end analysis
         Rem="LL_"+Step; // ñîáñíî ëèíèÿ òðåíäà
         if (TrendLine==1) {
            ObjectCreate(Rem,OBJ_TREND,0,Time[L2],Low[L2],Time[L1],Low[L1]);
            ObjectSet(Rem,OBJPROP_COLOR,col); ObjectSet(Rem,OBJPROP_WIDTH,3-MathMin(2,Step));
         }    
         Rem="HLL_"+Step; // óðîâåíü ïðîáîÿ ëèíèè òðåíäà
         if (HorizontLine && (Step+BackStep)==1){
            ObjectCreate(Rem,OBJ_HLINE,0,0,qLevel);
            ObjectSet(Rem,OBJPROP_COLOR,col);
         }
         Rem="LCL_"+Step; // ëèíèÿ êàíàëà
         if (ChannelLine){
            ObjectCreate(Rem,OBJ_TREND,0,Time[qExt],High[qExt],Time[0],High[qExt]+qTL*qExt);
            ObjectSet(Rem,OBJPROP_COLOR,col);
         }
         Rem="LTL_"+Step;
         if (TakeLines){ // ëèíèè öåëåé
            LT[3]=qLevel-High[qExt]+(High[qExt]-Low[L1])*1.618; // äîï óðîâåíü
            LT[0]=High[qExt]-qTL*(L2-qExt)-Low[L2];
            LT[1]=Close[qExt]-qTL*(L2-qExt)-Low[L2];
            qExt=Highest(NULL,0,MODE_CLOSE,L2-L1,L1);
            LT[2]=Close[qExt]-qTL*(L2-qExt)-Low[L2];
            Comm=Comm+"Öåëè: ";
            for(i=0;i<4;i++){
               qTL=NormalizeDouble(qLevel-LT[i],Digits);
               ObjectCreate(Rem+i,OBJ_HLINE,0,0,qTL,0,0);
               ObjectSet(Rem+i,OBJPROP_STYLE,STYLE_DOT); ObjectSet(Rem+i,OBJPROP_COLOR,col);
               Comm=Comm+DoubleToStr(qTL,Digits)+" ("+DoubleToStr(LT[i]/Point,0)+"ï.) ";
             }
            Comm=Comm+"\n";
         }
      }
   }
   return(Comm);
}

int GetTD(int P, double Arr[]){
   int i=0,j=0;
   while(j<P){ i++; while(Arr[i]==0){i++;if(i>showBars-2)return(-1);} j++;}
   return (i);         
}
int GetNextHighTD(int P){ 
   int i=P+1;
   while(Buf1[i]<=High[P]){i++;if(i>showBars-2)return(-1);}
   return (i);
}
int GetNextLowTD(int P){
   int i=P+1;
   while(Buf2[i]>=Low[P] || Buf2[i]==0){i++;if(i>showBars-2)return(-1);}
   return (i);
}
// ðåêóðñèâíàÿ ïðîâåðêà íà óñëîâèÿ Äåìàðêà (õàé), âîçâðàùàåò çíà÷åíèå èëè 0
double DemHigh(int cnt, int sh){
   if (High[cnt]>=High[cnt+sh] && High[cnt]>High[cnt-sh]) {
      if (sh>1) return(DemHigh(cnt,sh-1));
      else return(High[cnt]);
   }
   else return(0);
}
// ðåêóðñèâíàÿ ïðîâåðêà íà óñëîâèÿ Äåìàðêà (ëîó), âîçâðàùàåò çíà÷åíèå èëè 0
double DemLow(int cnt, int sh){
   if (Low[cnt]<=Low[cnt+sh] && Low[cnt]<Low[cnt-sh]) {
      if (sh>1) return(DemLow(cnt,sh-1));
      else return(Low[cnt]);
   }
   else return(0);
}
string On(bool On){
   if (On) return("Âêë"); else return("Âûêë");
}
string UpHitch(int P, double qLevel){ // îïðåäåëåíèå êâàëèôèêàòîðîâ ïðîðûâà ââåðõ
   string Comm="";
   if (Close[P+1]<Close[P+2]) Comm=Comm+" 1";
   if (P>=0 && Open[P]>qLevel) Comm=Comm+" 2";
   if (2*Close[P+1]-Low[P+1]<qLevel) Comm=Comm+" 3";
   if (Comm!="") Comm="[ Êâ.Ïð:"+Comm+" ]";
   return(Comm);
}
string DownHitch(int P, double qLevel){ // îïðåäåëåíèå êâàëèôèêàòîðîâ ïðîðûâà âíèç
   string Comm="";
   if (Close[P+1]>Close[P+2]) Comm=Comm+" 1";
   if (P>=0 && Open[P]<qLevel) Comm=Comm+" 2";
   if (2*Close[P+1]-High[P+1]>qLevel) Comm=Comm+" 3";
   if (Comm!="") Comm="[ Êâ.Ïð:"+Comm+" ]";
   return(Comm);
}

Comments