# 求mt5获取zig指标的n个高低点，mt4代码求改写

//+------------------------------------------------------------------+
//| test_ZigZag.mq4 |
//| https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
#property version "1.00"
#property strict

extern int ExtDepth=8;
extern int ExtDeviation=5;
extern int ExtBackstep=3;

struct ZigZag
{
int shift;//K线位置
int flag;//ZZ是高点还是低点，1是高点，-1是低点
double value;//ZZ的值
};

#define ZZ_NUM 16 // 找最近16个ZZ拐点
ZigZag ZZ[ZZ_NUM]; // 找最近16个ZZ拐点
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---

GetZigZag(ExtDepth,ExtDeviation,ExtDeviation);//获得16个拐点
for(int i=0; i<ZZ_NUM; i++)
{
int flag = ZZ[i].flag;
if(flag==1)//是个高点
{
int k = ZZ[i].shift; //拐点对应的K线的位置
//---
double open = Open[k];//拐点开盘价
double close = Close[k];//拐点收盘价
}
}
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void GetZigZag(int depth,int deviation,int backstep)
{
int k=0;
for(int i=0; i<Bars; i++)
{
double z = iCustom(NULL,0,"ZigZag",depth,deviation,backstep,0,i);
if(z>0&&z!=EMPTY_VALUE) //是个拐点
{
if(z==High[i])//是个高点
{
ZZ[k].value = z;//拐点值
ZZ[k].shift = i;//K线位置
ZZ[k].flag = 1;//1 表示高点
}
else if(z==Low[i])//是个低点
{
ZZ[k].value = z;//拐点值
ZZ[k].shift = i;//K线位置
ZZ[k].flag = -1;// -1 表示低点
}
k++;
if(k>=ZZ_NUM)break;
}
}
}
//+------------------------------------------------------------------+
Ziheng Zhuang
• www.mql5.com

```//+------------------------------------------------------------------+
//|                                                  test_ZigZag.mq5 |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
#property version   "1.00"

input int      ExtDepth =12;
input int      ExtDeviation=5;
input int      ExtBackstep=3;

struct ZigZag
{
int               shift;//K线位置
int               flag;//ZZ是高点还是低点，1是高点，-1是低点
double            value;//ZZ的值
};

#define  ZZ_NUM   16    // 找最近16个ZZ拐点
ZigZag ZZ[ZZ_NUM];      // 找最近16个ZZ拐点

int handle = -1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
handle = iCustom(Symbol(),PERIOD_CURRENT,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep);
if(handle==-1)
{
int error = GetLastError();
printf("create ZigZag handle failed, error code =  %d",error);
return(INIT_FAILED);
}

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(!GetZigZag(ExtDepth,ExtDeviation,ExtDeviation))return;
static int cnt =0;
if(cnt++==0)
{
for(int i=0; i<ZZ_NUM; i++)
{
int shift = ZZ[i].shift;
datetime ot = iTime(Symbol(),PERIOD_CURRENT,shift);
printf("shift = %d,time = %s, value = %s",shift,(string)ot,DoubleToString(ZZ[i].value,_Digits));
}
}

}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool GetZigZag(int depth,int deviation,int backstep)
{
double z=0,v[1]= {0};
int total = iBars(Symbol(),PERIOD_CURRENT);

int k=0;
int i;
for(i=0; i<total; i++)
{
if(CopyBuffer(handle,0,i,1,v)!=1)return(false);
z = v[0];
if(z>0&&z!=EMPTY_VALUE) //是个拐点
{
if(z==iHigh(Symbol(),PERIOD_CURRENT,i))//是个高点
{
ZZ[k].value = z;//拐点值
ZZ[k].shift = i;//K线位置
ZZ[k].flag = 1;//1 表示高点
}
else if(z==iLow(Symbol(),PERIOD_CURRENT,i))//是个低点
{
ZZ[k].value = z;//拐点值
ZZ[k].shift = i;//K线位置
ZZ[k].flag = -1;// -1 表示低点
}
k++;
if(k>=ZZ_NUM)break;
}
}

return(k==ZZ_NUM);
}
//+------------------------------------------------------------------+
```