-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Indi_ZigZag.mqh
423 lines (387 loc) · 14.3 KB
/
Indi_ZigZag.mqh
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Includes.
#include "../Indicator/IndicatorTickOrCandleSource.h"
#include "../Storage/ValueStorage.all.h"
// Enums.
// Indicator mode identifiers used in ZigZag indicator.
enum ENUM_ZIGZAG_LINE { ZIGZAG_BUFFER = 0, ZIGZAG_HIGHMAP = 1, ZIGZAG_LOWMAP = 2, FINAL_ZIGZAG_LINE_ENTRY };
// Structs.
struct IndiZigZagParams : IndicatorParams {
unsigned int depth;
unsigned int deviation;
unsigned int backstep;
// Struct constructors.
IndiZigZagParams(unsigned int _depth = 12, unsigned int _deviation = 5, unsigned int _backstep = 3, int _shift = 0)
: depth(_depth), deviation(_deviation), backstep(_backstep), IndicatorParams(INDI_ZIGZAG) {
shift = _shift;
SetCustomIndicatorName("Examples\\ZigZag");
};
IndiZigZagParams(IndiZigZagParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
enum EnSearchMode {
Extremum = 0, // searching for the first extremum
Peak = 1, // searching for the next ZigZag peak
Bottom = -1 // searching for the next ZigZag bottom
};
/**
* Implements ZigZag indicator.
*/
class Indi_ZigZag : public IndicatorTickOrCandleSource<IndiZigZagParams> {
public:
/**
* Class constructor.
*/
Indi_ZigZag(IndiZigZagParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN, IndicatorData *_indi_src = NULL,
int _indi_src_mode = 0)
: IndicatorTickOrCandleSource(_p,
IndicatorDataParams::GetInstance(FINAL_ZIGZAG_LINE_ENTRY, TYPE_DOUBLE, _idstype,
IDATA_RANGE_PRICE_ON_SIGNAL),
_indi_src) {}
Indi_ZigZag(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0)
: IndicatorTickOrCandleSource(INDI_ZIGZAG, _tf, _shift) {}
/**
* Returns value for ZigZag indicator.
*/
static double iCustomZigZag(string _symbol, ENUM_TIMEFRAMES _tf, string _name, int _depth, int _deviation,
int _backstep, ENUM_ZIGZAG_LINE _mode = 0, int _shift = 0, IndicatorData *_obj = NULL) {
#ifdef __MQL5__
int _handle = Object::IsValid(_obj) ? _obj.Get<int>(IndicatorState::INDICATOR_STATE_PROP_HANDLE) : NULL;
double _res[];
if (_handle == NULL || _handle == INVALID_HANDLE) {
if ((_handle = ::iCustom(_symbol, _tf, _name, _depth, _deviation, _backstep)) == INVALID_HANDLE) {
SetUserError(ERR_USER_INVALID_HANDLE);
return EMPTY_VALUE;
} else if (Object::IsValid(_obj)) {
_obj.SetHandle(_handle);
}
}
if (Terminal::IsVisualMode()) {
// To avoid error 4806 (ERR_INDICATOR_DATA_NOT_FOUND),
// we check the number of calculated data only in visual mode.
int _bars_calc = BarsCalculated(_handle);
if (GetLastError() > 0) {
return EMPTY_VALUE;
} else if (_bars_calc <= 2) {
SetUserError(ERR_USER_INVALID_BUFF_NUM);
return EMPTY_VALUE;
}
}
if (CopyBuffer(_handle, _mode, _shift, 1, _res) < 0) {
return ArraySize(_res) > 0 ? _res[0] : EMPTY_VALUE;
}
return _res[0];
#else
return ::iCustom(_symbol, _tf, _name, _depth, _deviation, _backstep, _mode, _shift);
#endif
}
/**
* Returns value for ZigZag indicator.
*/
static double iZigZag(string _symbol, ENUM_TIMEFRAMES _tf, int _depth, int _deviation, int _backstep,
ENUM_ZIGZAG_LINE _mode = 0, int _shift = 0, Indi_ZigZag *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG(_symbol, _tf,
Util::MakeKey("Indi_ZigZag", _depth, _deviation, _backstep));
return iZigZagOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _depth, _deviation, _backstep, _mode, _shift,
_cache);
}
/**
* Calculates ZigZag on the array of values.
*/
static double iZigZagOnArray(INDICATOR_CALCULATE_PARAMS_LONG, int _depth, int _deviation, int _backstep, int _mode,
int _shift, IndicatorCalculateCache<double> *_cache, bool _recalculate = false) {
_cache.SetPriceBuffer(_open, _high, _low, _close);
if (!_cache.HasBuffers()) {
_cache.AddBuffer<NativeValueStorage<double>>(1 + 2);
}
if (_recalculate) {
_cache.ResetPrevCalculated();
}
_cache.SetPrevCalculated(Indi_ZigZag::Calculate(INDICATOR_CALCULATE_GET_PARAMS_LONG, _cache.GetBuffer<double>(0),
_cache.GetBuffer<double>(1), _cache.GetBuffer<double>(2), _depth,
_deviation, _backstep));
return _cache.GetTailValue<double>(_mode, _shift);
}
/**
* On-indicator version of ZigZag indicator.
*/
static double iZigZagOnIndicator(IndicatorData *_indi, string _symbol, ENUM_TIMEFRAMES _tf, int _depth,
int _deviation, int _backstep, int _mode = 0, int _shift = 0,
IndicatorData *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG_DS(
_indi, _symbol, _tf, Util::MakeKey("Indi_ZigZag_ON_" + _indi.GetFullName(), _depth, _deviation, _backstep));
return iZigZagOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _depth, _deviation, _backstep, _mode, _shift,
_cache);
}
/**
* OnCalculate() method for ZigZag indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &ZigZagBuffer,
ValueStorage<double> &HighMapBuffer, ValueStorage<double> &LowMapBuffer, int InpDepth,
int InpDeviation, int InpBackstep) {
int ExtRecalc = 3;
if (rates_total < 100) return (0);
//---
int i = 0;
int start = 0, extreme_counter = 0, extreme_search = Extremum;
int shift = 0, back = 0, last_high_pos = 0, last_low_pos = 0;
double val = 0, res = 0;
double curlow = 0, curhigh = 0, last_high = 0, last_low = 0;
// Initializing.
if (prev_calculated == 0) {
ArrayInitialize(ZigZagBuffer, 0.0);
ArrayInitialize(HighMapBuffer, 0.0);
ArrayInitialize(LowMapBuffer, 0.0);
start = InpDepth;
}
// ZigZag was already calculated before.
if (prev_calculated > 0) {
i = rates_total - 1;
// Searching for the third extremum from the last uncompleted bar.
while (extreme_counter < ExtRecalc && i > rates_total - 100) {
res = ZigZagBuffer[i].Get();
if (res != 0.0) extreme_counter++;
i--;
}
i++;
start = i;
// What type of extremum we search for.
if (LowMapBuffer[i] != 0.0) {
curlow = LowMapBuffer[i].Get();
extreme_search = Peak;
} else {
curhigh = HighMapBuffer[i].Get();
extreme_search = Bottom;
}
// Clear indicator values.
for (i = start + 1; i < rates_total && !IsStopped(); i++) {
ZigZagBuffer[i] = 0.0;
LowMapBuffer[i] = 0.0;
HighMapBuffer[i] = 0.0;
}
}
// Searching for high and low extremes.
for (shift = start; shift < rates_total && !IsStopped(); shift++) {
// Low.
val = low[Lowest(low, InpDepth, shift)].Get();
if (val == last_low) {
val = 0.0;
} else {
last_low = val;
if ((low[shift] - val) > InpDeviation * _Point) {
val = 0.0;
} else {
for (back = 1; back <= InpBackstep; back++) {
res = LowMapBuffer[shift - back].Get();
if ((res != 0) && (res > val)) LowMapBuffer[shift - back] = 0.0;
}
}
}
LowMapBuffer[shift] = (low[shift] == val) ? val : 0.0;
// High.
val = high[Highest(high, InpDepth, shift)].Get();
if (val == last_high) {
val = 0.0;
} else {
last_high = val;
if ((val - high[shift].Get()) > InpDeviation * _Point) {
val = 0.0;
} else {
for (back = 1; back <= InpBackstep; back++) {
res = HighMapBuffer[shift - back].Get();
if ((res != 0) && (res < val)) HighMapBuffer[shift - back] = 0.0;
}
}
}
HighMapBuffer[shift] = (high[shift] == val) ? val : 0.0;
}
// Set last values.
if (extreme_search == 0) {
// Undefined values.
last_low = 0.0;
last_high = 0.0;
} else {
last_low = curlow;
last_high = curhigh;
}
// Final selection of extreme points for ZigZag.
for (shift = start; shift < rates_total && !IsStopped(); shift++) {
res = 0.0;
switch (extreme_search) {
case Extremum:
if (last_low == 0.0 && last_high == 0.0) {
if (HighMapBuffer[shift] != 0) {
last_high = high[shift].Get();
last_high_pos = shift;
extreme_search = Bottom;
ZigZagBuffer[shift] = last_high;
res = 1;
}
if (LowMapBuffer[shift] != 0.0) {
last_low = low[shift].Get();
last_low_pos = shift;
extreme_search = Peak;
ZigZagBuffer[shift] = last_low;
res = 1;
}
}
break;
case Peak:
if (LowMapBuffer[shift] != 0.0 && LowMapBuffer[shift] < last_low && HighMapBuffer[shift] == 0.0) {
ZigZagBuffer[last_low_pos] = 0.0;
last_low_pos = shift;
last_low = LowMapBuffer[shift].Get();
ZigZagBuffer[shift] = last_low;
res = 1;
}
if (HighMapBuffer[shift] != 0.0 && LowMapBuffer[shift] == 0.0) {
last_high = HighMapBuffer[shift].Get();
last_high_pos = shift;
ZigZagBuffer[shift] = last_high;
extreme_search = Bottom;
res = 1;
}
break;
case Bottom:
if (HighMapBuffer[shift] != 0.0 && HighMapBuffer[shift] > last_high && LowMapBuffer[shift] == 0.0) {
ZigZagBuffer[last_high_pos] = 0.0;
last_high_pos = shift;
last_high = HighMapBuffer[shift].Get();
ZigZagBuffer[shift] = last_high;
}
if (LowMapBuffer[shift] != 0.0 && HighMapBuffer[shift] == 0.0) {
last_low = LowMapBuffer[shift].Get();
last_low_pos = shift;
ZigZagBuffer[shift] = last_low;
extreme_search = Peak;
}
break;
default:
return (rates_total);
}
}
// Return value of prev_calculated for next call.
return (rates_total);
}
/**
* Search for the index of the highest bar.
*/
static int Highest(ValueStorage<double> &array, const int depth, const int start) {
if (start < 0) return (0);
double max = array[start].Get();
int index = start;
// Start searching.
for (int i = start - 1; i > start - depth && i >= 0; i--) {
if (array[i] > max) {
index = i;
max = array[i].Get();
}
}
// Return index of the highest bar.
return index;
}
/**
* Search for the index of the lowest bar.
*/
static int Lowest(ValueStorage<double> &array, const int depth, const int start) {
if (start < 0) return (0);
double min = array[start].Get();
int index = start;
// Start searching.
for (int i = start - 1; i > start - depth && i >= 0; i--) {
if (array[i] < min) {
index = i;
min = array[i].Get();
}
}
// Return index of the lowest bar.
return index;
}
/**
* Returns the indicator's value.
*/
virtual IndicatorDataEntryValue GetEntryValue(int _mode, int _shift = 0) {
double _value = EMPTY_VALUE;
int _ishift = _shift >= 0 ? _shift : iparams.GetShift();
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_BUILTIN:
_value = Indi_ZigZag::iZigZag(GetSymbol(), GetTf(), /*[*/ GetDepth(), GetDeviation(), GetBackstep() /*]*/,
(ENUM_ZIGZAG_LINE)_mode, _ishift, THIS_PTR);
break;
case IDATA_ICUSTOM:
_value =
Indi_ZigZag::iCustomZigZag(GetSymbol(), GetTf(), iparams.GetCustomIndicatorName(), /*[*/ GetDepth(),
GetDeviation(), GetBackstep() /*]*/, (ENUM_ZIGZAG_LINE)_mode, _ishift, THIS_PTR);
break;
case IDATA_INDICATOR:
_value = Indi_ZigZag::iZigZagOnIndicator(GetDataSource(), GetSymbol(), GetTf(), /*[*/ GetDepth(),
GetDeviation(), GetBackstep() /*]*/, _mode, _ishift, THIS_PTR);
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
}
return _value;
}
/**
* Checks if indicator entry values are valid.
*/
virtual bool IsValidEntry(IndicatorDataEntry &_entry) {
return !_entry.HasValue<double>(EMPTY_VALUE);
}
/* Getters */
/**
* Get depth.
*/
unsigned int GetDepth() { return iparams.depth; }
/**
* Get deviation.
*/
unsigned int GetDeviation() { return iparams.deviation; }
/**
* Get backstep.
*/
unsigned int GetBackstep() { return iparams.backstep; }
/* Setters */
/**
* Set depth.
*/
void SetDepth(unsigned int _depth) {
istate.is_changed = true;
iparams.depth = _depth;
}
/**
* Set deviation.
*/
void SetDeviation(unsigned int _deviation) {
istate.is_changed = true;
iparams.deviation = _deviation;
}
/**
* Set backstep.
*/
void SetBackstep(unsigned int _backstep) {
istate.is_changed = true;
iparams.backstep = _backstep;
}
};