找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 8|回复: 0

【ZIGZAG】适用于MT5程序的ZIGZAG指标,解决官方指标存在的问题

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-23 17:57:00 | 显示全部楼层 |阅读模式
最后由 wudadao 于 2025-10-23 12:43 编辑

官方ZIGZAG指标在程序调用时会出现与加载图表时值不符的情况,下面的指标源码能很好地解决这个问题

里面含有几个源码:这是其中一段

//---- author of the indicator

#property copyright "Copyright ?2005, MetaQuotes Software Corp."

//---- link to the website of the author

#property link      "http://www.metaquotes.net/"

//---- indicator version

#property version   "1.00"

#property description "ZigZag"

//+----------------------------------------------+

//|  Indicator drawing parameters                |

//+----------------------------------------------+

//---- drawing the indicator in the main window

#property indicator_chart_window

//---- 2 buffers are used for calculation of drawing of the indicator

#property indicator_buffers 2

//---- only 1 plot is used

#property indicator_plots   1

//---- ZIGZAG is used for the indicator

#property indicator_type1   DRAW_ZIGZAG

//---- displaying the indicator label

#property indicator_label1  "ZigZag_NK"

//---- use blue violet color for the indicator line

#property indicator_color1 BlueViolet

//---- the indicator line is a long dashed line

#property indicator_style1  STYLE_DASH

//---- indicator line width is equal to 1

#property indicator_width1  1

//+----------------------------------------------+

//|  Indicator input parameters                  |

//+----------------------------------------------+

input int ExtDepth=12;

input int ExtDeviation=5;

input int ExtBackstep =3;


//---- declaration of dynamic arrays that

// will be used as indicator buffers

double HighestBuffer[];

double LowestBuffer[];


//---- declaration of memory variables for recalculation of the indiator only at the previously not calculated bars

int LASTlowpos,LASThighpos;

double LASTlow0,LASTlow1,LASThigh0,LASThigh1;


//---- Declaration of the integer variables for the start of data calculation

int StartBars;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//---- initialization of variables of the start of data calculation

   StartBars=ExtDepth+ExtBackstep;


//---- set dynamic arrays as indicator buffers

   SetIndexBuffer(0,LowestBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,HighestBuffer,INDICATOR_DATA);

//---- restriction to draw empty values for the indicator

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//---- create labels to display in Data Window

   PlotIndexSetString(0,PLOT_LABEL,"ZigZag Lowest");

   PlotIndexSetString(1,PLOT_LABEL,"ZigZag Highest");

//---- indexing the elements in buffers as timeseries   

   ArraySetAsSeries(LowestBuffer,true);

   ArraySetAsSeries(HighestBuffer,true);

//---- set the position, from which the Bollinger Bands drawing starts

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBars);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBars);

//---- Setting the format of accuracy of displaying the indicator

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//---- name for the data window and the label for sub-windows

   string shortname;

   StringConcatenate(shortname,"ZigZag (ExtDepth=",

                     ExtDepth,"ExtDeviation = ",ExtDeviation,"ExtBackstep = ",ExtBackstep,")");

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//----   

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

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[])

  {

//---- checking the number of bars to be enough for the calculation

   if(rates_total<StartBars) return(0);


//---- declarations of local variables

   int limit,bar,back,lasthighpos,lastlowpos;

   double curlow,curhigh,lasthigh0=0.0,lastlow0=0.0,lasthigh1,lastlow1,val,res;


//---- calculate the limit starting number for loop of bars recalculation and start initialization of variables

   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of the indicator calculation

     {

      limit=rates_total-StartBars; // starting index for calculation of all bars


      lastlow1=-1;

      lasthigh1=-1;

      lastlowpos=-1;

      lasthighpos=-1;

     }

   else

     {

      limit=rates_total-prev_calculated; // starting index for calculation of new bars


      //---- restore values of the variables

      lastlow0=LASTlow0;

      lasthigh0=LASThigh0;


      lastlow1=LASTlow1;

      lasthigh1=LASThigh1;


      lastlowpos=LASTlowpos+limit;

      lasthighpos=LASThighpos+limit;

     }


//---- indexing elements in arrays as timeseries  

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);


//---- first big indicator calculation loop

   for(bar=limit; bar>=0 && !IsStopped(); bar--)

     {

      //---- store values of the variables before running at the current bar

      if(rates_total!=prev_calculated && bar==0)

        {

         LASTlow0=lastlow0;

         LASThigh0=lasthigh0;

        }


      //---- low

      val=low[ArrayMinimum(low,bar,ExtDepth)];

      if(val==lastlow0) val=0.0;

      else

        {

         lastlow0=val;

         if((low[bar]-val)>(ExtDeviation*_Point))val=0.0;

         else

           {

            for(back=1; back<=ExtBackstep; back++)

              {

               res=LowestBuffer[bar+back];

               if((res!=0) && (res>val))LowestBuffer[bar+back]=0.0;

              }

           }

        }

      LowestBuffer[bar]=val;


      //---- high

      val=high[ArrayMaximum(high,bar,ExtDepth)];

      if(val==lasthigh0) val=0.0;

      else

        {

         lasthigh0=val;

         if((val-high[bar])>(ExtDeviation*_Point))val=0.0;

         else

           {

            for(back=1; back<=ExtBackstep; back++)

              {

               res=HighestBuffer[bar+back];

               if((res!=0) && (res<val))HighestBuffer[bar+back]=0.0;

              }

           }

        }

      HighestBuffer[bar]=val;


     }


//---- the second big indicator calculation loop

   for(bar=limit; bar>=0; bar--)

     {

      //---- store values of the variables before running at the current bar

      if(rates_total!=prev_calculated && bar==0)

        {

         LASTlow1=lastlow1;

         LASThigh1=lasthigh1;


         LASTlowpos=lastlowpos;

         LASThighpos=lasthighpos;

        }


      curlow=LowestBuffer[bar];

      curhigh=HighestBuffer[bar];

      //----

      if((curlow==0) && (curhigh==0))continue;

      //----

      if(curhigh!=0)

        {

         if(lasthigh1>0)

           {

            if(lasthigh1<curhigh)HighestBuffer[lasthighpos]=0;

            else HighestBuffer[bar]=0;

           }

         //----

         if(lasthigh1<curhigh || lasthigh1<0)

           {

            lasthigh1=curhigh;

            lasthighpos=bar;

           }

         lastlow1=-1;

        }

      //----

      if(curlow!=0)

        {

         if(lastlow1>0)

           {

            if(lastlow1>curlow) LowestBuffer[lastlowpos]=0;

            else LowestBuffer[bar]=0;

           }

         //----

         if((curlow<lastlow1) || (lastlow1<0))

           {

            lastlow1=curlow;

            lastlowpos=bar;

           }

         lasthigh1=-1;

        }

     }

//----     

   return(rates_total);

  }

//+------------------------------------------------------------------+
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-12 17:33 , Processed in 0.102063 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表