Indicators Used
1
Views
0
Downloads
0
Favorites
ZigZag_Stochastic
ÿþ//+------------------------------------------------------------------+
//| ZigZag_Stochastic.mq5 |
//| Copyright © 2016, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//---- 02B>@AB2> 8=48:0B>@0
#property copyright "Copyright © 2016, MetaQuotes Software Corp."
//---- AAK;:0 =0 A09B 02B>@0
#property link "http://www.metaquotes.net/"
//---- =><5@ 25@A88 8=48:0B>@0
#property version "1.00"
#property description "ZigZag"
//---- >B@8A>2:0 8=48:0B>@0 2 >B45;L=>< >:=5
#property indicator_separate_window
//---- 4;O @0AGQB0 8 >B@8A>2:8 8=48:0B>@0 8A?>;L7>20=> 4 1CD5@0
#property indicator_buffers 4
//---- 8A?>;L7>20=> 2A53> 2 3@0D8G5A:8E ?>AB@>5=8O
#property indicator_plots 2
//+----------------------------------------------+
//| 0@0<5B@K >B@8A>2:8 >AF8;;OB>@0 |
//+----------------------------------------------+
//--- >B@8A>2:0 8=48:0B>@0 2 2845 F25B=>3> >1;0:0
#property indicator_type1 DRAW_FILLING
//--- 2 :0G5AB25 F25B>2 8=48:0B>@0 8A?>;L7>20=K
#property indicator_color1 clrPaleGreen,clrPink
//---- >B>1@065=85 1KG59 <5B:8 8=48:0B>@0
#property indicator_label1 "Stochastic Oscillator"
//+----------------------------------------------+
//| 0@0<5B@K >B@8A>2:8 7837030 |
//+----------------------------------------------+
//---- 2 :0G5AB25 8=48:0B>@0 8A?>;L7>20= ZIGZAG
#property indicator_type2 DRAW_ZIGZAG
//---- >B>1@065=85 <5B:8 8=48:0B>@0
#property indicator_label2 "ZigZag_Stochastic"
//---- 2 :0G5AB25 F25B0 ;8=88 8=48:0B>@0 8A?>;L7>20= Blue F25B
#property indicator_color2 clrBlue
//---- ;8=8O 8=48:0B>@0 - =5?@5@K2=0O :@820O
#property indicator_style2 STYLE_SOLID
//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 2
#property indicator_width2 2
//+----------------------------------------------+
//| >1JO2;5=85 ?5@5G8A;5=89 |
//+----------------------------------------------+
enum ENUM_MODE //"8? :>=AB0=BK
{
MODE_MAIN = 0, //83703 ?> >A=>2=>9 ;8=88 8=48:0B>@0
MODE_SIGNAL //83703 ?> A83=0;L=>9 ;8=88 8=48:0B>@0
};
//+----------------------------------------------+
//| >1JO2;5=85 :>=AB0=B |
//+----------------------------------------------+
#define RESET 0 // >=AB0=B0 4;O 2>72@0B0 B5@<8=0;C :><0=4K =0 ?5@5AGQB 8=48:0B>@0
//+----------------------------------------------+
//| E>4=K5 ?0@0<5B@K 8=48:0B>@0 |
//+----------------------------------------------+
//---- 2E>4=K5 ?0@0<5B@K >AF8;;OB>@0 Stochastic
input ENUM_MODE Mode=MODE_MAIN; // 0@80=B ?>AB@>5=8O
input int KPeriod=5;
input int DPeriod=3;
input int Slowing=3;
input ENUM_MA_METHOD iMA_Method=MODE_SMA;
input ENUM_STO_PRICE Price_field=STO_LOWHIGH;
//---- 2E>4=K5 ?0@0<5B@K 7837030
input uint ExtDepth=3;
input double ExtDeviation=10;
input uint ExtBackstep=3;
input uint HighLevel=70; // C@>25=L ?5@570:C?0
input uint LowLevel=30; // C@>25=L ?5@5?@>40==>AB8
//+----------------------------------------------+
//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2
// 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2
double LowestBuffer[],HighestBuffer[],StochasticBuffer[],SignBuffer[];
//---- 1JO2;5=85 ?5@5<5==KE ?0<OB8 4;O ?5@5AGQB0 8=48:0B>@0 B>;L:> =0 =5?>AG8B0==KE 10@0E
int LASTlowpos,LASThighpos,LASTColor;
double LASTlow0,LASTlow1,LASThigh0,LASThigh1;
//---- 1JO2;5=85 F5;KE ?5@5<5==KE =0G0;0 >BAGQB0 40==KE
int min_rates_1,min_rates_total,Stochastic_Handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- ?>;CG5=85 E5=4;0 8=48:0B>@0 iStochastic
Stochastic_Handle=iStochastic(NULL,0,KPeriod,DPeriod,Slowing,iMA_Method,Price_field);
if(Stochastic_Handle==INVALID_HANDLE)
{
Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 iStochastic");
return(INIT_FAILED);
}
//---- =8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE
min_rates_1=int(KPeriod+DPeriod+Slowing);
min_rates_total=int(min_rates_1+ExtDepth+ExtBackstep);
//---- ?@52@0I5=85 48=0<8G5A:8E <0AA82>2 2 8=48:0B>@=K5 1CD5@K
SetIndexBuffer(0,StochasticBuffer,INDICATOR_DATA);
SetIndexBuffer(1,SignBuffer,INDICATOR_DATA);
SetIndexBuffer(2,LowestBuffer,INDICATOR_DATA);
SetIndexBuffer(3,HighestBuffer,INDICATOR_DATA);
//---- 70?@5B =0 >B@8A>2:C 8=48:0B>@>< ?CABKE 7=0G5=89
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- 8=45:A0F8O M;5<5=B>2 2 1CD5@0E :0: 2 B09<A5@8OE
ArraySetAsSeries(LowestBuffer,true);
ArraySetAsSeries(HighestBuffer,true);
ArraySetAsSeries(StochasticBuffer,true);
ArraySetAsSeries(SignBuffer,true);
//---- CAB0=>2:0 ?>78F88, A :>B>@>9 =0G8=05BAO >B@8A>2:0
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_1);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//---- #AB0=>2:0 D>@<0B0 B>G=>AB8 >B>1@065=8O 8=48:0B>@0
IndicatorSetInteger(INDICATOR_DIGITS,3);
//---- 8<O 4;O >:>= 40==KE 8 ;M910 4;O AC1J>:>=
IndicatorSetString(INDICATOR_SHORTNAME,"ZigZag_Stochastic");
//---- :>;8G5AB2> 3>@87>=B0;L=KE C@>2=59 8=48:0B>@0 3
IndicatorSetInteger(INDICATOR_LEVELS,3);
//---- 7=0G5=8O 3>@87>=B0;L=KE C@>2=59 8=48:0B>@0
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,HighLevel);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0.0);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,LowLevel);
//---- 2 :0G5AB25 F25B>2 ;8=89 3>@87>=B0;L=KE C@>2=59 8A?>;L7>20=K
IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrDodgerBlue);
IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrGray);
IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrRed);
//---- 2 ;8=88 3>@87>=B0;L=>3> C@>2=O 8A?>;L7>20= :>@>B:89 HB@8E-?C=:B8@
IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT);
IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASHDOTDOT);
IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASHDOTDOT);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0
if(BarsCalculated(Stochastic_Handle)<rates_total || rates_total<min_rates_total) return(RESET);
//---- >1JO2;5=8O ;>:0;L=KE ?5@5<5==KE
int to_copy,limit,climit,bar,back,lasthighpos,lastlowpos;
double curlow,curhigh,lasthigh0=EMPTY_VALUE,lastlow0=EMPTY_VALUE,lasthigh1,lastlow1,val,res;
//---- @0AGQB AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AGQB0 10@>2 8 AB0@B>20O 8=8F80;870F8O ?5@5<5==KE
if(prev_calculated>rates_total || prev_calculated<=0)// ?@>25@:0 =0 ?5@2K9 AB0@B @0AGQB0 8=48:0B>@0
{
limit=rates_total-min_rates_total; // AB0@B>2K9 =><5@ 4;O @0AGQB0 2A5E 10@>2
lastlow1=-1;
lasthigh1=-1;
lastlowpos=-1;
lasthighpos=-1;
}
else
{
limit=rates_total-prev_calculated; // AB0@B>2K9 =><5@ 4;O @0AGQB0 =>2KE 10@>2
climit=limit+min_rates_total; // AB0@B>2K9 =><5@ 4;O @0A:@0A:8 8=48:0B>@0
//---- 2>AAB0=02;8205< 7=0G5=8O ?5@5<5==KE
lastlow0=LASTlow0;
lasthigh0=LASThigh0;
lastlow1=LASTlow1;
lasthigh1=LASThigh1;
lastlowpos=LASTlowpos+limit;
lasthighpos=LASThighpos+limit;
}
to_copy=limit+1;
//--- :>?8@C5< 2=>2L ?>O282H85AO 40==K5 2 <0AA82K
if(CopyBuffer(Stochastic_Handle,0,0,to_copy,StochasticBuffer)<=0) return(RESET);
if(CopyBuffer(Stochastic_Handle,1,0,to_copy,SignBuffer)<=0) return(RESET);
//---- 5@2K9 1>;LH>9 F8:; @0AGQB0 8=48:0B>@0
if(Mode==MODE_MAIN)for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- 70?><8=05< 7=0G5=8O ?5@5<5==KE ?5@54 ?@>3>=0<8 =0 B5:CI5< 10@5
if(rates_total!=prev_calculated && bar==0)
{
LASTlow0=lastlow0;
LASThigh0=lasthigh0;
}
LowestBuffer[bar]=EMPTY_VALUE;
HighestBuffer[bar]=EMPTY_VALUE;
//--- low
val=StochasticBuffer[ArrayMinimum(StochasticBuffer,bar,ExtDepth)];
if(val==lastlow0) val=EMPTY_VALUE;
else
{
lastlow0=val;
if(StochasticBuffer[bar]-val>ExtDeviation) val=EMPTY_VALUE;
else
{
for(back=1; back<=int(ExtBackstep); back++)
{
res=LowestBuffer[bar+back];
if(res!=EMPTY_VALUE && res>val) LowestBuffer[bar+back]=EMPTY_VALUE;
}
}
}
LowestBuffer[bar]=val;
//--- high
val=StochasticBuffer[ArrayMaximum(StochasticBuffer,bar,ExtDepth)];
if(val==lasthigh0) val=EMPTY_VALUE;
else
{
lasthigh0=val;
if(val-StochasticBuffer[bar]>ExtDeviation) val=EMPTY_VALUE;
else
{
for(back=1; back<=int(ExtBackstep); back++)
{
res=HighestBuffer[bar+back];
if(res!=EMPTY_VALUE && res<val) HighestBuffer[bar+back]=EMPTY_VALUE;
}
}
}
HighestBuffer[bar]=val;
}
//---- 5@2K9 1>;LH>9 F8:; @0AGQB0 8=48:0B>@0
if(Mode==MODE_SIGNAL)for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- 70?><8=05< 7=0G5=8O ?5@5<5==KE ?5@54 ?@>3>=0<8 =0 B5:CI5< 10@5
if(rates_total!=prev_calculated && bar==0)
{
LASTlow0=lastlow0;
LASThigh0=lasthigh0;
}
LowestBuffer[bar]=EMPTY_VALUE;
HighestBuffer[bar]=EMPTY_VALUE;
//--- low
val=SignBuffer[ArrayMinimum(SignBuffer,bar,ExtDepth)];
if(val==lastlow0) val=EMPTY_VALUE;
else
{
lastlow0=val;
if(SignBuffer[bar]-val>ExtDeviation) val=EMPTY_VALUE;
else
{
for(back=1; back<=int(ExtBackstep); back++)
{
res=LowestBuffer[bar+back];
if(res!=EMPTY_VALUE && res>val) LowestBuffer[bar+back]=EMPTY_VALUE;
}
}
}
LowestBuffer[bar]=val;
//--- high
val=SignBuffer[ArrayMaximum(SignBuffer,bar,ExtDepth)];
if(val==lasthigh0) val=EMPTY_VALUE;
else
{
lasthigh0=val;
if(val-SignBuffer[bar]>ExtDeviation) val=EMPTY_VALUE;
else
{
for(back=1; back<=int(ExtBackstep); back++)
{
res=HighestBuffer[bar+back];
if(res!=EMPTY_VALUE && res<val) HighestBuffer[bar+back]=EMPTY_VALUE;
}
}
}
HighestBuffer[bar]=val;
}
//---- B>@>9 1>;LH>9 F8:; @0AGQB0 8=48:0B>@0
for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- 70?><8=05< 7=0G5=8O ?5@5<5==KE ?5@54 ?@>3>=0<8 =0 B5:CI5< 10@5
if(rates_total!=prev_calculated && bar==0)
{
LASTlow1=lastlow1;
LASThigh1=lasthigh1;
//----
LASTlowpos=lastlowpos;
LASThighpos=lasthighpos;
}
curlow=LowestBuffer[bar];
curhigh=HighestBuffer[bar];
//---
if(curlow==EMPTY_VALUE && curhigh==EMPTY_VALUE) continue;
//---
if(curhigh!=EMPTY_VALUE)
{
if(lasthigh1>0)
{
if(lasthigh1<curhigh) HighestBuffer[lasthighpos]=EMPTY_VALUE;
else HighestBuffer[bar]=EMPTY_VALUE;
}
//---
if(lasthigh1<curhigh || lasthigh1<0 || curhigh==EMPTY_VALUE || lasthigh1==EMPTY_VALUE)
{
lasthigh1=curhigh;
lasthighpos=bar;
}
lastlow1=-1;
}
//----
if(curlow!=EMPTY_VALUE)
{
if(lastlow1>0)
{
if(lastlow1>curlow) LowestBuffer[lastlowpos]=EMPTY_VALUE;
else LowestBuffer[bar]=EMPTY_VALUE;
}
//---
if(curlow<lastlow1 || lastlow1<0 || curlow==EMPTY_VALUE || lastlow1==EMPTY_VALUE)
{
lastlow1=curlow;
lastlowpos=bar;
}
lasthigh1=-1;
}
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---