-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIndicatorBase.h
640 lines (542 loc) · 17.1 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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, 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 "Indicator.struct.signal.h"
#include "Object.mqh"
#include "Refs.mqh"
#include "Serializer.mqh"
#include "SerializerCsv.mqh"
#include "SerializerJson.mqh"
#include "Storage/ValueStorage.h"
#include "Storage/ValueStorage.indicator.h"
#include "Storage/ValueStorage.native.h"
#include "Util.h"
/**
* Class to deal with indicators.
*/
class IndicatorBase : public Object {
protected:
IndicatorState istate;
void* mydata;
bool is_fed; // Whether calc_start_bar is already calculated.
int calc_start_bar; // Index of the first valid bar (from 0).
DictStruct<int, Ref<IndicatorBase>> indicators; // Indicators list keyed by id.
bool indicator_builtin;
ARRAY(ValueStorage<double>*, value_storages);
Ref<IndicatorBase> indi_src; // // Indicator used as data source.
int indi_src_mode; // Mode of source indicator
IndicatorCalculateCache<double> cache;
ARRAY(WeakRef<IndicatorBase>, listeners); // List of indicators that listens for events from this one.
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) : indi_src(NULL), Chart(_tf, _symbol) {
calc_start_bar = 0;
is_fed = false;
indi_src = NULL;
}
/**
* Class constructor.
*/
IndicatorBase(ENUM_TIMEFRAMES_INDEX _tfi, string _symbol = NULL) : Chart(_tfi, _symbol) {
calc_start_bar = 0;
is_fed = false;
indi_src = NULL;
}
/**
* Class deconstructor.
*/
virtual ~IndicatorBase() {
ReleaseHandle();
for (int i = 0; i < ArraySize(value_storages); ++i) {
if (value_storages[i] != NULL) {
delete value_storages[i];
}
}
}
/* Operator overloading methods */
/**
* Access indicator entry data using [] operator.
*/
// IndicatorDataEntry operator[](datetime _dt) { return GetEntry(_dt); }
IndicatorDataEntry operator[](int _index) { return GetEntry(_index); }
IndicatorDataEntry operator[](ENUM_INDICATOR_INDEX _index) { return GetEntry(_index); }
/* 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;
}
*/
/**
* Gets indicator data from a buffer and copy into struct array.
*
* @return
* Returns true of successful copy.
* Returns false on invalid values.
*/
bool CopyEntries(IndicatorDataEntry& _data[], int _count, int _start_shift = 0) {
bool _is_valid = true;
if (ArraySize(_data) < _count) {
_is_valid &= ArrayResize(_data, _count) > 0;
}
for (int i = 0; i < _count; i++) {
IndicatorDataEntry _entry = GetEntry(_start_shift + i);
_is_valid &= _entry.IsValid();
_data[i] = _entry;
}
return _is_valid;
}
/**
* Gets indicator data from a buffer and copy into array of values.
*
* @return
* Returns true of successful copy.
* Returns false on invalid values.
*/
template <typename T>
bool CopyValues(T& _data[], int _count, int _start_shift = 0, int _mode = 0) {
bool _is_valid = true;
if (ArraySize(_data) < _count) {
_count = ArrayResize(_data, _count);
_count = _count > 0 ? _count : ArraySize(_data);
}
for (int i = 0; i < _count; i++) {
IndicatorDataEntry _entry = GetEntry(_start_shift + i);
_is_valid &= _entry.IsValid();
_data[i] = (T)_entry[_mode];
}
return _is_valid;
}
/**
* Validates currently selected indicator used as data source.
*/
void ValidateSelectedDataSource() {
if (HasDataSource()) {
ValidateDataSource(THIS_PTR, GetDataSourceRaw());
}
}
/**
* Loads and validates built-in indicators whose can be used as data source.
*/
virtual void ValidateDataSource(IndicatorBase* _target, IndicatorBase* _source) {}
/**
* Checks whether indicator have given mode index.
*
* If given mode is -1 (default one) and indicator has exactly one mode, then mode index will be replaced by 0.
*/
virtual void ValidateDataSourceMode(int& _out_mode) {}
/**
* Provides built-in indicators whose can be used as data source.
*/
virtual IndicatorBase* FetchDataSource(ENUM_INDICATOR_TYPE _id) { return NULL; }
/**
* Returns currently selected data source without any validation.
*/
IndicatorBase* GetDataSourceRaw() { return indi_src.Ptr(); }
/* Getters */
/**
* Returns buffers' cache.
*/
IndicatorCalculateCache<double>* GetCache() { return &cache; }
/**
* Gets an indicator's state property value.
*/
template <typename T>
T Get(STRUCT_ENUM(IndicatorState, 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() { return false; }
/**
* Returns currently selected data source doing validation.
*/
virtual IndicatorBase* GetDataSource() { return NULL; }
int GetDataSourceMode() { return indi_src_mode; }
/**
* 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 "<Unknown>"; }
/**
* 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);
}
/**
* Adds event listener.
*/
void AddListener(IndicatorBase* _indi) {
WeakRef<IndicatorBase> _ref = _indi;
ArrayPushObject(listeners, _ref);
}
/**
* Removes event listener.
*/
void RemoveListener(IndicatorBase* _indi) {
WeakRef<IndicatorBase> _ref = _indi;
Util::ArrayRemoveFirst(listeners, _ref);
}
/**
* Sets indicator data source.
*/
virtual void SetDataSource(IndicatorBase* _indi, int _input_mode = 0) = NULL;
/**
* Sets data source's input mode.
*/
void SetDataSourceMode(int _mode) { indi_src_mode = _mode; }
/**
* 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;
}
ValueStorage<double>* GetValueStorage(int _mode = 0) {
if (_mode >= ArraySize(value_storages)) {
ArrayResize(value_storages, _mode + 1);
}
if (value_storages[_mode] == NULL) {
value_storages[_mode] = new IndicatorBufferValueStorage<double>(THIS_PTR, _mode);
}
return value_storages[_mode];
}
template <typename T>
T GetValue(int _index = 0, int _mode = 0) {
T _out;
GetEntryValue(_index, _mode).Get(_out);
return _out;
}
/**
* Returns values for a given shift.
*
* Note: Remember to check if shift exists by HasValidEntry(shift).
*/
template <typename T>
bool GetValues(int _index, T& _out1, T& _out2) {
IndicatorDataEntry _entry = GetEntry(_index);
_out1 = _entry.values[0];
_out2 = _entry.values[1];
bool _result = GetLastError() != 4401;
ResetLastError();
return _result;
}
template <typename T>
bool GetValues(int _index, T& _out1, T& _out2, T& _out3) {
IndicatorDataEntry _entry = GetEntry(_index);
_out1 = _entry.values[0];
_out2 = _entry.values[1];
_out3 = _entry.values[2];
bool _result = GetLastError() != 4401;
ResetLastError();
return _result;
}
template <typename T>
bool GetValues(int _index, T& _out1, T& _out2, T& _out3, T& _out4) {
IndicatorDataEntry _entry = GetEntry(_index);
_out1 = _entry.values[0];
_out2 = _entry.values[1];
_out3 = _entry.values[2];
_out4 = _entry.values[3];
bool _result = GetLastError() != 4401;
ResetLastError();
return _result;
}
virtual void OnTick() {}
/* Data representation methods */
/* Virtual methods */
/**
* Returns the indicator's struct value.
*/
virtual IndicatorDataEntry GetEntry(int _index = -1) = NULL;
/**
* Alters indicator's struct value.
*
* This method allows user to modify the struct entry before it's added to cache.
* This method is called on GetEntry() right after values are set.
*/
virtual void GetEntryAlter(IndicatorDataEntry& _entry, int _index = -1) = NULL;
// virtual ENUM_IDATA_VALUE_RANGE GetIDataValueRange() = NULL;
/**
* Returns the indicator's entry value.
*/
virtual IndicatorDataEntryValue GetEntryValue(int _mode = 0, int _index = -1) = NULL;
/**
* Sends entry to listening indicators.
*/
void EmitEntry(IndicatorDataEntry& entry) {
for (int i = 0; i < ArraySize(listeners); ++i) {
if (listeners[i].ObjectExists()) {
listeners[i].Ptr().OnDataSourceEntry(entry);
}
}
}
/**
* Sends historic entries to listening indicators. May be overriden.
*/
virtual void EmitHistory() {}
/**
* Called when data source emits new entry (historic or future one).
*/
virtual void OnDataSourceEntry(IndicatorDataEntry& entry){};
/**
* Called when indicator became a data source for other indicator.
*/
virtual void OnBecomeDataSourceFor(IndicatorBase* _base_indi){};
/**
* Returns indicator value for a given shift and mode.
*/
// virtual double GetValue(int _shift = -1, 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?
/**
* Whether we can and have to select mode when specifying data source.
*/
virtual bool IsDataSourceModeSelectable() { return true; }
/**
* Update indicator.
*/
virtual bool Update() {
// @todo
return false;
};
/**
* Returns the indicator's value in plain format.
*/
virtual string ToString(int _index = 0) {
IndicatorDataEntry _entry = GetEntry(_index);
int _serializer_flags = SERIALIZER_FLAG_SKIP_HIDDEN | SERIALIZER_FLAG_INCLUDE_DEFAULT |
SERIALIZER_FLAG_INCLUDE_DYNAMIC | SERIALIZER_FLAG_INCLUDE_FEATURE;
IndicatorDataEntry _stub_entry;
_stub_entry.AddFlags(_entry.GetFlags());
SerializerConverter _stub = SerializerConverter::MakeStubObject(_stub_entry, _serializer_flags, _entry.GetSize());
return SerializerConverter::FromObject(_entry, _serializer_flags).ToString<SerializerCsv>(0, &_stub);
}
/* @todo
int GetBarsCalculated() {
int _bars = Bars(GetSymbol(), GetTf());
if (!is_fed) {
// Calculating start_bar.
for (; calc_start_bar < _bars; ++calc_start_bar) {
// Iterating from the oldest or previously iterated.
IndicatorDataEntry _entry = GetEntry(_bars - calc_start_bar - 1);
if (_entry.IsValid()) {
// From this point we assume that future entries will be all valid.
is_fed = true;
return _bars - calc_start_bar;
}
}
}
if (!is_fed) {
Print("Can't find valid bars for ", GetFullName());
return 0;
}
// Assuming all entries are calculated (even if have invalid values).
return _bars;
}
*/
/* Methods to get rid of */
/**
* Gets indicator's symbol.
*/
string GetSymbol() { return Get<string>(CHART_PARAM_SYMBOL); }
/**
* Gets indicator's time-frame.
*/
ENUM_TIMEFRAMES GetTf() { return Get<ENUM_TIMEFRAMES>(CHART_PARAM_TF); }
};
/**
* CopyBuffer() method to be used on Indicator instance with ValueStorage buffer.
*
* Note that data will be copied so that the oldest element will be located at the start of the physical memory
* allocated for the array
*/
template <typename T>
int CopyBuffer(IndicatorBase* _indi, int _mode, int _start, int _count, ValueStorage<T>& _buffer, int _rates_total) {
int _num_copied = 0;
int _buffer_size = ArraySize(_buffer);
if (_buffer_size < _rates_total) {
_buffer_size = ArrayResize(_buffer, _rates_total);
}
for (int i = _start; i < _count; ++i) {
IndicatorDataEntry _entry = _indi.GetEntry(i);
if (!_entry.IsValid()) {
break;
}
T _value = _entry.GetValue<T>(_mode);
// Print(_value);
_buffer[_buffer_size - i - 1] = _value;
++_num_copied;
}
return _num_copied;
}
/**
* BarsCalculated()-compatible method to be used on Indicator instance.
*/
// int BarsCalculated(IndicatorBase* _indi) { return _indi.GetBarsCalculated(); } // @todo