Time: 2017-04-01 | Download file:LabTrendZigZag_v1.mq4
//+------------------------------------------------------------------+ //| LabTrendZigZag_v1.mq4 | //| Copyright © 2008, TrendLaboratory | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //| E-mail: igorad2003@yahoo.co.uk | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, TrendLaboratory" #property link "http://finance.groups.yahoo.com/group/TrendLaboratory" #property indicator_chart_window #property indicator_buffers 1 #property indicator_color1 Gold #property indicator_width1 2 //---- input parameters extern double Risk = 3; //Price Channel narrowing factor (1..10) extern int PriceMode = 0; //Price Mode: 0-High/Low, 1-Close double ZZBuffer[]; double Hi[]; double Lo[]; double trend[]; int ilow, ihigh, nlow, nhigh, prevnhigh,prevnlow, BarsBack, Length; datetime lotime,hitime; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { Length = 3 + 2*Risk; IndicatorBuffers(4); SetIndexStyle(0,DRAW_SECTION); SetIndexBuffer(0,ZZBuffer); SetIndexBuffer(1,Hi); SetIndexBuffer(2,Lo); SetIndexBuffer(3,trend); string short_name; //---- indicator line IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)); //---- name for DataWindow and indicator subwindow label short_name="LabTrendZigZag("+Risk+")"; IndicatorShortName(short_name); SetIndexLabel(0,"LabTrendZigZag"); //---- SetIndexEmptyValue(0,0.0); SetIndexDrawBegin(0,Length); return(0); } //+------------------------------------------------------------------+ //| LabTrendZigZag_v1 | //+------------------------------------------------------------------+ int start() { int i,shift, counted_bars=IndicatorCounted(),limit; double smin,smax; if ( counted_bars > 0 ) limit=Bars-counted_bars; if ( counted_bars < 0 ) return(0); if ( counted_bars ==0 ) limit=Bars-1; if ( counted_bars < 1 ) { for(i=0;i=0;shift--) { smax = High[iHighest(NULL,0,MODE_HIGH,Length,shift)]; smin = Low[iLowest(NULL,0,MODE_LOW,Length,shift)]; Hi[shift]=smax-(smax-smin)*(33.0-Risk)/100.0; Lo[shift]=smin+(smax-smin)*(33.0-Risk)/100.0; trend[shift]=trend[shift+1]; if (Close[shift] > Hi[shift] && trend[shift+1] <= 0) trend[shift]= 1; if (Close[shift] < Lo[shift] && trend[shift+1] >= 0) trend[shift]=-1; if(trend[shift]>0) { if( trend[shift]!=trend[shift+1]) { ilow = LowestBar(iBarShift(NULL,0,hitime,FALSE)-shift,shift); lotime = Time[ilow]; if(PriceMode==0) ZZBuffer[ilow] = Low[ilow]; else ZZBuffer[ilow] = MathMin(Close[ilow],Open[ilow]); } else if (shift==0) { int hilen = iBarShift(NULL,0,lotime,FALSE); nhigh = HighestBar(hilen,0); if(PriceMode==0) ZZBuffer[nhigh] = High[nhigh]; else ZZBuffer[nhigh]=MathMax(Close[nhigh],Open[nhigh]); if (nhigh== 0) for (i=hilen-1;i>=1;i--) ZZBuffer[i]=0; if (nhigh > 0) for (i=nhigh-1;i>=0;i--) ZZBuffer[i]=0; } } if (trend[shift]<0) { if( trend[shift]!=trend[shift+1]) { ihigh = HighestBar(iBarShift(NULL,0,lotime,FALSE)-shift,shift); hitime=Time[ihigh]; if(PriceMode==0) ZZBuffer[ihigh] = High[ihigh]; else ZZBuffer[ihigh] = MathMax(Close[ihigh],Open[ihigh]); } else if (shift==0) { int lolen = iBarShift(NULL,0,hitime,FALSE); nlow = LowestBar(lolen,0); if(PriceMode==0) ZZBuffer[nlow] = Low[nlow]; else ZZBuffer[nlow] = MathMin(Close[nlow],Open[nlow]); if (nlow==0) for (i=lolen-1;i>=1;i--) ZZBuffer[i]=0; if (nlow >0) for (i=nlow-1;i>=0;i--) ZZBuffer[i]=0; } } } return(0); } int LowestBar(int len,int k) { double min = 10000000; int lobar; for (int i=k+len-1;i>=k;i--) { if(PriceMode==0) { if(i==0) { if(Low[i] < min) { min = Low[i]; lobar = i; } } else { if(Low[i] < min && Low[i] < Low[i-1]) { min = Low[i]; lobar = i; } } } else { if(i==0) { if(MathMin(Close[i],Open[i]) < min) { min = MathMin(Close[i],Open[i]); lobar = i; } } else { if(MathMin(Close[i],Open[i]) < min && MathMin(Close[i],Open[i]) < MathMin(Close[i-1],Open[i-1])) { min = MathMin(Close[i],Open[i]); lobar = i; } } } } if(len<=0) lobar=k; return(lobar); } int HighestBar(int len,int k) { double max = -10000000; int hibar; for (int i=k+len-1;i>=k;i--) { if(PriceMode==0) { if(i==0) { if(High[i] > max) { max = High[i]; hibar = i; } } else { if(High[i] > max && High[i] > High[i-1]) { max = High[i]; hibar = i; } } } else { if(i==0) { if(MathMax(Close[i],Open[i]) > max && MathMax(Close[i],Open[i]) > MathMax(Close[i-1],Open[i-1])) { max = MathMax(Close[i],Open[i]); hibar = i; } } else { if(MathMax(Close[i],Open[i]) > max) { max = MathMax(Close[i],Open[i]); hibar = i; } } } } if(len<=0) hibar=k; return(hibar); }