forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndicatorData.struct.h
506 lines (491 loc) · 14.7 KB
/
IndicatorData.struct.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//+------------------------------------------------------------------+
//| 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
* Includes IndicatorData's structs.
*/
// Defines.
#define STRUCT_ENUM_IDATA_PARAM STRUCT_ENUM(IndicatorDataParams, ENUM_IDATA_PARAM)
// Includes.
#include "Indicator.struct.cache.h"
#include "SerializerNode.enum.h"
#include "Storage/ValueStorage.indicator.h"
// Type-less value for IndicatorDataEntryValue structure.
union IndicatorDataEntryTypelessValue {
double vdbl;
float vflt;
int vint;
long vlong;
};
// Type-aware value for IndicatorDataEntry class.
struct IndicatorDataEntryValue {
unsigned char flags;
IndicatorDataEntryTypelessValue value;
// Returns type of the value.
ENUM_DATATYPE GetDataType() { return (ENUM_DATATYPE)((flags & 0xF0) >> 4); }
// Sets type of the value.
void SetDataType(ENUM_DATATYPE _type) {
// Clearing type.
flags &= 0x0F;
// Setting type.
flags |= (unsigned char)_type << 4;
}
// Union operators.
template <typename T>
T operator*(const T _value) {
return Get<T>() * _value;
}
template <typename T>
T operator+(const T _value) {
return Get<T>() + _value;
}
template <typename T>
T operator-(const T _value) {
return Get<T>() - _value;
}
template <typename T>
T operator/(const T _value) {
return Get<T>() / _value;
}
template <typename T>
bool operator!=(const T _value) {
return Get<T>() != _value;
}
template <typename T>
bool operator<(const T _value) {
return Get<T>() < _value;
}
template <typename T>
bool operator<=(const T _value) {
return Get<T>() <= _value;
}
template <typename T>
bool operator==(const T _value) {
return Get<T>() == _value;
}
template <typename T>
bool operator>(const T _value) {
return Get<T>() > _value;
}
template <typename T>
bool operator>=(const T _value) {
return Get<T>() >= _value;
}
template <typename T>
void operator=(const T _value) {
Set(_value);
}
// Checkers.
template <typename T>
bool IsGt(T _value) {
return Get<T>() > _value;
}
template <typename T>
bool IsLt(T _value) {
return Get<T>() < _value;
}
// Getters.
double GetDbl() { return value.vdbl; }
float GetFloat() { return value.vflt; }
int GetInt() { return value.vint; }
long GetLong() { return value.vlong; }
template <typename T>
void Get(T &_out) {
_out = Get<T>();
}
template <typename T>
T Get() {
T _v;
Get(_v);
return _v;
}
void Get(datetime &_out) { _out = (datetime)value.vlong; }
void Get(double &_out) { _out = value.vdbl; }
void Get(float &_out) { _out = value.vflt; }
void Get(int &_out) { _out = value.vint; }
void Get(unsigned int &_out) { _out = (unsigned int)value.vint; }
void Get(long &_out) { _out = value.vlong; }
void Get(unsigned long &_out) { _out = (unsigned long)value.vint; }
// Setters.
template <typename T>
void Set(T _value) {
Set(_value);
}
void Set(double _value) {
value.vdbl = _value;
SetDataType(TYPE_DOUBLE);
}
void Set(float _value) {
value.vflt = _value;
SetDataType(TYPE_FLOAT);
}
void Set(int _value) {
value.vint = _value;
SetDataType(TYPE_INT);
}
void Set(unsigned int _value) {
value.vint = (int)_value;
SetDataType(TYPE_UINT);
}
void Set(long _value) {
value.vlong = _value;
SetDataType(TYPE_LONG);
}
void Set(unsigned long _value) {
value.vlong = (long)_value;
SetDataType(TYPE_ULONG);
}
// Serializers.
// SERIALIZER_EMPTY_STUB
SerializerNodeType Serialize(Serializer &_s);
// To string
template <typename T>
string ToString() {
return (string)Get<T>();
}
};
/* Structure for indicator data entry. */
struct IndicatorDataEntry {
long timestamp; // Timestamp of the entry's bar.
unsigned short flags; // Indicator entry flags.
ARRAY(IndicatorDataEntryValue, values);
// Constructors.
IndicatorDataEntry(int _size = 1) : flags(INDI_ENTRY_FLAG_NONE), timestamp(0) { Resize(_size); }
IndicatorDataEntry(IndicatorDataEntry &_entry) { THIS_REF = _entry; }
int GetSize() { return ArraySize(values); }
// Operator overloading methods.
template <typename T>
T operator*(const T _value) {
return values[0].Get<T>() * _value;
}
template <typename T>
T operator+(const T _value) {
return values[0].Get<T>() + _value;
}
template <typename T>
T operator-(const T _value) {
return values[0].Get<T>() - _value;
}
template <typename T>
T operator/(const T _value) {
return values[0].Get<T>() / _value;
}
template <typename T, typename I>
T operator[](I _index) {
return values[(int)_index].Get<T>();
}
template <>
double operator[](int _index) {
if (_index >= ArraySize(values)) {
return 0;
}
double _value;
values[_index].Get(_value);
return _value;
}
// Checkers.
template <typename T>
bool HasValue(T _value) {
bool _result = false;
int _asize = ArraySize(values);
T _value2;
for (int i = 0; i < _asize; i++) {
values[i].Get(_value2);
if (_value == _value2) {
_result = true;
break;
}
}
return _result;
}
template <typename T>
bool IsGe(T _value) {
return GetMin<T>() >= _value;
}
template <typename T>
bool IsGt(T _value) {
return GetMin<T>() > _value;
}
template <typename T>
bool IsLe(T _value) {
return GetMax<T>() <= _value;
}
template <typename T>
bool IsLt(T _value) {
return GetMax<T>() < _value;
}
template <typename T>
bool IsWithinRange(T _min, T _max) {
return GetMin<T>() >= _min && GetMax<T>() <= _max;
}
// Getters.
template <typename T>
void GetArray(ARRAY_REF(T, _out), int _size = 0) {
int _asize = _size > 0 ? _size : ArraySize(_out);
for (int i = 0; i < _asize; i++) {
values[i].Get(_out[i]);
}
};
template <typename T>
T GetAvg(int _size = 0) {
int _asize = _size > 0 ? _size : ArraySize(values);
T _avg = GetSum<T>() / _asize;
return _avg;
};
template <typename T>
T GetMin(int _size = 0) {
int _asize = _size > 0 ? _size : ArraySize(values);
int _index = 0;
for (int i = 1; i < _asize; i++) {
_index = values[i].Get<T>() < values[_index].Get<T>() ? i : _index;
}
return values[_index].Get<T>();
};
template <typename T>
T GetMax(int _size = 0) {
int _asize = _size > 0 ? _size : ArraySize(values);
int _index = 0;
for (int i = 1; i < _asize; i++) {
_index = values[i].Get<T>() > values[_index].Get<T>() ? i : _index;
}
return values[_index].Get<T>();
};
template <typename T>
T GetSum(int _size = 0) {
int _asize = _size > 0 ? _size : ArraySize(values);
T _sum = 0;
for (int i = 1; i < _asize; i++) {
_sum = +values[i].Get<T>();
}
return _sum;
};
template <typename T>
T GetValue(int _index = 0) {
return values[_index].Get<T>();
};
template <typename T>
void GetValues(T &_out1, T &_out2) {
values[0].Get(_out1);
values[1].Get(_out2);
};
template <typename T>
void GetValues(T &_out1, T &_out2, T &_out3) {
values[0].Get(_out1);
values[1].Get(_out2);
values[2].Get(_out3);
};
template <typename T>
void GetValues(T &_out1, T &_out2, T &_out3, T &_out4) {
values[0].Get(_out1);
values[1].Get(_out2);
values[2].Get(_out3);
values[3].Get(_out4);
};
// Getters.
int GetDayOfYear() { return DateTimeStatic::DayOfYear(timestamp); }
int GetMonth() { return DateTimeStatic::Month(timestamp); }
int GetYear() { return DateTimeStatic::Year(timestamp); }
long GetTime() { return timestamp; };
ENUM_DATATYPE GetDataType(int _mode) { return values[_mode].GetDataType(); }
ushort GetDataTypeFlags(ENUM_DATATYPE _dt) {
switch (_dt) {
case TYPE_BOOL:
case TYPE_CHAR:
SetUserError(ERR_INVALID_PARAMETER);
break;
case TYPE_INT:
return INDI_ENTRY_FLAG_NONE;
case TYPE_LONG:
return INDI_ENTRY_FLAG_IS_DOUBLED;
case TYPE_UINT:
return INDI_ENTRY_FLAG_IS_UNSIGNED;
case TYPE_ULONG:
return INDI_ENTRY_FLAG_IS_UNSIGNED | INDI_ENTRY_FLAG_IS_DOUBLED;
case TYPE_DOUBLE:
return INDI_ENTRY_FLAG_IS_REAL | INDI_ENTRY_FLAG_IS_DOUBLED;
case TYPE_FLOAT:
return INDI_ENTRY_FLAG_IS_REAL;
case TYPE_STRING:
case TYPE_UCHAR:
SetUserError(ERR_INVALID_PARAMETER);
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
break;
}
SetUserError(ERR_INVALID_PARAMETER);
return INDI_ENTRY_FLAG_NONE;
}
// Setters.
bool Resize(int _size = 0) { return _size > 0 ? ArrayResize(values, _size) > 0 : true; }
// Value flag methods for bitwise operations.
bool CheckFlag(INDICATOR_ENTRY_FLAGS _prop) { return CheckFlags(_prop); }
bool CheckFlags(unsigned short _flags) { return (flags & _flags) != 0; }
bool CheckFlagsAll(unsigned short _flags) { return (flags & _flags) == _flags; }
void AddFlags(unsigned short _flags) { flags |= _flags; }
void RemoveFlags(unsigned short _flags) { flags &= ~_flags; }
void SetFlag(INDICATOR_ENTRY_FLAGS _flag, bool _value) {
if (_value) {
AddFlags(_flag);
} else {
RemoveFlags(_flag);
}
}
void SetFlags(unsigned short _flags) { flags = _flags; }
unsigned short GetFlags() { return flags; }
// Converters.
// State checkers.
bool IsValid() { return CheckFlags(INDI_ENTRY_FLAG_IS_VALID); }
// Serializers.
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {
ArrayResize(values, _n1);
for (int i = 0; i < _n1; ++i) {
values[i] = (int)1;
}
}
SerializerNodeType Serialize(Serializer &_s);
template <typename T>
string ToCSV() {
int _asize = ArraySize(values);
string _result = "";
for (int i = 0; i < _asize; i++) {
_result += StringFormat("%s%s", (string)values[i].Get<T>(), i < _asize ? "," : "");
}
return _result;
}
template <typename T>
string ToString() {
return ToCSV<T>();
}
};
/* Structure for indicator data parameters. */
struct IndicatorDataParams {
protected:
/* Struct protected variables */
bool is_fed; // Whether calc_start_bar is already calculated.
int data_src_mode; // Mode used as input from data source.
int src_id; // Id of the indicator to be used as data source.
int src_mode; // Mode of source indicator
unsigned int max_buffers; // Max buffers to store.
unsigned int max_modes; // Max supported indicator modes (values per entry).
ENUM_DATATYPE dtype; // Type of basic data to store values (DTYPE_DOUBLE, DTYPE_INT).
ENUM_IDATA_SOURCE_TYPE idstype; // Indicator's data source type (e.g. IDATA_BUILTIN, IDATA_ICUSTOM).
ENUM_IDATA_VALUE_RANGE idvrange; // Indicator's range value data type.
public:
/* Struct enumerations */
enum ENUM_IDATA_PARAM {
IDATA_PARAM_IS_FED = 0,
IDATA_PARAM_DATA_SRC_MODE,
IDATA_PARAM_DTYPE,
IDATA_PARAM_IDSTYPE,
IDATA_PARAM_IDVRANGE,
IDATA_PARAM_MAX_BUFFERS,
IDATA_PARAM_MAX_MODES,
IDATA_PARAM_SRC_ID,
IDATA_PARAM_SRC_MODE,
};
public:
/* Special methods */
// Constructor.
IndicatorDataParams(unsigned int _max_modes = 1, ENUM_DATATYPE _dtype = TYPE_DOUBLE,
ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN,
ENUM_IDATA_VALUE_RANGE _idvrange = IDATA_RANGE_UNKNOWN, int _data_src_mode = 0)
: data_src_mode(_data_src_mode),
dtype(_dtype),
max_modes(_max_modes),
max_buffers(10),
idstype(_idstype),
idvrange(_idvrange),
is_fed(false),
src_id(-1),
src_mode(-1){};
// Copy constructor.
IndicatorDataParams(const IndicatorDataParams &_idp) { THIS_REF = _idp; }
// Deconstructor.
~IndicatorDataParams(){};
/* Getters */
template <typename T>
T Get(STRUCT_ENUM_IDATA_PARAM _param) {
switch (_param) {
case IDATA_PARAM_IS_FED:
return (T)is_fed;
case IDATA_PARAM_DATA_SRC_MODE:
return (T)data_src_mode;
case IDATA_PARAM_DTYPE:
return (T)dtype;
case IDATA_PARAM_IDSTYPE:
return (T)idstype;
case IDATA_PARAM_IDVRANGE:
return (T)idvrange;
case IDATA_PARAM_MAX_BUFFERS:
return (T)max_buffers;
case IDATA_PARAM_MAX_MODES:
return (T)max_modes;
case IDATA_PARAM_SRC_ID:
return (T)src_id;
case IDATA_PARAM_SRC_MODE:
return (T)src_mode;
}
SetUserError(ERR_INVALID_PARAMETER);
return (T)WRONG_VALUE;
}
static IndicatorDataParams GetInstance(unsigned int _max_modes = 1, ENUM_DATATYPE _dtype = TYPE_DOUBLE,
ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN,
ENUM_IDATA_VALUE_RANGE _idvrange = IDATA_RANGE_UNKNOWN,
int _data_src_mode = 0) {
IndicatorDataParams _instance(_max_modes, _dtype, _idstype, _idvrange, _data_src_mode);
return _instance;
}
/* Setters */
template <typename T>
void Set(STRUCT_ENUM_IDATA_PARAM _param, T _value) {
switch (_param) {
case IDATA_PARAM_IS_FED:
is_fed = (bool)_value;
return;
case IDATA_PARAM_DATA_SRC_MODE:
data_src_mode = (int)_value;
return;
case IDATA_PARAM_DTYPE:
dtype = (ENUM_DATATYPE)_value;
return;
case IDATA_PARAM_IDSTYPE:
idstype = (ENUM_IDATA_SOURCE_TYPE)_value;
return;
case IDATA_PARAM_IDVRANGE:
idvrange = (ENUM_IDATA_VALUE_RANGE)_value;
return;
case IDATA_PARAM_MAX_BUFFERS:
max_buffers = (unsigned int)_value;
return;
case IDATA_PARAM_MAX_MODES:
max_modes = (unsigned int)_value;
return;
case IDATA_PARAM_SRC_ID:
src_id = (int)_value;
return;
case IDATA_PARAM_SRC_MODE:
src_mode = (int)_value;
return;
}
SetUserError(ERR_INVALID_PARAMETER);
}
};