-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Indicator.struct.cache.h
266 lines (221 loc) · 6.55 KB
/
Indicator.struct.cache.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
//+------------------------------------------------------------------+
//| 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
* IndicatorBufferValueStorage class.
*/
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif
// Includes.
#include "Refs.mqh"
#include "Storage/ValueStorage.h"
/**
* Holds buffers used to cache values calculated via OnCalculate methods.
*/
template <typename C>
class IndicatorCalculateCache : public Dynamic {
public:
// Total number of calculated values.
int prev_calculated;
// Number of prices to use.
int total;
// Whether cache was initialized with price buffer.
bool initialized;
// Buffer to store input prices. Won't be deleted!
ValueStorage<C> *price_buffer;
// Buffer to store input open prices. Won't be deleted!
ValueStorage<C> *price_open_buffer;
// Buffer to store input high prices. Won't be deleted!
ValueStorage<C> *price_high_buffer;
// Buffer to store input low prices. Won't be deleted!
ValueStorage<C> *price_low_buffer;
// Buffer to store input close prices. Won't be deleted!
ValueStorage<C> *price_close_buffer;
// Buffers used for OnCalculate calculations.
ARRAY(IValueStorage *, buffers);
// Auxiliary caches related to this one.
ARRAY(IndicatorCalculateCache<C> *, subcaches);
/**
* Constructor.
*/
IndicatorCalculateCache(int _buffers_size = 0) {
prev_calculated = 0;
total = 0;
initialized = false;
Resize(_buffers_size);
}
/**
* Destructor.
*/
~IndicatorCalculateCache() {
int i;
for (i = 0; i < ArraySize(buffers); ++i) {
if (buffers[i] != NULL) {
delete buffers[i];
}
}
for (i = 0; i < ArraySize(subcaches); ++i) {
if (subcaches[i] != NULL) {
delete subcaches[i];
}
}
}
/**
* Returns size of the current price buffer.
*/
int GetTotal() { return price_buffer != NULL ? ArraySize(price_buffer) : ArraySize(price_open_buffer); }
/**
* Returns number of already calculated prices (bars).
*/
int GetPrevCalculated() { return prev_calculated; }
/**
* Whether cache have any buffer.
*/
bool HasBuffers() { return ArraySize(buffers) != 0; }
/**
* Returns number of added buffers.
*/
int NumBuffers() { return ArraySize(buffers); }
/**
* Returns existing or new cache as a child of current one. Useful when indicator uses other indicators and requires
* unique caches for them.
*/
IndicatorCalculateCache<C> *GetSubCache(int index) {
if (index >= ArraySize(subcaches)) {
ArrayResize(subcaches, index + 1, 10);
}
if (subcaches[index] == NULL) {
subcaches[index] = new IndicatorCalculateCache();
}
return subcaches[index];
}
/**
* Add buffer of the given type. Usage: AddBuffer<NativeBuffer>()
*/
template <typename T>
int AddBuffer(int _num_buffers = 1) {
IValueStorage *_ptr;
while (_num_buffers-- > 0) {
_ptr = new T();
ArrayPushObject(buffers, _ptr);
}
return ArraySize(buffers) - 1;
}
/**
* Returns given calculation buffer.
*/
template <typename D>
ValueStorage<D> *GetBuffer(int _index) {
return (ValueStorage<D> *)buffers[_index];
}
/**
* Returns main price buffer.
*/
ValueStorage<C> *GetPriceBuffer() { return price_buffer; }
/**
* Returns given price buffer.
*/
ValueStorage<C> *GetPriceBuffer(ENUM_APPLIED_PRICE _applied_price) {
switch (_applied_price) {
case PRICE_OPEN:
return price_open_buffer;
case PRICE_HIGH:
return price_high_buffer;
case PRICE_LOW:
return price_low_buffer;
case PRICE_CLOSE:
return price_close_buffer;
}
return NULL;
}
/**
* Sets price buffer for later use.
*/
void SetPriceBuffer(ValueStorage<C> &_price, int _total = 0) {
price_buffer = &_price;
if (_total == 0) {
_total = _price.Size();
}
total = _total;
// Cache is ready to be used.
initialized = true;
}
/**
* Sets price buffers for later use.
*/
void SetPriceBuffer(ValueStorage<C> &_price_open, ValueStorage<C> &_price_high, ValueStorage<C> &_price_low,
ValueStorage<C> &_price_close, int _total = 0) {
price_open_buffer = &_price_open;
price_high_buffer = &_price_high;
price_low_buffer = &_price_low;
price_close_buffer = &_price_close;
if (_total == 0) {
_total = _price_open.Size();
}
total = _total;
// Cache is ready to be used.
initialized = true;
}
/**
* Resizes all buffers.
*/
void Resize(int _buffers_size) {
for (int i = 0; i < ArraySize(buffers); ++i) {
buffers[i].Resize(_buffers_size, 65535);
}
}
/**
* Retrieves cached value from the given buffer.
*/
template <typename D>
D GetValue(int _buffer_index, int _shift) {
return GetBuffer<D>(_buffer_index).Fetch(_shift).Get();
}
/**
*
*/
template <typename D>
D GetTailValue(int _buffer_index, int _shift) {
ValueStorage<D> *_buff = GetBuffer<D>(_buffer_index);
int _index = _buff.IsSeries() ? _shift : (ArraySize(_buff) - _shift - 1);
return _buff[_index].Get();
}
/**
* Updates prev_calculated value used by indicator's OnCalculate method.
*/
void SetPrevCalculated(int _prev_calculated) {
if (_prev_calculated == 0) {
ResetPrevCalculated();
} else {
prev_calculated = _prev_calculated;
}
}
/**
* Resets prev_calculated value used by indicator's OnCalculate method.
*/
void ResetPrevCalculated() { prev_calculated = 0; }
/**
* Returns prev_calculated value used by indicator's OnCalculate method.
*/
int GetPrevCalculated(int _prev_calculated) { return prev_calculated; }
};