//+------------------------------------------------------------------+ //| ZigZag.mq5 | //| Copyright 2009-2020, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009-2020, MetaQuotes Software Corp." #property link "http://www.mql5.com" #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 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- input parameters input int InpDepth =12; // Depth input int InpDeviation=5; // Deviation input int InpBackstep =3; // Back Step //--- indicator buffers double ZigZagBuffer[]; // main buffer double HighMapBuffer[]; // ZigZag high extremes (peaks) double LowMapBuffer[]; // ZigZag low extremes (bottoms) int ExtRecalc=3; // number of last extremes for recalculation enum EnSearchMode { Extremum=0, // searching for the first extremum Peak=1, // searching for the next ZigZag peak Bottom=-1 // searching for the next ZigZag bottom }; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,ZigZagBuffer,INDICATOR_DATA); SetIndexBuffer(1,HighMapBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(2,LowMapBuffer,INDICATOR_CALCULATIONS); //--- set short name and digits string short_name=StringFormat("ZigZag(%d,%d,%d)",InpDepth,InpDeviation,InpBackstep); IndicatorSetString(INDICATOR_SHORTNAME,short_name); PlotIndexSetString(0,PLOT_LABEL,short_name); IndicatorSetInteger(INDICATOR_DIGITS,_Digits); //--- set an empty value PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); } //+------------------------------------------------------------------+ //| ZigZag calculation | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(rates_total<100) return(0); //--- int i=0; int start=0,extreme_counter=0,extreme_search=Extremum; int shift=0,back=0,last_high_pos=0,last_low_pos=0; double val=0,res=0; double curlow=0,curhigh=0,last_high=0,last_low=0; //--- initializing if(prev_calculated==0) { ArrayInitialize(ZigZagBuffer,0.0); ArrayInitialize(HighMapBuffer,0.0); ArrayInitialize(LowMapBuffer,0.0); start=InpDepth; } //--- ZigZag was already calculated before if(prev_calculated>0) { i=rates_total-1; //--- searching for the third extremum from the last uncompleted bar while(extreme_counter<ExtRecalc && i>rates_total-100) { res=ZigZagBuffer[i]; if(res!=0.0) extreme_counter++; i--; } i++; start=i; //--- what type of exremum we search for if(LowMapBuffer[i]!=0.0) { curlow=LowMapBuffer[i]; extreme_search=Peak; } else { curhigh=HighMapBuffer[i]; extreme_search=Bottom; } //--- clear indicator values for(i=start+1; i<rates_total && !IsStopped(); i++) { ZigZagBuffer[i] =0.0; LowMapBuffer[i] =0.0; HighMapBuffer[i]=0.0; } } //--- searching for high and low extremes for(shift=start; shift<rates_total && !IsStopped(); shift++) { //--- low val=low[Lowest(low,InpDepth,shift)]; if(val==last_low) val=0.0; else { last_low=val; if((low[shift]-val)>InpDeviation*_Point) val=0.0; else { for(back=1; back<=InpBackstep; back++) { res=LowMapBuffer[shift-back]; if((res!=0) && (res>val)) LowMapBuffer[shift-back]=0.0; //Print("LOW"); } } } if(low[shift]==val) LowMapBuffer[shift]=val; else LowMapBuffer[shift]=0.0; //--- high val=high[Highest(high,InpDepth,shift)]; if(val==last_high) val=0.0; else { last_high=val; if((val-high[shift])>InpDeviation*_Point) val=0.0; else { for(back=1; back<=InpBackstep; back++) { res=HighMapBuffer[shift-back]; if((res!=0) && (res<val)) HighMapBuffer[shift-back]=0.0; //Print("High"); } } } if(high[shift]==val) HighMapBuffer[shift]=val; else HighMapBuffer[shift]=0.0; } //--- set last values if(extreme_search==0) // undefined values { last_low=0.0; last_high=0.0; } else { last_low=curlow; last_high=curhigh; } //--- final selection of extreme points for ZigZag for(shift=start; shift<rates_total && !IsStopped(); shift++) { res=0.0; switch(extreme_search) { case Extremum: if(last_low==0.0 && last_high==0.0) { if(HighMapBuffer[shift]!=0) { last_high=high[shift]; last_high_pos=shift; extreme_search=Bottom; ZigZagBuffer[shift]=last_high; res=1; //Print("LOW"); } if(LowMapBuffer[shift]!=0.0) { last_low=low[shift]; last_low_pos=shift; extreme_search=Peak; ZigZagBuffer[shift]=last_low; res=1; //Print("HIGH"); } } break; case Peak: if(LowMapBuffer[shift]!=0.0 && LowMapBuffer[shift]<last_low && HighMapBuffer[shift]==0.0) { ZigZagBuffer[last_low_pos]=0.0; last_low_pos=shift; last_low=LowMapBuffer[shift]; ZigZagBuffer[shift]=last_low; res=1; //Print("LOW"); } if(HighMapBuffer[shift]!=0.0 && LowMapBuffer[shift]==0.0) { last_high=HighMapBuffer[shift]; last_high_pos=shift; ZigZagBuffer[shift]=last_high; extreme_search=Bottom; res=1; //Print("LOW"); } break; case Bottom: if(HighMapBuffer[shift]!=0.0 && HighMapBuffer[shift]>last_high && LowMapBuffer[shift]==0.0) { ZigZagBuffer[last_high_pos]=0.0; last_high_pos=shift; last_high=HighMapBuffer[shift]; ZigZagBuffer[shift]=last_high; //Print("HIGH"); } if(LowMapBuffer[shift]!=0.0 && HighMapBuffer[shift]==0.0) { last_low=LowMapBuffer[shift]; last_low_pos=shift; ZigZagBuffer[shift]=last_low; extreme_search=Peak; //Print("HIGH"); } break; default: return(rates_total); } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Search for the index of the highest bar | //+------------------------------------------------------------------+ int Highest(const double &array[],const int depth,const int start) { if(start<0) return(0); double max=array[start]; int index=start; //--- start searching for(int i=start-1; i>start-depth && i>=0; i--) { if(array[i]>max) { index=i; max=array[i]; } } //--- return index of the highest bar return(index); } //+------------------------------------------------------------------+ //| Search for the index of the lowest bar | //+------------------------------------------------------------------+ int Lowest(const double &array[],const int depth,const int start) { if(start<0) return(0); double min=array[start]; int index=start; //--- start searching for(int i=start-1; i>start-depth && i>=0; i--) { if(array[i]<min) { index=i; min=array[i]; } } //--- return index of the lowest bar return(index); } //+------------------------------------------------------------------+
Codigo do indicador ZigZag
//+------------------------------------------------------------------+ //| ZigzagColor.mq5 | //| Copyright 2009-2020, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009-2020, MetaQuotes Software Corp." #property link "http://www.mql5.com" //--- indicator settings #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 1 #property indicator_type1 DRAW_COLOR_ZIGZAG #property indicator_color1 clrDodgerBlue, clrRed //--- input parameters input int InpDepth =12; // Depth input int InpDeviation=5; // Deviation input int InpBackstep =3; // Back Step //--- indicator buffers double ZigzagPeakBuffer[]; double ZigzagBottomBuffer[]; double HighMapBuffer[]; double LowMapBuffer[]; double ColorBuffer[]; int ExtRecalc=3; // recounting's depth enum EnSearchMode { Extremum=0, // searching for the first extremum Peak=1, // searching for the next ZigZag peak Bottom=-1 // searching for the next ZigZag bottom }; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,ZigzagPeakBuffer,INDICATOR_DATA); SetIndexBuffer(1,ZigzagBottomBuffer,INDICATOR_DATA); SetIndexBuffer(2,ColorBuffer,INDICATOR_COLOR_INDEX); SetIndexBuffer(3,HighMapBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(4,LowMapBuffer,INDICATOR_CALCULATIONS); //--- set accuracy IndicatorSetInteger(INDICATOR_DIGITS,_Digits); //--- name for DataWindow and indicator subwindow label string short_name=StringFormat("ZigZagColor(%d,%d,%d)",InpDepth,InpDeviation,InpBackstep); IndicatorSetString(INDICATOR_SHORTNAME,short_name); PlotIndexSetString(0,PLOT_LABEL,short_name); //--- set an empty value PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); } //+------------------------------------------------------------------+ //| ZigZag calculation | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(rates_total<100) return(0); //--- int i,start=0; int extreme_counter=0,extreme_search=Extremum; int shift,back=0,last_high_pos=0,last_low_pos=0; double val=0,res=0; double cur_low=0,cur_high=0,last_high=0,last_low=0; //--- initializing if(prev_calculated==0) { ArrayInitialize(ZigzagPeakBuffer,0.0); ArrayInitialize(ZigzagBottomBuffer,0.0); ArrayInitialize(HighMapBuffer,0.0); ArrayInitialize(LowMapBuffer,0.0); //--- start calculation from bar number InpDepth start=InpDepth-1; } //--- ZigZag was already calculated before if(prev_calculated>0) { i=rates_total-1; //--- searching for the third extremum from the last uncompleted bar while(extreme_counter<ExtRecalc && i>rates_total -100) { res=(ZigzagPeakBuffer[i]+ZigzagBottomBuffer[i]); //--- if(res!=0) extreme_counter++; i--; } i++; start=i; //--- what type of exremum we search for if(LowMapBuffer[i]!=0) { cur_low=LowMapBuffer[i]; extreme_search=Peak; } else { cur_high=HighMapBuffer[i]; extreme_search=Bottom; } //--- clear indicator values for(i=start+1; i<rates_total && !IsStopped(); i++) { ZigzagPeakBuffer[i] =0.0; ZigzagBottomBuffer[i]=0.0; LowMapBuffer[i] =0.0; HighMapBuffer[i] =0.0; } } //--- searching for high and low extremes for(shift=start; shift<rates_total && !IsStopped(); shift++) { //--- low val=Lowest(low,InpDepth,shift); if(val==last_low) val=0.0; else { last_low=val; if((low[shift]-val)>(InpDeviation*_Point)) val=0.0; else { for(back=InpBackstep; back>=1; back--) { res=LowMapBuffer[shift-back]; //--- if((res!=0) && (res>val)) LowMapBuffer[shift-back]=0.0; //Print("Low"); } } } if(low[shift]==val) LowMapBuffer[shift]=val; else LowMapBuffer[shift]=0.0; //--- high val=Highest(high,InpDepth,shift); if(val==last_high) val=0.0; else { last_high=val; if((val-high[shift])>(InpDeviation*_Point)) val=0.0; else { for(back=InpBackstep; back>=1; back--) { res=HighMapBuffer[shift-back]; //--- if((res!=0) && (res<val)) HighMapBuffer[shift-back]=0.0; //Print("High"); } } } if(high[shift]==val) HighMapBuffer[shift]=val; else HighMapBuffer[shift]=0.0; } //--- set last values if(extreme_search==0) // undefined values { last_low=0; last_high=0; } else { last_low=cur_low; last_high=cur_high; } //--- final selection of extreme points for ZigZag for(shift=start; shift<rates_total && !IsStopped(); shift++) { res=0.0; switch(extreme_search) { case Extremum: if(last_low==0 && last_high==0) { if(HighMapBuffer[shift]!=0) { last_high=high[shift]; last_high_pos=shift; extreme_search=-1; ZigzagPeakBuffer[shift]=last_high; ColorBuffer[shift]=0; //Print("HIGH"); res=1; } if(LowMapBuffer[shift]!=0) { last_low=low[shift]; last_low_pos=shift; extreme_search=1; ZigzagBottomBuffer[shift]=last_low; ColorBuffer[shift]=1; //Print("LOW"); res=1; } } break; case Peak: if(LowMapBuffer[shift]!=0.0 && LowMapBuffer[shift]<last_low && HighMapBuffer[shift]==0.0) { ZigzagBottomBuffer[last_low_pos]=0.0; last_low_pos=shift; last_low=LowMapBuffer[shift]; ZigzagBottomBuffer[shift]=last_low; ColorBuffer[shift]=1; //Print("LOW"); res=1; } if(HighMapBuffer[shift]!=0.0 && LowMapBuffer[shift]==0.0) { last_high=HighMapBuffer[shift]; last_high_pos=shift; ZigzagPeakBuffer[shift]=last_high; ColorBuffer[shift]=0; //Print("LOW"); extreme_search=Bottom; res=1; } break; case Bottom: if(HighMapBuffer[shift]!=0.0 && HighMapBuffer[shift]>last_high && LowMapBuffer[shift]==0.0) { ZigzagPeakBuffer[last_high_pos]=0.0; last_high_pos=shift; last_high=HighMapBuffer[shift]; ZigzagPeakBuffer[shift]=last_high; ColorBuffer[shift]=0; //Print("HIGH"); } if(LowMapBuffer[shift]!=0.0 && HighMapBuffer[shift]==0.0) { last_low=LowMapBuffer[shift]; last_low_pos=shift; ZigzagBottomBuffer[shift]=last_low; ColorBuffer[shift]=1; //Print("HIGH"); extreme_search=Peak; } break; default: return(rates_total); } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Get highest value for range | //+------------------------------------------------------------------+ double Highest(const double&array[],int count,int start) { double res=array[start]; //--- for(int i=start-1; i>start-count && i>=0; i--) if(res<array[i]) res=array[i]; //Print("HIGH"); //--- return(res); } //+------------------------------------------------------------------+ //| Get lowest value for range | //+------------------------------------------------------------------+ double Lowest(const double&array[],int count,int start) { double res=array[start]; //--- for(int i=start-1; i>start-count && i>=0; i--) if(res>array[i]) res=array[i]; //Print("LOW"); //--- return(res); } //+------------------------------------------------------------------+
Codigo do indicado ZigZag Color
Você está perdendo oportunidades de negociação:
- Aplicativos de negociação gratuitos
- 8 000+ sinais para cópia
- Notícias econômicas para análise dos mercados financeiros
Registro
Login
Você concorda com a política do site e com os termos de uso
Se você não tem uma conta, por favor registre-se
Olá, estou tentando criar uma estratégia baseada no indicador zigzag color. O indicador zigzag plota linhas diagonais crescentes e decrescentes conforme o movimento do gráfico e dos parâmetros prè selecionados.
Gostaria que meu programa realizasse uma compra quando a linha vermelha (zigzag color) ou linha decrescente (zigzag) aparecesse no gráfico e uma venda somente quando a linha verde (zigzag color) ou a linha crescente (zigzag) aparecesse.
Dito isso, ainda não consegui puxar o Buffer de cor!
Poderiam me ajudar ?