forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndi_RVI.mqh
153 lines (138 loc) · 4.83 KB
/
Indi_RVI.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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments 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/>.
*
*/
// Includes.
#include "../Indicator.mqh"
// Structs.
struct RVIParams : IndicatorParams {
unsigned int period;
// Struct constructor.
void RVIParams(unsigned int _period) : period(_period) {
itype = INDI_RVI;
max_modes = FINAL_SIGNAL_LINE_ENTRY;
SetDataValueType(TYPE_DOUBLE);
};
};
/**
* Implements the Relative Vigor Index indicator.
*/
class Indi_RVI : public Indicator {
protected:
RVIParams params;
public:
/**
* Class constructor.
*/
Indi_RVI(const RVIParams &_p) : params(_p.period), Indicator((IndicatorParams)_p) { params = _p; }
Indi_RVI(const RVIParams &_p, ENUM_TIMEFRAMES _tf) : params(_p.period), Indicator(INDI_RVI, _tf) { params = _p; }
/**
* Returns the indicator value.
*
* @docs
* - https://docs.mql4.com/indicators/irvi
* - https://www.mql5.com/en/docs/indicators/irvi
*/
static double iRVI(
string _symbol = NULL, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, unsigned int _period = 10,
ENUM_SIGNAL_LINE _mode = LINE_MAIN, // (MT4/MT5): 0 - MODE_MAIN/MAIN_LINE, 1 - MODE_SIGNAL/SIGNAL_LINE
int _shift = 0, Indicator *_obj = NULL) {
#ifdef __MQL4__
return ::iRVI(_symbol, _tf, _period, _mode, _shift);
#else // __MQL5__
int _handle = Object::IsValid(_obj) ? _obj.GetState().GetHandle() : NULL;
double _res[];
if (_handle == NULL || _handle == INVALID_HANDLE) {
if ((_handle = ::iRVI(_symbol, _tf, _period)) == INVALID_HANDLE) {
SetUserError(ERR_USER_INVALID_HANDLE);
return EMPTY_VALUE;
} else if (Object::IsValid(_obj)) {
_obj.SetHandle(_handle);
}
}
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, _mode, _shift, 1, _res) < 0) {
return EMPTY_VALUE;
}
return _res[0];
#endif
}
/**
* Returns the indicator's value.
*/
double GetValue(ENUM_SIGNAL_LINE _mode = LINE_MAIN, int _shift = 0) {
ResetLastError();
istate.handle = istate.is_changed ? INVALID_HANDLE : istate.handle;
double _value = Indi_RVI::iRVI(GetSymbol(), GetTf(), GetPeriod(), _mode, _shift, GetPointer(this));
istate.is_ready = _LastError == ERR_NO_ERROR;
istate.is_changed = false;
return _value;
}
/**
* Returns the indicator's struct value.
*/
IndicatorDataEntry GetEntry(int _shift = 0) {
long _bar_time = GetBarTime(_shift);
unsigned int _position;
IndicatorDataEntry _entry;
if (idata.KeyExists(_bar_time, _position)) {
_entry = idata.GetByPos(_position);
} else {
_entry.timestamp = GetBarTime(_shift);
_entry.value.SetValue(params.idvtype, GetValue(LINE_MAIN, _shift), LINE_MAIN);
_entry.value.SetValue(params.idvtype, GetValue(LINE_SIGNAL, _shift), LINE_SIGNAL);
_entry.SetFlag(INDI_ENTRY_FLAG_IS_VALID, !_entry.value.HasValue(params.idvtype, (double)NULL) &&
!_entry.value.HasValue(params.idvtype, EMPTY_VALUE));
if (_entry.IsValid()) idata.Add(_entry, _bar_time);
}
return _entry;
}
/**
* Returns the indicator's entry value.
*/
MqlParam GetEntryValue(int _shift = 0, int _mode = 0) {
MqlParam _param = {TYPE_DOUBLE};
_param.double_value = GetEntry(_shift).value.GetValueDbl(params.idvtype, _mode);
return _param;
}
/* Getters */
/**
* Get period value.
*/
unsigned int GetPeriod() { return params.period; }
/* Setters */
/**
* Set the averaging period for the RVI calculation.
*/
void SetPeriod(unsigned int _period) {
istate.is_changed = true;
params.period = _period;
}
/* Printer methods */
/**
* Returns the indicator's value in plain format.
*/
string ToString(int _shift = 0) { return GetEntry(_shift).value.ToString(params.idvtype); }
};