forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndi_ZigZagColor.mqh
360 lines (335 loc) · 12.4 KB
/
Indi_ZigZagColor.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
//+------------------------------------------------------------------+
//| 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 "../BufferStruct.mqh"
#include "../Indicator/IndicatorTickOrCandleSource.h"
#include "../Storage/ValueStorage.all.h"
#include "Indi_ZigZag.mqh"
// Structs.
struct IndiZigZagColorParams : IndicatorParams {
unsigned int depth;
unsigned int deviation;
unsigned int backstep;
// Struct constructor.
IndiZigZagColorParams(unsigned int _depth = 12, unsigned int _deviation = 5, unsigned int _backstep = 3,
int _shift = 0)
: IndicatorParams(INDI_ZIGZAG_COLOR) {
backstep = _backstep;
depth = _depth;
deviation = _deviation;
SetCustomIndicatorName("Examples\\ZigZagColor");
shift = _shift;
};
IndiZigZagColorParams(IndiZigZagColorParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
/**
* Implements the Volume Rate of Change indicator.
*/
class Indi_ZigZagColor : public IndicatorTickOrCandleSource<IndiZigZagColorParams> {
public:
/**
* Class constructor.
*/
Indi_ZigZagColor(IndiZigZagColorParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN,
IndicatorData *_indi_src = NULL, int _indi_src_mode = 0)
: IndicatorTickOrCandleSource(
_p, IndicatorDataParams::GetInstance(3, TYPE_DOUBLE, _idstype, IDATA_RANGE_PRICE_ON_SIGNAL, _indi_src_mode),
_indi_src){};
Indi_ZigZagColor(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0)
: IndicatorTickOrCandleSource(INDI_VROC, _tf, _shift){};
/**
* Returns value for ZigZag Color indicator.
*/
static double iZigZagColor(string _symbol, ENUM_TIMEFRAMES _tf, int _depth, int _deviation, int _backstep,
ENUM_ZIGZAG_LINE _mode = 0, int _shift = 0, IndicatorData *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG(
_symbol, _tf, Util::MakeKey("Indi_ZigZagColor", _depth, _deviation, _backstep));
return iZigZagColorOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _depth, _deviation, _backstep, _mode, _shift,
_cache);
}
/**
* Calculates ZigZag Color on the array of values.
*/
static double iZigZagColorOnArray(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>>(3 + 2);
}
if (_recalculate) {
_cache.ResetPrevCalculated();
}
_cache.SetPrevCalculated(Indi_ZigZagColor::Calculate(INDICATOR_CALCULATE_GET_PARAMS_LONG,
_cache.GetBuffer<double>(0), _cache.GetBuffer<double>(1),
_cache.GetBuffer<double>(2), _cache.GetBuffer<double>(3),
_cache.GetBuffer<double>(4), _depth, _deviation, _backstep));
return _cache.GetTailValue<double>(_mode, _shift);
}
/**
* On-indicator version of ZigZag indicator.
*/
static double iZigZagColorOnIndicator(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_ZigZagColor_ON_" + _indi.GetFullName(), _depth, _deviation, _backstep));
return iZigZagColorOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _depth, _deviation, _backstep, _mode, _shift,
_cache);
}
/**
* OnCalculate() method for ZigZag Color indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &ZigzagPeakBuffer,
ValueStorage<double> &ZigzagBottomBuffer, ValueStorage<double> &HighMapBuffer,
ValueStorage<double> &LowMapBuffer, ValueStorage<double> &ColorBuffer, int InpDepth,
int InpDeviation, int InpBackstep) {
int ExtRecalc = 3;
if (rates_total < 100) return 0;
int i, start = 0;
int extreme_counter = 0, extreme_search = Extremum;
int shift, back = 0, last_high_pos = 0, last_low_pos = 0;
double val = 0, res = 0;
double cur_low = 0, cur_high = 0, last_high = 0, last_low = 0;
// Initialize variables.
if (prev_calculated == 0) {
ArrayInitialize(ZigzagPeakBuffer, 0.0);
ArrayInitialize(ZigzagBottomBuffer, 0.0);
ArrayInitialize(HighMapBuffer, 0.0);
ArrayInitialize(LowMapBuffer, 0.0);
// Start calculation from bar number InpDepth.
start = InpDepth - 1;
}
if (prev_calculated > 0) {
// ZigZag was already calculated before.
i = rates_total - 1;
// Search for the third extremum from the last uncompleted bar.
while (extreme_counter < ExtRecalc && i > rates_total - 100) {
res = (ZigzagPeakBuffer[i] + ZigzagBottomBuffer[i]);
if (res != 0) extreme_counter++;
i--;
}
i++;
start = i;
// Type of extremum we search for.
if (LowMapBuffer[i] != 0) {
cur_low = LowMapBuffer[i].Get();
extreme_search = Peak;
} else {
cur_high = HighMapBuffer[i].Get();
extreme_search = Bottom;
}
// Clear indicator values.
for (i = start + 1; i < rates_total && !IsStopped(); i++) {
ZigzagPeakBuffer[i] = 0.0;
ZigzagBottomBuffer[i] = 0.0;
LowMapBuffer[i] = 0.0;
HighMapBuffer[i] = 0.0;
}
}
// Search for high and low extremes.
for (shift = start; shift < rates_total && !IsStopped(); shift++) {
// Low.
val = Indi_ZigZag::Lowest(low, InpDepth, shift);
if (val == last_low)
val = 0.0;
else {
last_low = val;
if ((low[shift] - val) > (InpDeviation * _Point))
val = 0.0;
else {
for (back = InpBackstep; back >= 1; back--) {
res = LowMapBuffer[shift - back].Get();
//---
if ((res != 0) && (res > val)) LowMapBuffer[shift - back] = 0.0;
}
}
}
if (low[shift] == val)
LowMapBuffer[shift] = val;
else
LowMapBuffer[shift] = 0.0;
// High.
val = Indi_ZigZag::Highest(high, InpDepth, shift);
if (val == last_high)
val = 0.0;
else {
last_high = val;
if ((val - high[shift].Get()) > (InpDeviation * _Point))
val = 0.0;
else {
for (back = InpBackstep; back >= 1; back--) {
res = HighMapBuffer[shift - back].Get();
//---
if ((res != 0) && (res < val)) HighMapBuffer[shift - back] = 0.0;
}
}
}
if (high[shift] == val)
HighMapBuffer[shift] = val;
else
HighMapBuffer[shift] = 0.0;
}
// Set last values.
if (extreme_search == 0) {
// Undefined values.
last_low = 0;
last_high = 0;
} else {
last_low = cur_low;
last_high = cur_high;
}
// 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 && last_high == 0) {
if (HighMapBuffer[shift] != 0) {
last_high = high[shift].Get();
last_high_pos = shift;
extreme_search = -1;
ZigzagPeakBuffer[shift] = last_high;
ColorBuffer[shift] = 0;
res = 1;
}
if (LowMapBuffer[shift] != 0) {
last_low = low[shift].Get();
last_low_pos = shift;
extreme_search = 1;
ZigzagBottomBuffer[shift] = last_low;
ColorBuffer[shift] = 1;
res = 1;
}
}
break;
case Peak:
if (LowMapBuffer[shift] != 0.0 && LowMapBuffer[shift] < last_low && HighMapBuffer[shift] == 0.0) {
ZigzagBottomBuffer[last_low_pos] = 0.0;
last_low_pos = shift;
last_low = LowMapBuffer[shift].Get();
ZigzagBottomBuffer[shift] = last_low;
ColorBuffer[shift] = 1;
res = 1;
}
if (HighMapBuffer[shift] != 0.0 && LowMapBuffer[shift] == 0.0) {
last_high = HighMapBuffer[shift].Get();
last_high_pos = shift;
ZigzagPeakBuffer[shift] = last_high;
ColorBuffer[shift] = 0;
extreme_search = Bottom;
res = 1;
}
break;
case Bottom:
if (HighMapBuffer[shift] != 0.0 && HighMapBuffer[shift] > last_high && LowMapBuffer[shift] == 0.0) {
ZigzagPeakBuffer[last_high_pos] = 0.0;
last_high_pos = shift;
last_high = HighMapBuffer[shift].Get();
ZigzagPeakBuffer[shift] = last_high;
ColorBuffer[shift] = 0;
}
if (LowMapBuffer[shift] != 0.0 && HighMapBuffer[shift] == 0.0) {
last_low = LowMapBuffer[shift].Get();
last_low_pos = shift;
ZigzagBottomBuffer[shift] = last_low;
ColorBuffer[shift] = 1;
extreme_search = Peak;
}
break;
default:
return (rates_total);
}
}
return rates_total;
}
/**
* Returns the indicator's value.
*/
virtual IndicatorDataEntryValue GetEntryValue(int _mode = 0, 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_ZigZagColor::iZigZagColor(GetSymbol(), GetTf(), /*[*/ GetDepth(), GetDeviation(),
GetBackstep() /*]*/, (ENUM_ZIGZAG_LINE)_mode, _ishift, THIS_PTR);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.GetCustomIndicatorName(),
/*[*/ GetDepth(), GetDeviation(), GetBackstep() /*]*/, _mode, _ishift);
break;
case IDATA_INDICATOR:
_value =
Indi_ZigZagColor::iZigZagColorOnIndicator(GetDataSource(), GetSymbol(), GetTf(), /*[*/ GetDepth(),
GetDeviation(), GetBackstep() /*]*/, _mode, _ishift, THIS_PTR);
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
break;
}
return _value;
}
/**
* Checks if indicator entry values are valid.
*/
virtual bool IsValidEntry(IndicatorDataEntry &_entry) {
return _entry.values[0].Get<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;
}
};