下载MetaTrader 5
要添加评论,请登录注册
学习MQL5?开始使用文档吧!
魏冠中
6
魏冠中 2011.07.18 13:16 
//+------------------------------------------------------------------+
//| Zigzag.mq4 |
//| Copyright ?2005-2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 DarkBlue
#property indicator_color3 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 1
//---- indicator parameters
//---- indicator buffers
double Zig[];
double Zag[];
double Zigzagbuffer[];
double Highmapbuffer[];
double Lowmapbuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(5);

SetIndexStyle(0,DRAW_HISTOGRAM,0,3,indicator_color1);
SetIndexBuffer(0,Zig);
SetIndexStyle(1,DRAW_HISTOGRAM,0,3,indicator_color2);
SetIndexBuffer(1,Zag);
//----
SetIndexStyle(2,DRAW_SECTION);
SetIndexBuffer(0,Zig);
SetIndexBuffer(1,Zag);
SetIndexBuffer(2,Zigzagbuffer);
SetIndexBuffer(3,Highmapbuffer);
SetIndexBuffer(4,Lowmapbuffer);
SetIndexEmptyValue(2,0.0);
//----
SetIndexDrawBegin(0,0);
SetIndexDrawBegin(1,0);
//---- indicator buffers mapping
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int i=Bars, counted_bars = IndicatorCounted();
while(i>=0){Zig[i]=iCustom(NULL, 0, "分形", 0, i); Zag[i]=iCustom(NULL, 0, "分形", 1, i); i--;}

int shift;
for(shift=Bars-2;shift>=0;shift--)
{
if(Zig[shift]>=Zig[shift-1] && Zag[shift]<=Zag[shift-1])
{Zig[shift]=0.0;
Zag[shift]=0.0;}
if(Zig[shift]<=Zig[shift-1] && Zag[shift]>=Zag[shift-1])
{Zig[shift]=0.0;
Zag[shift]=0.0;}
}

bool downloadhistory=false;
int limit,j,counterZ,kof,whatlookfor,lastlowpos,lasthighpos;
double res,curlow,curhigh,lastlow,lasthigh;


if(shift<=0 && downloadhistory==false)
{
limit=Bars-1;
downloadhistory=true;
ArrayInitialize(Zigzagbuffer,0.0);
ArrayInitialize(Lowmapbuffer,0.0);
ArrayInitialize(Highmapbuffer,0.0);
}

if(downloadhistory==true && j<100)
{
//---1
while(counterZ<3 && j<100)
{
res=Zigzagbuffer[j];
if(res!=0) counterZ++;
j++;
}
j--;
limit=j;
if(Lowmapbuffer[j]!=0)
{
curlow=Lowmapbuffer[j];
whatlookfor=1;
}
else
{
curhigh=Highmapbuffer[j];
whatlookfor=-1;
}
for(j=limit-1;j>=0;j--)
{
Zigzagbuffer[j]=0.0;
Highmapbuffer[j]=0.0;
Lowmapbuffer[j]=0.0;
}
}
//---2
int counterX;
int k;
for(kof=limit;kof>=0;kof--)
{
if(Zig[kof]!=0.0 && Zag[kof]!=0.0)
{
//---High
for(k=1,counterX=0;counterX<=4;k++)
{
if(counterX>=4)
{
Highmapbuffer[kof]=Zig[kof];
counterX++;
}
else
{
if(Zig[kof]>Zig[kof+k] && Zig[kof+k]!=0)
{
counterX++;
}
}
if(Zig[kof]<Zig[kof+k])
{
counterX=counterX+10;
Highmapbuffer[kof]=0.0;
}
}
//---low
for(k=1,counterX=0;counterX<=4;k++)
{
if(counterX>=4)
{
Lowmapbuffer[kof]=Zag[kof];
counterX++;
}
else
{
if(Zag[kof]<Zag[kof+k] && Zag[kof+k]!=0)
{
counterX++;
}
}
if(Zag[kof]>Zag[kof+k])
{
counterX=counterX+10;
Lowmapbuffer[kof]=0.0;
}
}

}
}

//--- final
if(whatlookfor==0)
{
lasthigh=0;
lastlow=0;
}
else
{
lastlow=curlow;
lasthigh=curhigh;
}
for(kof=limit;kof>=0;kof--)
{
res=0.0;
switch(whatlookfor)
{case 0:
if(lastlow==0 && lasthigh==0)
{
if(Highmapbuffer[kof]!=0)
{
if(Zig[kof]!=0) lasthigh=Zig[kof];
else lasthigh=High[kof];
lasthighpos=kof;
whatlookfor=-1;
Zigzagbuffer[kof]=lasthigh;
res=1;
}
if(Lowmapbuffer[kof]!=0)
{
if(Zag[kof]!=0) lastlow=Zag[kof];
else lastlow=Low[kof];
lastlowpos=kof;
whatlookfor=1;
Zigzagbuffer[kof]=lastlow;
res=1;
}
}
break;

case 1:
if(Highmapbuffer[kof]!=0.0)
{
for(k=1,counterX=0;counterX<=4 && kof+k<lastlowpos;k++)
{
if(Zig[kof+k]!=0) counterX++;
if(counterX>=3)
{
Highmapbuffer[kof]=lasthigh;
lasthighpos=kof;
whatlookfor=-1;
Zigzagbuffer[kof]=lasthigh;
res=1;
counterX=counterX+3;
}
}

}
if(Lowmapbuffer[kof]!=0.0 && Lowmapbuffer[kof]<lastlow)
{
Lowmapbuffer[lastlowpos]=0.0;
lastlow=Lowmapbuffer[kof];
Zigzagbuffer[lastlowpos]=0.0;
Zigzagbuffer[kof]=lastlow;
lastlowpos=kof;
whatlookfor=1;
res=1;
}
break;

case -1:
if(Lowmapbuffer[kof]!=0.0)
{
for(k=1,counterX=0;counterX<=4 && kof+k<lasthighpos;k++)
{
if(Zag[kof+k]!=0) counterX++;
if(counterX>=3)
{
Lowmapbuffer[kof]=lastlow;
lastlowpos=kof;
whatlookfor=1;
Zigzagbuffer[kof]=lastlow;
res=1;
counterX=counterX+3;
}
}

}
if(Highmapbuffer[kof]!=0.0 && Highmapbuffer[kof]>lasthigh)
{
Highmapbuffer[lasthighpos]=0.0;
lasthigh=Highmapbuffer[kof];
Zigzagbuffer[lasthighpos]=0.0;
Zigzagbuffer[kof]=lasthigh;
lasthighpos=kof;
whatlookfor=-1;
res=1;
}
break;
default:return;
}
}


return(0);
}
魏冠中
6
魏冠中 2011.07.18 13:16  

这里是上个指标引用的指标的代码



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

//| Zigzag.mq4 |
//| Copyright ?2005-2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 DarkBlue
#property indicator_color3 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 1
//---- indicator parameters
//---- indicator buffers
double Zig[];
double Zag[];
double Zigzagbuffer[];
double Highmapbuffer[];
double Lowmapbuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(5);

SetIndexStyle(0,DRAW_HISTOGRAM,0,3,indicator_color1);
SetIndexBuffer(0,Zig);
SetIndexStyle(1,DRAW_HISTOGRAM,0,3,indicator_color2);
SetIndexBuffer(1,Zag);
//----
SetIndexStyle(2,DRAW_SECTION);
SetIndexBuffer(0,Zig);
SetIndexBuffer(1,Zag);
SetIndexBuffer(2,Zigzagbuffer);
SetIndexBuffer(3,Highmapbuffer);
SetIndexBuffer(4,Lowmapbuffer);
SetIndexEmptyValue(2,0.0);
//----
SetIndexDrawBegin(0,0);
SetIndexDrawBegin(1,0);
//---- indicator buffers mapping
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int i=Bars, counted_bars = IndicatorCounted();
while(i>=0){Zig[i]=iCustom(NULL, 0, "分形", 0, i); Zag[i]=iCustom(NULL, 0, "分形", 1, i); i--;}

int shift;
for(shift=Bars-2;shift>=0;shift--)
{
if(Zig[shift]>=Zig[shift-1] && Zag[shift]<=Zag[shift-1])
{Zig[shift]=0.0;
Zag[shift]=0.0;}
if(Zig[shift]<=Zig[shift-1] && Zag[shift]>=Zag[shift-1])
{Zig[shift]=0.0;
Zag[shift]=0.0;}
}

bool downloadhistory=false;
int limit,j,counterZ,kof,whatlookfor,lastlowpos,lasthighpos;
double res,curlow,curhigh,lastlow,lasthigh;


if(shift<=0 && downloadhistory==false)
{
limit=Bars-1;
downloadhistory=true;
ArrayInitialize(Zigzagbuffer,0.0);
ArrayInitialize(Lowmapbuffer,0.0);
ArrayInitialize(Highmapbuffer,0.0);
}

if(downloadhistory==true && j<100)
{
//---1
while(counterZ<3 && j<100)
{
res=Zigzagbuffer[j];
if(res!=0) counterZ++;
j++;
}
j--;
limit=j;
if(Lowmapbuffer[j]!=0)
{
curlow=Lowmapbuffer[j];
whatlookfor=1;
}
else
{
curhigh=Highmapbuffer[j];
whatlookfor=-1;
}
for(j=limit-1;j>=0;j--)
{
Zigzagbuffer[j]=0.0;
Highmapbuffer[j]=0.0;
Lowmapbuffer[j]=0.0;
}
}
//---2
int counterX;
int k;
for(kof=limit;kof>=0;kof--)
{
if(Zig[kof]!=0.0 && Zag[kof]!=0.0)
{
//---High
for(k=1,counterX=0;counterX<=4;k++)
{
if(counterX>=4)
{
Highmapbuffer[kof]=Zig[kof];
counterX++;
}
else
{
if(Zig[kof]>Zig[kof+k] && Zig[kof+k]!=0)
{
counterX++;
}
}
if(Zig[kof]<Zig[kof+k])
{
counterX=counterX+10;
Highmapbuffer[kof]=0.0;
}
}
//---low
for(k=1,counterX=0;counterX<=4;k++)
{
if(counterX>=4)
{
Lowmapbuffer[kof]=Zag[kof];
counterX++;
}
else
{
if(Zag[kof]<Zag[kof+k] && Zag[kof+k]!=0)
{
counterX++;
}
}
if(Zag[kof]>Zag[kof+k])
{
counterX=counterX+10;
Lowmapbuffer[kof]=0.0;
}
}

}
}

//--- final
if(whatlookfor==0)
{
lasthigh=0;
lastlow=0;
}
else
{
lastlow=curlow;
lasthigh=curhigh;
}
for(kof=limit;kof>=0;kof--)
{
res=0.0;
switch(whatlookfor)
{case 0:
if(lastlow==0 && lasthigh==0)
{
if(Highmapbuffer[kof]!=0)
{
if(Zig[kof]!=0) lasthigh=Zig[kof];
else lasthigh=High[kof];
lasthighpos=kof;
whatlookfor=-1;
Zigzagbuffer[kof]=lasthigh;
res=1;
}
if(Lowmapbuffer[kof]!=0)
{
if(Zag[kof]!=0) lastlow=Zag[kof];
else lastlow=Low[kof];
lastlowpos=kof;
whatlookfor=1;
Zigzagbuffer[kof]=lastlow;
res=1;
}
}
break;

case 1:
if(Highmapbuffer[kof]!=0.0)
{
for(k=1,counterX=0;counterX<=4 && kof+k<lastlowpos;k++)
{
if(Zig[kof+k]!=0) counterX++;
if(counterX>=3)
{
Highmapbuffer[kof]=lasthigh;
lasthighpos=kof;
whatlookfor=-1;
Zigzagbuffer[kof]=lasthigh;
res=1;
counterX=counterX+3;
}
}

}
if(Lowmapbuffer[kof]!=0.0 && Lowmapbuffer[kof]<lastlow)
{
Lowmapbuffer[lastlowpos]=0.0;
lastlow=Lowmapbuffer[kof];
Zigzagbuffer[lastlowpos]=0.0;
Zigzagbuffer[kof]=lastlow;
lastlowpos=kof;
whatlookfor=1;
res=1;
}
break;

case -1:
if(Lowmapbuffer[kof]!=0.0)
{
for(k=1,counterX=0;counterX<=4 && kof+k<lasthighpos;k++)
{
if(Zag[kof+k]!=0) counterX++;
if(counterX>=3)
{
Lowmapbuffer[kof]=lastlow;
lastlowpos=kof;
whatlookfor=1;
Zigzagbuffer[kof]=lastlow;
res=1;
counterX=counterX+3;
}
}

}
if(Highmapbuffer[kof]!=0.0 && Highmapbuffer[kof]>lasthigh)
{
Highmapbuffer[lasthighpos]=0.0;
lasthigh=Highmapbuffer[kof];
Zigzagbuffer[lasthighpos]=0.0;
Zigzagbuffer[kof]=lasthigh;
lasthighpos=kof;
whatlookfor=-1;
res=1;
}
break;
default:return;
}
}


return(0);
}
/
要添加评论,请登录注册