-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIndi_Momentum.mqh
212 lines (190 loc) · 7.21 KB
/
Indi_Momentum.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
//+------------------------------------------------------------------+
//| 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
* Momentum oscillator.
*
* It helps identify the strength behind price movement.
* We can also use to identify when a market is likely to continue in the direction of the main trend.
* In addition, it can help to identify when the price action is losing steam to prepare for a potential trend reversal.
*/
// Includes.
#include "../Indicator.mqh"
#include "Indi_PriceFeeder.mqh"
#ifndef __MQL4__
// Defines global functions (for MQL4 backward compability).
double iMomentum(string _symbol, int _tf, int _period, int _ap, int _shift) {
ResetLastError();
return Indi_Momentum::iMomentum(_symbol, (ENUM_TIMEFRAMES)_tf, _period, (ENUM_APPLIED_PRICE)_ap, _shift);
}
#endif
// Structs.
struct IndiMomentumParams : IndicatorParams {
unsigned int period;
ENUM_APPLIED_PRICE applied_price;
// Struct constructors.
IndiMomentumParams(unsigned int _period = 12, ENUM_APPLIED_PRICE _ap = PRICE_OPEN, int _shift = 0)
: period(_period), applied_price(_ap), IndicatorParams(INDI_MOMENTUM, 1, TYPE_DOUBLE) {
shift = _shift;
SetDataValueRange(IDATA_RANGE_MIXED);
SetCustomIndicatorName("Examples\\Momentum");
};
IndiMomentumParams(IndiMomentumParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
/**
* Implements the Momentum indicator.
*/
class Indi_Momentum : public Indicator<IndiMomentumParams> {
public:
/**
* Class constructor.
*/
Indi_Momentum(IndiMomentumParams &_p, IndicatorBase *_indi_src = NULL)
: Indicator<IndiMomentumParams>(_p, _indi_src) {}
Indi_Momentum(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0) : Indicator(INDI_MOMENTUM, _tf, _shift) {}
/**
* Returns the indicator value.
*
* @docs
* - https://docs.mql4.com/indicators/imomentum
* - https://www.mql5.com/en/docs/indicators/imomentum
*/
static double iMomentum(string _symbol, ENUM_TIMEFRAMES _tf, unsigned int _period, ENUM_APPLIED_PRICE _ap,
int _shift = 0, IndicatorBase *_obj = NULL) {
#ifdef __MQL4__
return ::iMomentum(_symbol, _tf, _period, _ap, _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 = ::iMomentum(_symbol, _tf, _period, _ap)) == 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
}
static double iMomentumOnIndicator(IndicatorBase *_indi, string _symbol, ENUM_TIMEFRAMES _tf, unsigned int _period,
int _mode, int _shift = 0) {
double _indi_value_buffer[];
IndicatorDataEntry _entry(_indi.GetModeCount());
_period += 1;
ArrayResize(_indi_value_buffer, _period);
for (int i = 0; i < (int)_period; i++) {
// Getting value from single, selected buffer.
_indi_value_buffer[i] = _indi[i].GetValue<double>(_mode);
}
double momentum = (_indi_value_buffer[0] / _indi_value_buffer[_period - 1]) * 100;
return momentum;
}
static double iMomentumOnArray(double &array[], int total, int period, int shift) {
#ifdef __MQL4__
return ::iMomentumOnArray(array, total, period, shift);
#else
Indi_PriceFeeder indi_price_feeder(array);
return iMomentumOnIndicator(&indi_price_feeder, NULL, NULL, period, /*unused*/ PRICE_OPEN, shift);
#endif
}
/**
* Returns the indicator's value.
*/
virtual IndicatorDataEntryValue GetEntryValue(int _mode = 0, int _shift = -1) {
double _value = EMPTY_VALUE;
int _ishift = _shift >= 0 ? _shift : iparams.GetShift();
switch (iparams.idstype) {
case IDATA_BUILTIN:
istate.handle = istate.is_changed ? INVALID_HANDLE : istate.handle;
// @fixit Somehow shift isn't used neither in MT4 nor MT5.
_value = Indi_Momentum::iMomentum(_Symbol, GetTf(), GetPeriod(), GetAppliedPrice(), iparams.shift + _ishift,
THIS_PTR);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, _Symbol, GetTf(), iparams.GetCustomIndicatorName(), /*[*/ GetPeriod() /*]*/, 0,
_ishift);
break;
case IDATA_INDICATOR:
ValidateSelectedDataSource();
// @fixit Somehow shift isn't used neither in MT4 nor MT5.
_value = Indi_Momentum::iMomentumOnIndicator(GetDataSource(), _Symbol, GetTf(), GetPeriod(),
GetDataSourceMode(), iparams.shift + _shift);
if (iparams.is_draw) {
draw.DrawLineTo(StringFormat("%s", GetName()), GetBarTime(iparams.shift + _shift), _value, 1);
}
break;
}
return _value;
}
/* Getters */
/**
* Get period value.
*
* Averaging period (bars count) for the calculation of the price change.
*/
unsigned int GetPeriod() { return iparams.period; }
/**
* Get applied price value.
*
* The desired price base for calculations.
*/
ENUM_APPLIED_PRICE GetAppliedPrice() { return iparams.applied_price; }
/* Setters */
/**
* Set period value.
*
* Averaging period (bars count) for the calculation of the price change.
*/
void SetPeriod(unsigned int _period) {
istate.is_changed = true;
iparams.period = _period;
}
/**
* 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 _ap) {
istate.is_changed = true;
iparams.applied_price = _ap;
}
};