-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIndi_MA.mqh
767 lines (691 loc) · 26.5 KB
/
Indi_MA.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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//+------------------------------------------------------------------+
//| 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/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef INDI_MA_MQH
#define INDI_MA_MQH
// Includes.
#include "../Dict.mqh"
#include "../DictObject.mqh"
#include "../Indicator/IndicatorTickSource.h"
#include "../Refs.mqh"
#include "../Storage/Singleton.h"
#include "../Storage/ValueStorage.h"
#include "../String.mqh"
#ifndef __MQL4__
// Defines global functions (for MQL4 backward compability).
double iMA(string _symbol, int _tf, int _ma_period, int _ma_shift, int _ma_method, int _ap, int _shift) {
ResetLastError();
return Indi_MA::iMA(_symbol, (ENUM_TIMEFRAMES)_tf, _ma_period, _ma_shift, (ENUM_MA_METHOD)_ma_method,
(ENUM_APPLIED_PRICE)_ap, _shift);
}
double iMAOnArray(double &_arr[], int _total, int _period, int _ma_shift, int _ma_method, int _shift,
IndicatorCalculateCache<double> *_cache = NULL) {
ResetLastError();
return Indi_MA::iMAOnArray(_arr, _total, _period, _ma_shift, _ma_method, _shift, _cache);
}
#endif
// Structs.
struct IndiMAParams : IndicatorParams {
unsigned int period;
unsigned int ma_shift;
ENUM_MA_METHOD ma_method;
ENUM_APPLIED_PRICE applied_array;
// Struct constructors.
IndiMAParams(unsigned int _period = 13, int _ma_shift = 10, ENUM_MA_METHOD _ma_method = MODE_SMA,
ENUM_APPLIED_PRICE _ap = PRICE_OPEN, int _shift = 0)
: period(_period), ma_shift(_ma_shift), ma_method(_ma_method), applied_array(_ap), IndicatorParams(INDI_MA) {
shift = _shift;
SetCustomIndicatorName("Examples\\Moving Average");
};
IndiMAParams(IndiMAParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
/**
* Implements the Moving Average indicator.
*/
class Indi_MA : public IndicatorTickSource<IndiMAParams> {
public:
/**
* Class constructor.
*/
Indi_MA(IndiMAParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN, IndicatorData *_indi_src = NULL,
int _indi_src_mode = 0)
: IndicatorTickSource(
_p, IndicatorDataParams::GetInstance(1, TYPE_DOUBLE, _idstype, IDATA_RANGE_PRICE, _indi_src_mode),
_indi_src) {}
Indi_MA(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0) : IndicatorTickSource(INDI_MA, _tf, _shift) {}
/**
* Returns the indicator value.
*
* @docs
* - https://docs.mql4.com/indicators/ima
* - https://www.mql5.com/en/docs/indicators/ima
*/
static double iMA(string _symbol, ENUM_TIMEFRAMES _tf, unsigned int _ma_period, unsigned int _ma_shift,
ENUM_MA_METHOD _ma_method, ENUM_APPLIED_PRICE _applied_array, int _shift = 0,
IndicatorData *_obj = NULL) {
#ifdef __MQL4__
return ::iMA(_symbol, _tf, _ma_period, _ma_shift, _ma_method, _applied_array, _shift);
#else // __MQL5__
int _handle = Object::IsValid(_obj) ? _obj.Get<int>(IndicatorState::INDICATOR_STATE_PROP_HANDLE) : NULL;
double _res[];
if (_handle == NULL || _handle == INVALID_HANDLE) {
if ((_handle = ::iMA(_symbol, _tf, _ma_period, _ma_shift, _ma_method, _applied_array)) == INVALID_HANDLE) {
SetUserError(ERR_USER_INVALID_HANDLE);
return EMPTY_VALUE;
} else if (Object::IsValid(_obj)) {
_obj.SetHandle(_handle);
}
}
if (Terminal::IsVisualMode()) {
// To avoid error 4806 (ERR_INDICATOR_DATA_NOT_FOUND),
// we check the number of calculated data only in visual mode.
int _bars_calc = BarsCalculated(_handle);
if (GetLastError() > 0) {
return EMPTY_VALUE;
} else if (_bars_calc <= 2) {
SetUserError(ERR_USER_INVALID_BUFF_NUM);
return EMPTY_VALUE;
}
}
if (CopyBuffer(_handle, 0, _shift, 1, _res) < 0) {
return ArraySize(_res) > 0 ? _res[0] : EMPTY_VALUE;
}
return _res[0];
#endif
}
/**
* Calculates MA on another indicator.
*/
static double iMAOnIndicator(IndicatorCalculateCache<double> *cache, IndicatorData *_indi, int indi_mode,
string symbol, ENUM_TIMEFRAMES tf, unsigned int ma_period, unsigned int ma_shift,
ENUM_MA_METHOD ma_method, // (MT4/MT5): MODE_SMA, MODE_EMA, MODE_SMMA, MODE_LWMA
int shift = 0) {
return iMAOnArray(_indi.GetValueStorage(indi_mode), 0, ma_period, ma_shift, ma_method, shift, cache);
}
/**
* Calculates MA on the array of values. Cache is optional.
*/
static double iMAOnArray(double &price[], int total, int ma_period, int ma_shift, int ma_method, int shift,
IndicatorCalculateCache<double> *cache = NULL) {
#ifdef __MQL4__
return ::iMAOnArray(price, total, ma_period, ma_shift, ma_method, shift);
#else
// We're reusing the same native array for each consecutive calculation.
NativeValueStorage<double> *_array_storage = Singleton<NativeValueStorage<double>>::Get();
_array_storage.SetData(price);
return iMAOnArray((ValueStorage<double> *)_array_storage, total, ma_period, ma_shift, ma_method, shift, cache);
#endif
}
/**
* Calculates MA on the array of values.
*/
static double iMAOnArray(ValueStorage<double> &price, int total, int ma_period, int ma_shift, int ma_method,
int shift, IndicatorCalculateCache<double> *_cache = NULL, bool recalculate = false) {
if (_cache != NULL) {
_cache.SetPriceBuffer(price);
if (!_cache.HasBuffers()) {
_cache.AddBuffer<NativeValueStorage<double>>();
}
if (recalculate) {
_cache.ResetPrevCalculated();
}
_cache.SetPrevCalculated(
Indi_MA::Calculate(INDICATOR_CALCULATE_GET_PARAMS_SHORT, _cache.GetBuffer<double>(0), ma_method, ma_period));
// Returns value from the first calculation buffer.
// Returns first value for as-series array or last value for non-as-series array.
return _cache.GetTailValue<double>(0, shift + ma_shift);
}
double buf[], arr[], _result, pr, _array;
int pos, i, k, weight;
double sum, lsum;
if (total == 0) total = ArraySize(price);
if (total > 0 && total < ma_period) return (0);
if (shift > total - ma_period - ma_shift) return (0);
bool _was_series = ArrayGetAsSeries(price);
ArraySetAsSeries(price, true);
switch (ma_method) {
case MODE_SMA:
total = ArrayCopy(arr, price, 0, shift + ma_shift, ma_period);
if (ArrayResize(buf, total) < 0) return (0);
sum = 0;
pos = total - 1;
for (i = 1; i < ma_period; i++, pos--) sum += arr[pos];
while (pos >= 0) {
sum += arr[pos];
buf[pos] = sum / ma_period;
sum -= arr[pos + ma_period - 1];
pos--;
}
_result = buf[0];
break;
case MODE_EMA:
if (ArrayResize(buf, total) < 0) return (0);
pr = 2.0 / (ma_period + 1);
pos = total - 2;
while (pos >= 0) {
if (pos == total - 2) buf[pos + 1] = price[pos + 1].Get();
buf[pos] = price[pos] * pr + buf[pos + 1] * (1 - pr);
pos--;
}
_result = buf[0];
break;
case MODE_SMMA:
if (ArrayResize(buf, total) < 0) return (0);
sum = 0;
pos = total - ma_period;
while (pos >= 0) {
if (pos == total - ma_period) {
for (i = 0, k = pos; i < ma_period; i++, k++) {
sum += price[k].Get();
buf[k] = 0;
}
} else
sum = buf[pos + 1] * (ma_period - 1) + price[pos].Get();
buf[pos] = sum / ma_period;
pos--;
}
_result = buf[0];
break;
case MODE_LWMA:
if (ArrayResize(buf, total) < 0) return (0);
sum = 0.0;
lsum = 0.0;
weight = 0;
pos = total - 1;
for (i = 1; i <= ma_period; i++, pos--) {
_array = price[pos].Get();
sum += _array * i;
lsum += _array;
weight += i;
}
pos++;
i = pos + ma_period;
while (pos >= 0) {
buf[pos] = sum / weight;
if (pos == 0) break;
pos--;
i--;
_array = price[pos].Get();
sum = sum - lsum + _array * ma_period;
lsum -= price[i].Get();
lsum += _array;
}
_result = buf[0];
break;
default:
_result = 0;
}
ArraySetAsSeries(price, _was_series);
return _result;
}
/**
* Calculates Simple Moving Average (SMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateSimpleMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
int i, start;
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
start = _ma_period + begin;
// Set empty value for first start bars.
for (i = 0; i < start - 1; i++) ExtLineBuffer[i] = 0.0;
// Calculate first visible value.
double first_value = 0;
for (i = begin; i < start; i++) first_value += price[i].Get();
first_value /= _ma_period;
ExtLineBuffer[start - 1] = first_value;
} else
start = prev_calculated - 1;
// Main loop.
for (i = start; i < rates_total && !IsStopped(); i++) {
ExtLineBuffer[i] = ExtLineBuffer[i - 1] + (price[i] - price[i - _ma_period]) / _ma_period;
}
}
/**
* Calculates Exponential Moving Average (EMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateEMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
int i, limit;
double SmoothFactor = 2.0 / (1.0 + _ma_period);
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
limit = _ma_period + begin;
ExtLineBuffer[begin] = price[begin];
for (i = begin + 1; i < limit; i++) {
ExtLineBuffer[i] = price[i] * SmoothFactor + ExtLineBuffer[i - 1] * (1.0 - SmoothFactor);
}
} else
limit = prev_calculated - 1;
// Main loop.
for (i = limit; i < rates_total && !IsStopped(); i++) {
ExtLineBuffer[i] = price[i] * SmoothFactor + ExtLineBuffer[i - 1] * (1.0 - SmoothFactor);
}
}
/**
* Calculates Linearly Weighted Moving Average (LWMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateLWMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
int i, limit;
static int weightsum;
double sum;
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
weightsum = 0;
limit = _ma_period + begin;
// Set empty value for first limit bars.
for (i = 0; i < limit; i++) ExtLineBuffer[i] = 0.0;
// Calculate first visible value.
double firstValue = 0;
for (i = begin; i < limit; i++) {
int k = i - begin + 1;
weightsum += k;
firstValue += k * price[i].Get();
}
firstValue /= (double)weightsum;
ExtLineBuffer[limit - 1] = firstValue;
} else
limit = prev_calculated - 1;
// Main loop.
for (i = limit; i < rates_total && !IsStopped(); i++) {
sum = 0;
for (int j = 0; j < _ma_period; j++) sum += (_ma_period - j) * price[i - j].Get();
ExtLineBuffer[i] = sum / weightsum;
}
//---
}
/**
* Calculates Smoothed Moving Average (SMMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateSmoothedMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
int i, limit;
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
limit = _ma_period + begin;
// Set empty value for first limit bars.
for (i = 0; i < limit - 1; i++) ExtLineBuffer[i] = 0.0;
// Calculate first visible value.
double firstValue = 0;
for (i = begin; i < limit; i++) firstValue += price[i].Get();
firstValue /= _ma_period;
ExtLineBuffer[limit - 1] = firstValue;
} else
limit = prev_calculated - 1;
// Main loop.
for (i = limit; i < rates_total && !IsStopped(); i++)
ExtLineBuffer[i] = (ExtLineBuffer[i - 1] * (_ma_period - 1) + price[i].Get()) / _ma_period;
//---
}
static double ExponentialMA(const int position, const int period, const double prev_value,
ValueStorage<double> &price) {
double result = 0.0;
// Check period.
if (period > 0) {
double pr = 2.0 / (period + 1.0);
result = price[position] * pr + prev_value * (1 - pr);
}
return (result);
}
static int ExponentialMAOnBuffer(const int rates_total, const int prev_calculated, const int begin, const int period,
ValueStorage<double> &price, ValueStorage<double> &buffer) {
if (period <= 1 || period > (rates_total - begin)) return (0);
bool as_series_array = ArrayGetAsSeries(price);
bool as_series_buffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(price, false);
ArraySetAsSeries(buffer, false);
int start_position, i;
double smooth_factor = 2.0 / (1.0 + period);
if (prev_calculated == 0) {
// First calculation or number of bars was changed.
// Set empty value for first bars.
for (i = 0; i < begin; i++) buffer[i] = 0.0;
// Calculate first visible value.
start_position = period + begin;
buffer[begin] = price[begin];
for (i = begin + 1; i < start_position; i++)
buffer[i] = price[i] * smooth_factor + buffer[i - 1] * (1.0 - smooth_factor);
} else
start_position = prev_calculated - 1;
for (i = start_position; i < rates_total; i++)
buffer[i] = price[i] * smooth_factor + buffer[i - 1] * (1.0 - smooth_factor);
ArraySetAsSeries(price, as_series_array);
ArraySetAsSeries(buffer, as_series_buffer);
return (rates_total);
}
static int SimpleMAOnBuffer(const int rates_total, const int prev_calculated, const int begin, const int period,
ValueStorage<double> &price, ValueStorage<double> &buffer) {
int i;
// Check period.
if (period <= 1 || period > (rates_total - begin)) return (0);
// Save as_series flags.
bool as_series_price = ArrayGetAsSeries(price);
bool as_series_buffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(price, false);
ArraySetAsSeries(buffer, false);
// Calculate start position.
int start_position;
if (prev_calculated == 0) {
// First calculation or number of bars was changed.
// Set empty value for first bars.
start_position = period + begin;
for (i = 0; i < start_position - 1; i++) buffer[i] = 0.0;
// Calculate first visible value.
double first_value = 0;
for (i = begin; i < start_position; i++) first_value += price[i].Get();
buffer[start_position - 1] = first_value / period;
} else
start_position = prev_calculated - 1;
// Main loop.
for (i = start_position; i < rates_total; i++) buffer[i] = buffer[i - 1] + (price[i] - price[i - period]) / period;
// Restore as_series flags.
ArraySetAsSeries(price, as_series_price);
ArraySetAsSeries(buffer, as_series_buffer);
return (rates_total);
}
static int LinearWeightedMAOnBuffer(const int rates_total, const int prev_calculated, const int begin,
const int period, ValueStorage<double> &price, ValueStorage<double> &buffer) {
// Check period.
if (period <= 1 || period > (rates_total - begin)) return (0);
// Save as_series flags.
bool as_series_price = ArrayGetAsSeries(price);
bool as_series_buffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(price, false);
ArraySetAsSeries(buffer, false);
// Calculate start position.
int i, start_position;
if (prev_calculated <= period + begin + 2) {
// First calculation or number of bars was changed.
// Set empty value for first bars.
start_position = period + begin;
for (i = 0; i < start_position; i++) buffer[i] = 0.0;
} else
start_position = prev_calculated - 2;
// Calculate first visible value.
double sum = 0.0, lsum = 0.0;
int l, weight = 0;
for (i = start_position - period, l = 1; i < start_position; i++, l++) {
sum += price[i] * l;
lsum += price[i].Get();
weight += l;
}
buffer[start_position - 1] = sum / weight;
// Main loop.
for (i = start_position; i < rates_total; i++) {
sum = sum - lsum + price[i] * period;
lsum = lsum - price[i - period].Get() + price[i].Get();
buffer[i] = sum / weight;
}
// Restore as_series flags.
ArraySetAsSeries(price, as_series_price);
ArraySetAsSeries(buffer, as_series_buffer);
return (rates_total);
}
static int LinearWeightedMAOnBuffer(const int rates_total, const int prev_calculated, const int begin,
const int period, ValueStorage<double> &price, ValueStorage<double> &buffer,
int &weight_sum) {
int i, k;
// Check period.
if (period <= 1 || period > (rates_total - begin)) return (0);
// Save as_series flags.
bool as_series_price = ArrayGetAsSeries(price);
bool as_series_buffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(price, false);
ArraySetAsSeries(buffer, false);
// Calculate start position.
int start_position;
if (prev_calculated == 0) {
// First calculation or number of bars was changed.
// Set empty value for first bars.
start_position = period + begin;
for (i = 0; i < start_position; i++) buffer[i] = 0.0;
// Calculate first visible value.
double first_value = 0;
int wsum = 0;
for (i = begin, k = 1; i < start_position; i++, k++) {
first_value += k * price[i].Get();
wsum += k;
}
buffer[start_position - 1] = first_value / wsum;
weight_sum = wsum;
} else
start_position = prev_calculated - 1;
// Main loop.
for (i = start_position; i < rates_total; i++) {
double sum = 0;
for (int j = 0; j < period; j++) sum += (period - j) * price[i - j].Get();
buffer[i] = sum / weight_sum;
}
// Restore as_series flags.
ArraySetAsSeries(price, as_series_price);
ArraySetAsSeries(buffer, as_series_buffer);
return (rates_total);
}
static int SmoothedMAOnBuffer(const int rates_total, const int prev_calculated, const int begin, const int period,
ValueStorage<double> &price, ValueStorage<double> &buffer) {
int i;
// Check period.
if (period <= 1 || period > (rates_total - begin)) return (0);
// Save as_series flags.
bool as_series_price = ArrayGetAsSeries(price);
bool as_series_buffer = ArrayGetAsSeries(buffer);
ArraySetAsSeries(price, false);
ArraySetAsSeries(buffer, false);
// Calculate start position.
int start_position;
if (prev_calculated == 0) {
// First calculation or number of bars was changed.
// Set empty value for first bars.
start_position = period + begin;
for (i = 0; i < start_position - 1; i++) buffer[i] = 0.0;
// Calculate first visible value.
double first_value = 0;
for (i = begin; i < start_position; i++) first_value += price[i].Get();
buffer[start_position - 1] = first_value / period;
} else
start_position = prev_calculated - 1;
// Main loop.
for (i = start_position; i < rates_total; i++) buffer[i] = (buffer[i - 1] * (period - 1) + price[i].Get()) / period;
// Restore as_series flags.
ArraySetAsSeries(price, as_series_price);
ArraySetAsSeries(buffer, as_series_buffer);
return (rates_total);
}
/**
* Calculates Moving Average. The same as in "Example Moving Average" indicator.
*/
static int Calculate(const int rates_total, const int prev_calculated, const int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_method, int _ma_period) {
// Check for bars count.
if (rates_total < _ma_period - 1 + begin) {
// Not enough bars for calculation.
return (0);
}
if (prev_calculated == 0) {
// First calculation or number of bars was changed.
ArrayInitialize(ExtLineBuffer, (double)0);
}
// Calculation.
switch (_ma_method) {
case MODE_EMA:
CalculateEMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
break;
case MODE_LWMA:
CalculateLWMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
break;
case MODE_SMMA:
CalculateSmoothedMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
break;
case MODE_SMA:
CalculateSimpleMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
break;
}
// Return value of prev_calculated for next call.
return (rates_total);
}
static double SimpleMA(const int position, const int period, const double &price[]) {
double result = 0.0;
for (int i = 0; i < period; i++) {
result += price[i];
}
result /= period;
return result;
}
static double SimpleMA(const int position, const int period, ValueStorage<double> &price) {
double result = 0.0;
for (int i = 0; i < period; i++) {
result += price[i].Get();
}
result /= period;
return result;
}
/**
* 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_MA::iMA(GetSymbol(), GetTf(), GetPeriod(), GetMAShift(), GetMAMethod(), GetAppliedPrice(),
_ishift, THIS_PTR);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.custom_indi_name, /* [ */ GetPeriod(),
GetMAShift(), GetMAMethod(), GetAppliedPrice() /* ] */, 0, _ishift);
break;
case IDATA_INDICATOR:
// Calculating MA value from specified indicator.
_value = Indi_MA::iMAOnIndicator(GetCache(), GetDataSource(),
Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_MODE)), GetSymbol(),
GetTf(), GetPeriod(), GetMAShift(), GetMAMethod(), _ishift);
break;
}
return _value;
}
/**
* Returns reusable indicator.
*/
static Indi_MA *GetCached(string _symbol, ENUM_TIMEFRAMES _tf, int _period, int _ma_shift, ENUM_MA_METHOD _ma_method,
ENUM_APPLIED_PRICE _ap) {
Indi_MA *_ptr;
string _key = Util::MakeKey(_symbol, (int)_tf, _period, _ma_shift, (int)_ma_method, (int)_ap);
if (!Objects<Indi_MA>::TryGet(_key, _ptr)) {
IndiMAParams _p(_period, _ma_shift, _ma_method, _ap);
_ptr = Objects<Indi_MA>::Set(_key, new Indi_MA(_p));
_ptr.SetSymbol(_symbol);
}
return _ptr;
}
/**
* Returns value storage of given kind.
*/
IValueStorage *GetSpecificValueStorage(ENUM_INDI_VS_TYPE _type) override {
switch (_type) {
case INDI_VS_TYPE_PRICE_ASK:
case INDI_VS_TYPE_PRICE_BID:
// We're returning the same buffer for ask and bid price, as target indicator probably won't bother.
return GetValueStorage(0);
default:
// Trying in parent class.
return Indicator<IndiMAParams>::GetSpecificValueStorage(_type);
}
}
/**
* Checks whether indicator support given value storage type.
*/
bool HasSpecificValueStorage(ENUM_INDI_VS_TYPE _type) override {
switch (_type) {
case INDI_VS_TYPE_PRICE_ASK:
case INDI_VS_TYPE_PRICE_BID:
return true;
default:
// Trying in parent class.
return Indicator<IndiMAParams>::HasSpecificValueStorage(_type);
}
}
/* Getters */
/**
* Get period value.
*
* Averaging period for the calculation of the moving average.
*/
unsigned int GetPeriod() { return iparams.period; }
/**
* Get MA shift value.
*
* Indicators line offset relate to the chart by timeframe.
*/
unsigned int GetMAShift() { return iparams.ma_shift; }
/**
* Set MA method (smoothing type).
*/
ENUM_MA_METHOD GetMAMethod() { return iparams.ma_method; }
/**
* Get applied price value.
*
* The desired price base for calculations.
*/
ENUM_APPLIED_PRICE GetAppliedPrice() { return iparams.applied_array; }
/* Setters */
/**
* Set period value.
*
* Averaging period for the calculation of the moving average.
*/
void SetPeriod(unsigned int _period) {
istate.is_changed = true;
iparams.period = _period;
}
/**
* Set MA shift value.
*/
void SetMAShift(int _ma_shift) {
istate.is_changed = true;
iparams.ma_shift = _ma_shift;
}
/**
* Set MA method.
*
* Indicators line offset relate to the chart by timeframe.
*/
void SetMAMethod(ENUM_MA_METHOD _ma_method) {
istate.is_changed = true;
iparams.ma_method = _ma_method;
}
/**
* Set applied price value.
*
* The desired price base for calculations.
* @docs
* - https://docs.mql4.com/constants/indicatorconstants/prices#enum_applied_price_enum
* - https://www.mql5.com/en/docs/constants/indicatorconstants/prices#enum_applied_price_enum
*/
void SetAppliedPrice(ENUM_APPLIED_PRICE _applied_array) {
istate.is_changed = true;
iparams.applied_array = _applied_array;
}
};
#endif // INDI_MA_MQH