-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
IndicatorBase.h
459 lines (395 loc) · 14.2 KB
/
IndicatorBase.h
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//+------------------------------------------------------------------+
//| 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/>.
*
*/
/**
* @file
* Base interface for Indicator<T> class.
*/
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif
// Forward declaration.
class Chart;
// Includes.
#include "Array.mqh"
#include "BufferStruct.mqh"
#include "Chart.mqh"
#include "DateTime.mqh"
#include "DrawIndicator.mqh"
#include "Indicator.define.h"
#include "Indicator.enum.h"
#include "Indicator.struct.cache.h"
#include "Indicator.struct.h"
#include "Indicator.struct.serialize.h"
#include "Object.mqh"
#include "Refs.mqh"
#include "Serializer.mqh"
#include "SerializerCsv.mqh"
#include "SerializerJson.mqh"
#include "Util.h"
/**
* Class to deal with indicators.
*/
class IndicatorBase : public Chart {
protected:
IndicatorState istate;
void* mydata;
int calc_start_bar; // Index of the first valid bar (from 0).
bool indicator_builtin;
long last_tick_time; // Time of the last Tick() call.
int flags; // Flags such as INDI_FLAG_INDEXABLE_BY_SHIFT.
public:
/* Indicator enumerations */
/*
* Default enumerations:
*
* ENUM_MA_METHOD values:
* 0: MODE_SMA (Simple averaging)
* 1: MODE_EMA (Exponential averaging)
* 2: MODE_SMMA (Smoothed averaging)
* 3: MODE_LWMA (Linear-weighted averaging)
*/
/* Special methods */
/**
* Class constructor.
*/
IndicatorBase(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _symbol = NULL) : Chart(_tf, _symbol) {
// By default, indicator is indexable only by shift and data source must be also indexable by shift.
flags = INDI_FLAG_INDEXABLE_BY_SHIFT | INDI_FLAG_SOURCE_REQ_INDEXABLE_BY_SHIFT;
calc_start_bar = 0;
last_tick_time = 0;
}
/**
* Class constructor.
*/
IndicatorBase(ENUM_TIMEFRAMES_INDEX _tfi, string _symbol = NULL) : Chart(_tfi, _symbol) {
// By default, indicator is indexable only by shift and data source must be also indexable by shift.
flags = INDI_FLAG_INDEXABLE_BY_SHIFT | INDI_FLAG_SOURCE_REQ_INDEXABLE_BY_SHIFT;
calc_start_bar = 0;
last_tick_time = 0;
}
/**
* Class deconstructor.
*/
virtual ~IndicatorBase() { ReleaseHandle(); }
/* Buffer methods */
virtual string CacheKey() { return GetName(); }
/**
* Initializes a cached proxy between i*OnArray() methods and OnCalculate()
* used by custom indicators.
*
* Note that OnCalculateProxy() method sets incoming price array as not
* series. It will be reverted back by SetPrevCalculated(). It is because
* OnCalculate() methods assumes that prices are set as not series.
*
* For real example how you can use this method, look at
* Indi_MA::iMAOnArray() method.
*
* Usage:
*
* static double iFooOnArray(double &price[], int total, int period,
* int foo_shift, int foo_method, int shift, string cache_name = "")
* {
* if (cache_name != "") {
* String cache_key;
* cache_key.Add(cache_name);
* cache_key.Add(period);
* cache_key.Add(foo_method);
*
* Ref<IndicatorCalculateCache> cache = Indicator::OnCalculateProxy(cache_key.ToString(), price, total);
*
* int prev_calculated =
* Indi_Foo::Calculate(total, cache.Ptr().prev_calculated, 0, price, cache.Ptr().buffer1, ma_method, period);
*
* cache.Ptr().SetPrevCalculated(price, prev_calculated);
*
* return cache.Ptr().GetValue(1, shift + ma_shift);
* }
* else {
* // Default iFooOnArray.
* }
*
* WARNING: Do not use shifts when creating cache_key, as this will create many invalid buffers.
*/
/*
static IndicatorCalculateCache OnCalculateProxy(string key, double& price[], int& total) {
if (total == 0) {
total = ArraySize(price);
}
// Stores previously calculated value.
static DictStruct<string, IndicatorCalculateCache> cache;
unsigned int position;
IndicatorCalculateCache cache_item;
if (cache.KeyExists(key, position)) {
cache_item = cache.GetByKey(key);
} else {
IndicatorCalculateCache cache_item_new(1, ArraySize(price));
cache_item = cache_item_new;
cache.Set(key, cache_item);
}
// Number of bars available in the chart. Same as length of the input `array`.
int rates_total = ArraySize(price);
int begin = 0;
cache_item.Resize(rates_total);
cache_item.price_was_as_series = ArrayGetAsSeries(price);
ArraySetAsSeries(price, false);
return cache_item;
}
*/
/* Getters */
/**
* Returns indicator's flags.
*/
int GetFlags() { return flags; }
/**
* Gets an indicator's chart parameter value.
*/
template <typename T>
T Get(ENUM_CHART_PARAM _param) {
return Chart::Get<T>(_param);
}
/**
* Gets an indicator's state property value.
*/
template <typename T>
T Get(STRUCT_ENUM_INDICATOR_STATE_PROP _prop) {
return istate.Get<T>(_prop);
}
/**
* Gets number of modes available to retrieve by GetValue().
*/
virtual int GetModeCount() { return 0; }
/* Getters */
/**
* Whether data source is selected.
*/
virtual bool HasDataSource(bool _try_initialize = false) { return false; }
/**
* Returns currently selected data source doing validation.
*/
virtual IndicatorBase* GetDataSource() { return NULL; }
/**
* Get indicator type.
*/
virtual ENUM_INDICATOR_TYPE GetType() { return INDI_NONE; }
/**
* Get data type of indicator.
*/
virtual ENUM_DATATYPE GetDataType() { return (ENUM_DATATYPE)-1; }
/**
* Get name of the indicator.
*/
virtual string GetName() { return EnumToString(GetType()); }
/**
* Get full name of the indicator (with "over ..." part).
*/
virtual string GetFullName() { return GetName(); }
/**
* Get more descriptive name of the indicator.
*/
virtual string GetDescriptiveName() { return GetName(); }
/* Setters */
/**
* Sets an indicator's chart parameter value.
*/
template <typename T>
void Set(ENUM_CHART_PARAM _param, T _value) {
Chart::Set<T>(_param, _value);
}
/**
* Sets an indicator's state property value.
*/
template <typename T>
void Set(STRUCT_ENUM(IndicatorState, ENUM_INDICATOR_STATE_PROP) _prop, T _value) {
istate.Set<T>(_prop, _value);
}
/**
* Sets name of the indicator.
*/
virtual void SetName(string _name) {}
/**
* Sets indicator's handle.
*
* Note: Not supported in MT4.
*/
virtual void SetHandle(int _handle) {}
/**
* Sets indicator's symbol.
*/
void SetSymbol(string _symbol) { Set<string>(CHART_PARAM_SYMBOL, _symbol); }
/* Other methods */
/**
* Releases indicator's handle.
*
* Note: Not supported in MT4.
*/
void ReleaseHandle() {
#ifdef __MQL5__
if (istate.handle != INVALID_HANDLE) {
IndicatorRelease(istate.handle);
}
#endif
istate.handle = INVALID_HANDLE;
istate.is_changed = true;
}
/* Data representation methods */
/* Virtual methods */
/**
* Returns indicator value for a given shift and mode.
*/
// virtual double GetValue(int _shift = 0, int _mode = 0) = NULL;
/**
* Checks whether indicator has a valid value for a given shift.
*/
/*
virtual bool HasValidEntry(int _index = 0) {
unsigned int position;
long bar_time = GetBarTime(_index);
return bar_time > 0 && idata.KeyExists(bar_time, position) ? idata.GetByPos(position).IsValid() : false;
}
*/
/**
* Returns stored data in human-readable format.
*/
// virtual bool ToString() = NULL; // @fixme?
/**
* Gets indicator's symbol.
*/
string GetSymbol() { return Get<string>(CHART_PARAM_SYMBOL); }
/* Defines MQL backward compatible methods */
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, DUMMY);
#endif
}
template <typename A>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a);
#endif
}
template <typename A, typename B>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b);
#endif
}
template <typename A, typename B, typename C>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, int _mode,
int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c);
#endif
}
template <typename A, typename B, typename C, typename D>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, int _mode,
int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d);
#endif
}
template <typename A, typename B, typename C, typename D, typename E>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e,
int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
G _g, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _g, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
G _g, H _h, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _g, _h, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
G _g, H _h, I _i, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _g, _h, _i, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h COMMA _i);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
G _g, H _h, I _i, J _j, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h COMMA _i COMMA _j);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J, typename K>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
G _g, H _h, I _i, J _j, K _k, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h COMMA _i COMMA _j COMMA _k);
#endif
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J, typename K, typename L, typename M>
double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f,
G _g, H _h, I _i, J _j, K _k, L _l, M _m, int _mode, int _shift) {
#ifdef __MQL4__
return ::iCustom(_symbol, _tf, _name, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _mode, _shift);
#else // __MQL5__
ICUSTOM_DEF(;, COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h COMMA _i COMMA _j COMMA _k
COMMA _l COMMA _m);
#endif
}
};