-
Notifications
You must be signed in to change notification settings - Fork 2
/
ZigZagSignal.mq4
359 lines (355 loc) · 10.9 KB
/
ZigZagSignal.mq4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//+------------------------------------------------------------------+
//| ZigZagSignal.mq4 |
//| Copyright 2005-2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2005-2018, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property link "https://www.mql5.com/en/users/3rjfx"
#property description "Indicator ZigZag System with Signal and Alert"
#property description "Added Alert and modify by Roberto Jacobs 3rjfx @ 2018/12/29"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color2 clrBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--
enum YN
{
No,
Yes
};
//--
//---- indicator parameters
input int InpDepth = 38; // Depth
input int InpDeviation = 17; // Deviation
input int InpBackstep = 11; // Backstep
input YN alerts = Yes; // Display Alerts / Messages (Yes) or (No)
input YN EmailAlert = No; // Email Alert (Yes) or (No)
input int alertonbar = 3; // Alert On Bar after Limit
input YN displayinfo = Yes; // Display Trade Info
//---
//---- indicator buffers
double ZZBufferHi[];
double ZZBufferLo[];
//--
datetime
fbartime,
pbartime,
cbartime;
int za,zb;
int cur=5,prv=2;
int cmnt,pmnt;
static int fbar;
string posisi,
sigpos,
msgText;
string iname;
static bool bull,bear;
//---------//
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
IndicatorBuffers(2);
SetIndexBuffer(0,ZZBufferHi);
SetIndexBuffer(1,ZZBufferLo);
//---- drawing settings
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,234);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,233);
//----
//-- name for DataWindow
SetIndexLabel(0,"Upper Limit");
SetIndexLabel(1,"Lower Limit");
//--
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
//---- indicator short name
IndicatorDigits(_Digits);
iname=WindowExpertName();
IndicatorShortName(iname);
//---
return(INIT_SUCCEEDED);
}
//---------//
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
Comment("");
ObjectsDeleteAll();
GlobalVariablesDeleteAll();
//----
return(0);
}
//---------//
//+------------------------------------------------------------------+
//| 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[])
{
//---
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
ArraySetAsSeries(time,true);
//--
int shift,back,lasthighpos,lastlowpos;
double val,res;
double curlow,curhigh,lasthigh=0.0,lastlow=0.0;
//--- check for history and inputs
if(rates_total<InpDepth || InpBackstep>=InpDepth)
return(0);
//--
for(shift=rates_total-InpDepth; shift>=0; shift--)
{
val=low[iLowest(_Symbol,0,MODE_LOW,InpDepth,shift)];
if(val==lastlow) val=0.0;
else
{
lastlow=val;
if((low[shift]-val)>(InpDeviation*_Point)) val=0.0;
else
{
for(back=1; back<=InpBackstep; back++)
{
res=ZZBufferLo[shift+back];
if((res!=0)&&(res>val)) ZZBufferLo[shift+back]=0.0;
}
}
}
ZZBufferLo[shift]=val;
//--- high
val=high[iHighest(_Symbol,0,MODE_HIGH,InpDepth,shift)];
if(val==lasthigh) val=0.0;
else
{
lasthigh=val;
if((val-high[shift])>(InpDeviation*_Point)) val=0.0;
else
{
for(back=1; back<=InpBackstep; back++)
{
res=ZZBufferHi[shift+back];
if((res!=0)&&(res<val)) ZZBufferHi[shift+back]=0.0;
}
}
}
ZZBufferHi[shift]=val;
}
//--
// final cutting
lasthigh=-1; lasthighpos=-1;
lastlow=-1; lastlowpos=-1;
//--
for(shift=rates_total-InpDepth; shift>=0; shift--)
{
curlow=ZZBufferLo[shift];
curhigh=ZZBufferHi[shift];
if((curlow==0)&&(curhigh==0)) continue;
//---
if(curhigh!=0)
{
if(lasthigh>0)
{
if(lasthigh<curhigh) ZZBufferHi[lasthighpos]=0;
else ZZBufferHi[shift]=0;
}
//---
if(lasthigh<curhigh || lasthigh<0)
{
lasthigh=curhigh;
lasthighpos=shift;
}
lastlow=-1;
}
//----
if(curlow!=0)
{
if(lastlow>0)
{
if(lastlow>curlow) ZZBufferLo[lastlowpos]=0;
else ZZBufferLo[shift]=0;
}
//---
if((curlow<lastlow)||(lastlow<0))
{
lastlow=curlow;
lastlowpos=shift;
}
lasthigh=-1;
}
}
//--
for(shift=rates_total-1; shift>=0; shift--)
{
if(shift>=rates_total-InpDepth) ZZBufferLo[shift]=0.0;
else
{
res=ZZBufferHi[shift];
if(res!=0.0) ZZBufferHi[shift]=res;
}
}
//--
for(int zi=360; zi>=0 && !IsStopped(); zi--)
{
if(high[zi]==ZZBufferHi[zi]) za=zi;
if(low[zi]==ZZBufferLo[zi]) zb=zi;
}
//--
if(za<zb && za>alertonbar)
{
cbartime=time[za];
fbar=iBarShift(_Symbol,0,cbartime,false);
cur=-1;
}
if(za>zb && zb>alertonbar)
{
cbartime=time[zb];
fbar=iBarShift(_Symbol,0,cbartime,false);
cur=1;
}
//--
if((alerts==Yes||EmailAlert==Yes)&&(cur!=0 && cbartime!=pbartime)) Do_Alerts(cur,fbar);
if(displayinfo==Yes) ChartComm();
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//---------//
//+------------------------------------------------------------------+
void Do_Alerts(int fcur,int fb)
{
//--
cmnt=Minute();
if(cmnt!=pmnt)
{
//--
if(fcur==1)
{
msgText="ZigZag Lower Limit Found"+" at bars: "+string(fb);
posisi="Bullish";
sigpos="Open BUY Order";
fbartime=iTime(_Symbol,0,fb);
}
if(fcur==-1)
{
msgText="ZigZag Upper Limit Found"+" at bars: "+string(fb);
posisi="Bearish";
sigpos="Open SELL Order";
fbartime=iTime(_Symbol,0,fb);
}
//--
if(fcur!=prv || fbartime>pbartime)
{
Print(iname,"--- "+_Symbol+" "+TF2Str(_Period)+": "+msgText+
"\n--- at: ",TimeToString(iTime(_Symbol,0,0),TIME_DATE|TIME_MINUTES)+" - "+sigpos);
//--
if(alerts==Yes)
Alert(iname,"--- "+_Symbol+" "+TF2Str(_Period)+": "+msgText+
"--- at: ",TimeToString(iTime(_Symbol,0,0),TIME_DATE|TIME_MINUTES)+" - "+sigpos);
//--
if(EmailAlert==Yes)
SendMail(iname,"--- "+_Symbol+" "+TF2Str(_Period)+": "+msgText+
"\n--- at: "+TimeToString(iTime(_Symbol,0,0),TIME_DATE|TIME_MINUTES)+" - "+sigpos);
//--
prv=fcur;
pbartime=fbartime;
}
//--
pmnt=cmnt;
}
//--
return;
//---
}
//---------//
string TF2Str(int period)
{
switch(period)
{
//--
case PERIOD_M1: return("M1");
case PERIOD_M5: return("M5");
case PERIOD_M15: return("M15");
case PERIOD_M30: return("M30");
case PERIOD_H1: return("H1");
case PERIOD_H4: return("H4");
case PERIOD_D1: return("D1");
case PERIOD_W1: return("W1");
case PERIOD_MN1: return("MN");
//--
}
return(string(period));
}
//---------//
string AccountMode() // function: to known account trade mode
{
//----
//--- Demo, Contest or Real account
ENUM_ACCOUNT_TRADE_MODE account_type=(ENUM_ACCOUNT_TRADE_MODE)AccountInfoInteger(ACCOUNT_TRADE_MODE);
//---
string trade_mode;
//--
switch(account_type)
{
case ACCOUNT_TRADE_MODE_DEMO:
trade_mode="Demo";
break;
case ACCOUNT_TRADE_MODE_CONTEST:
trade_mode="Contest";
break;
default:
trade_mode="Real";
break;
}
//--
return(trade_mode);
//----
} //-end AccountMode()
//---------//
void ChartComm() // function: write comments on the chart
{
//----
//--
Comment("\n :: Server Date Time : ",(string)Year(),".",(string)Month(),".",(string)Day(), " ",TimeToString(TimeCurrent(),TIME_SECONDS),
"\n ------------------------------------------------------------",
"\n :: Broker : ", TerminalCompany(),
"\n :: Acc. Name : ", AccountName(),
"\n :: Acc, Number : ", (string)AccountNumber(),
"\n :: Acc,TradeMode : ", AccountMode(),
"\n :: Acc. Leverage : 1 : ", (string)AccountLeverage(),
"\n :: Acc. Balance : ", DoubleToString(AccountBalance(),2),
"\n :: Acc. Equity : ", DoubleToString(AccountEquity(),2),
"\n --------------------------------------------",
"\n :: Indicator Name : ",iname,
"\n :: Currency Pair : ",_Symbol,
"\n :: Current Spread : ",IntegerToString(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD),0),
"\n :: Signal Start : at bar ",string(iBarShift(_Symbol,0,cbartime,false)),
"\n :: Indicator Signal : ",posisi,
"\n :: Suggested : ",sigpos);
//---
ChartRedraw();
return;
//----
} //-end ChartComm()
//---------//