//+------------------------------------------------------------------+ //| ZigZagClose.mq5 | //| Lucas | //+------------------------------------------------------------------+ #property copyright "Lucas" #property version "1.00" #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 1 //---- plot Zigzag #property indicator_label1 "Zigzag" #property indicator_type1 DRAW_SECTION #property indicator_color1 Red #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- input parameters input int ExtDepth=12; input int ExtDeviation=5; input int ExtBackstep=3; //--- indicator buffers double ZigzagBuffer[]; // main buffer double HighMapBuffer[]; // highs double LowMapBuffer[]; // lows int level=3; // recounting depth double deviation; // deviation in points //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,ZigzagBuffer,INDICATOR_DATA); SetIndexBuffer(1,HighMapBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(2,LowMapBuffer,INDICATOR_CALCULATIONS); //--- set short name and digits PlotIndexSetString(0,PLOT_LABEL,"ZigZag("+(string)ExtDepth+","+(string)ExtDeviation+","+(string)ExtBackstep+")"); IndicatorSetInteger(INDICATOR_DIGITS,_Digits); //--- set empty value PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); //--- to use in cycle deviation=ExtDeviation*_Point; //--- return(0); } //+------------------------------------------------------------------+ //| searching index of the highest bar | //+------------------------------------------------------------------+ int iHighest(const double &array[], int depth, int startPos) { int index=startPos; //--- start index validation if(startPos<0) { Print("Invalid parameter in the function iHighest, startPos =",startPos); return 0; } int size=ArraySize(array); //--- depth correction if need if(startPos-depth<0) depth=startPos; double max=array[startPos]; //--- start searching for(int i=startPos;i>startPos-depth;i--) { if(array[i]>max) { index=i; max=array[i]; } } //--- return index of the highest bar return(index); } //+------------------------------------------------------------------+ //| searching index of the lowest bar | //+------------------------------------------------------------------+ int iLowest(const double &array[], int depth, int startPos) { int index=startPos; //--- start index validation if(startPos<0) { Print("Invalid parameter in the function iLowest, startPos =",startPos); return 0; } int size=ArraySize(array); //--- depth correction if need if(startPos-depth<0) depth=startPos; double min=array[startPos]; //--- start searching for(int i=startPos;i>startPos-depth;i--) { if(array[i]0) { i=rates_total-1; //--- searching third extremum from the last uncompleted bar while(counterZrates_total-100) { res=ZigzagBuffer[i]; if(res!=0) counterZ++; i--; } i++; limit=i; //--- what type of exremum we are going to find if(LowMapBuffer[i]!=0) { curlow=LowMapBuffer[i]; whatlookfor=Pike; } else { curhigh=HighMapBuffer[i]; whatlookfor=Sill; } //--- chipping for(i=limit+1;ideviation) val=0.0; else { for(back=1;back<=ExtBackstep;back++) { res=LowMapBuffer[shift-back]; if((res!=0) && (res>val)) LowMapBuffer[shift-back]=0.0; } } } if(close[shift]==val) LowMapBuffer[shift]=val; else LowMapBuffer[shift]=0.0; //--- high val=close[iHighest(close,ExtDepth,shift)]; if(val==lasthigh) val=0.0; else { lasthigh=val; if((val-close[shift])>deviation) val=0.0; else { for(back=1;back<=ExtBackstep;back++) { res=HighMapBuffer[shift-back]; if((res!=0) && (reslasthigh && LowMapBuffer[shift]==0.0) { ZigzagBuffer[lasthighpos]=0.0; lasthighpos=shift; lasthigh=HighMapBuffer[shift]; ZigzagBuffer[shift]=lasthigh; } if(LowMapBuffer[shift]!=0.0 && HighMapBuffer[shift]==0.0) { lastlow=LowMapBuffer[shift]; lastlowpos=shift; ZigzagBuffer[shift]=lastlow; whatlookfor=Pike; } break; default: return(rates_total); } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+