-
Notifications
You must be signed in to change notification settings - Fork 2
/
QQE.mq4
321 lines (295 loc) · 13 KB
/
QQE.mq4
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
//+------------------------------------------------------------------+
//| QQE.mq5 |
//| Copyright © 2010-2022, EarnForex |
//| https://www.earnforex.com |
//| Based on version by Tim Hyder (2008) |
//| Based on version by Roman Ignatov (2006) |
//+------------------------------------------------------------------+
#property copyright "www.EarnForex.com, 2010-2022"
#property link "https://www.earnforex.com/metatrader-indicators/QQE/"
#property version "1.03"
#property strict
#property description "QQE - Qualitative Quantitative Estimation."
#property description "Calculated as two indicators:"
#property description "1) MA on RSI"
#property description "2) Difference of MA on RSI and MA of MA of ATR of MA of RSI"
#property description "The signal for buy is when blue line crosses level 50 from below after crossing the yellow line from below."
#property description "The signal for sell is when blue line crosses level 50 from above after crossing the yellow line from above."
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_label1 "RSI MA"
#property indicator_color2 clrYellow
#property indicator_style2 STYLE_DOT
#property indicator_label2 "Smoothed"
#property indicator_type3 DRAW_NONE
#property indicator_type4 DRAW_NONE
#property indicator_type5 DRAW_NONE
#property indicator_level1 50
#property indicator_levelcolor clrAqua
#property indicator_levelstyle STYLE_DOT
// Inputs
input int SF = 5; // Smoothing Factor
input bool AlertOnCrossover = false;
input bool AlertOnLevel = false;
input int AlertLevel = 50;
input bool ArrowsOnCrossover = true;
input color CrossoverUpArrow = clrGreen;
input color CrossoverDnArrow = clrRed;
input bool ArrowsOnLevel = true;
input color LevelUpArrow = clrGreen;
input color LevelDnArrow = clrRed;
input bool NativeAlerts = false;
input bool EmailAlerts = false;
input bool NotificationAlerts = false;
input ENUM_TIMEFRAMES UpperTimeframe = PERIOD_CURRENT;
input string ObjectPrefix = "QQE-";
// Global variables:
int RSI_Period = 14;
int Wilders_Period;
int StartBar;
datetime LastAlertTimeCross, LastAlertTimeLevel;
// For MTF support:
string IndicatorFileName;
// Buffers:
double TrLevelSlow[];
double AtrRsi[];
double MaAtrRsi[];
double Rsi[];
double RsiMa[];
void OnInit()
{
LastAlertTimeCross = 0;
LastAlertTimeLevel = 0;
Wilders_Period = RSI_Period * 2 - 1;
StartBar = MathMax(SF, Wilders_Period);
SetIndexBuffer(0, RsiMa);
SetIndexDrawBegin(0, StartBar);
SetIndexBuffer(1, TrLevelSlow);
SetIndexDrawBegin(1, StartBar);
SetIndexBuffer(2, AtrRsi);
SetIndexBuffer(3, MaAtrRsi);
SetIndexBuffer(4, Rsi);
IndicatorShortName(StringConcatenate("QQE(", SF, ")"));
IndicatorSetInteger(INDICATOR_DIGITS, 2);
if (PeriodSeconds(UpperTimeframe) < PeriodSeconds())
{
Print("UpperTimeframe should be above the current timeframe.");
IndicatorFileName = "";
}
else if (PeriodSeconds(UpperTimeframe) > PeriodSeconds()) IndicatorFileName = WindowExpertName();
else IndicatorFileName = "";
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(ChartID(), ObjectPrefix);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if (Bars <= StartBar) return 0;
int counted = IndicatorCounted();
if (counted < 1)
{
for (int i = Bars - StartBar; i < Bars; i++)
{
TrLevelSlow[i] = 0.0;
AtrRsi[i] = 0.0;
MaAtrRsi[i] = 0.0;
Rsi[i] = 0.0;
RsiMa[i] = 0.0;
}
}
if ((counted > 0) && (IndicatorFileName != "")) counted -= PeriodSeconds(UpperTimeframe) / PeriodSeconds(); // Make the indicator redraw all current bars that constitute the upper timeframe bar.
else counted = Bars - counted - 1;
if (counted > Bars - StartBar - 1) counted = Bars - StartBar - 1;
if (IndicatorFileName == "")
{
for (int i = counted; i >= 0; i--)
{
Rsi[i] = iRSI(Symbol(), Period(), RSI_Period, PRICE_CLOSE, i);
}
}
for (int i = counted; i >= 0; i--)
{
if (IndicatorFileName == "") RsiMa[i] = iMAOnArray(Rsi, 0, SF, 0, MODE_EMA, i);
else
{
int shift = iBarShift(Symbol(), UpperTimeframe, Time[i]); // Get the upper timeframe shift based on the current timeframe bar's time.
RsiMa[i] = iCustom(Symbol(), UpperTimeframe, IndicatorFileName, SF, false, false, AlertLevel, false, LevelUpArrow, LevelDnArrow, false, CrossoverUpArrow, CrossoverDnArrow, false, false, false, UpperTimeframe, ObjectPrefix, 0, shift);
}
if (IndicatorFileName == "") AtrRsi[i] = MathAbs(RsiMa[i + 1] - RsiMa[i]);
}
if (IndicatorFileName == "")
{
for (int i = counted; i >= 0; i--)
{
MaAtrRsi[i] = iMAOnArray(AtrRsi, 0, Wilders_Period, 0, MODE_EMA, i);
}
}
int i = counted + 1;
double tr = 0, rsi1 = 0;
if (IndicatorFileName == "")
{
tr = TrLevelSlow[i];
rsi1 = iMAOnArray(Rsi, 0, SF, 0, MODE_EMA, i);
}
while (i > 0)
{
i--;
if (IndicatorFileName == "")
{
double rsi0 = iMAOnArray(Rsi, 0, SF, 0, MODE_EMA, i);
double dar = iMAOnArray(MaAtrRsi, 0, Wilders_Period, 0, MODE_EMA, i) * 4.236;
double dv = tr;
if (rsi0 < tr)
{
tr = rsi0 + dar;
if ((rsi1 < dv) && (tr > dv)) tr = dv;
}
else if (rsi0 > tr)
{
tr = rsi0 - dar;
if ((rsi1 > dv) && (tr < dv)) tr = dv;
}
rsi1 = rsi0;
TrLevelSlow[i] = tr;
}
else
{
int shift = iBarShift(Symbol(), UpperTimeframe, Time[i]); // Get the upper timeframe shift based on the current timeframe bar's time.
TrLevelSlow[i] = iCustom(Symbol(), UpperTimeframe, IndicatorFileName, SF, false, false, AlertLevel, false, LevelUpArrow, LevelDnArrow, false, CrossoverUpArrow, CrossoverDnArrow, false, false, false, UpperTimeframe, ObjectPrefix, 1, shift);
}
// Arrows:
if ((i > 0) || (IndicatorFileName != "")) // In MTF mode, check as soon as possible.
{
// Prepare for multi-timeframe mode.
int cur_i = i;
int pre_i = i + 1;
// Actual MTF (to avoid non-existing signals):
if ((IndicatorFileName != "") && (i < PeriodSeconds(UpperTimeframe) / PeriodSeconds())) // Can safely skip this step if processing old bars.
{
// Find the bar that corresponds to the upper timeframe's latest finished bar.
int customIndex = iBarShift(Symbol(), UpperTimeframe, Time[cur_i]);
cur_i++;
while (iBarShift(Symbol(), UpperTimeframe, Time[cur_i]) == customIndex)
{
cur_i++;
}
// Find the bar that corresponds to the upper timeframe's pre-latest finished bar.
customIndex = iBarShift(Symbol(), UpperTimeframe, Time[cur_i]);
pre_i = cur_i + 1;
while (iBarShift(Symbol(), UpperTimeframe, Time[pre_i]) == customIndex)
{
pre_i++;
}
cur_i = pre_i - 1; // Use oldest lower timeframe bar inside that upper timeframe bar.
}
if (ArrowsOnCrossover)
{
string name = ObjectPrefix + "CArrow" + TimeToString(Time[cur_i]);
if ((RsiMa[pre_i] < TrLevelSlow[pre_i]) && (RsiMa[cur_i] > TrLevelSlow[cur_i]))
{
ObjectCreate(ChartID(), name, OBJ_ARROW_THUMB_UP, 0, Time[cur_i], Low[cur_i] - 1 * _Point);
ObjectSetInteger(ChartID(), name, OBJPROP_COLOR, LevelUpArrow);
ObjectSetInteger(ChartID(), name, OBJPROP_ANCHOR, ANCHOR_TOP);
ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, 5);
}
else if ((RsiMa[pre_i] > TrLevelSlow[pre_i]) && (RsiMa[cur_i] < TrLevelSlow[cur_i]))
{
ObjectCreate(ChartID(), name, OBJ_ARROW_THUMB_DOWN, 0, Time[cur_i], High[cur_i] + 1 * _Point);
ObjectSetInteger(ChartID(), name, OBJPROP_COLOR, LevelDnArrow);
ObjectSetInteger(ChartID(), name, OBJPROP_ANCHOR, ANCHOR_BOTTOM);
ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, 5);
}
}
if (ArrowsOnLevel)
{
string name = ObjectPrefix + "LArrow" + TimeToString(Time[cur_i]);
if ((RsiMa[pre_i] < AlertLevel) && (RsiMa[cur_i] > AlertLevel))
{
ObjectCreate(ChartID(), name, OBJ_ARROW_UP, 0, Time[cur_i], Low[cur_i] - 1 * _Point);
ObjectSetInteger(ChartID(), name, OBJPROP_COLOR, LevelUpArrow);
ObjectSetInteger(ChartID(), name, OBJPROP_ANCHOR, ANCHOR_TOP);
ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, 5);
}
else if ((RsiMa[pre_i] > AlertLevel) && (RsiMa[cur_i] < AlertLevel))
{
ObjectCreate(ChartID(), name, OBJ_ARROW_DOWN, 0, Time[cur_i], High[cur_i] + 1 * _Point);
ObjectSetInteger(ChartID(), name, OBJPROP_COLOR, LevelDnArrow);
ObjectSetInteger(ChartID(), name, OBJPROP_ANCHOR, ANCHOR_BOTTOM);
ObjectSetInteger(ChartID(), name, OBJPROP_WIDTH, 5);
}
}
}
}
if ((!NativeAlerts) && (!EmailAlerts) && (!NotificationAlerts)) return rates_total;
if ((!AlertOnCrossover) && (!AlertOnLevel)) return rates_total;
// Prepare for multi-timeframe mode.
int pre_i = 2;
if (IndicatorFileName != "")
{
// Find the bar that corresponds to the upper timeframe's latest finished bar.
int cnt = 1;
int customIndex = iBarShift(Symbol(), UpperTimeframe, Time[0]);
while (iBarShift(Symbol(), UpperTimeframe, Time[cnt]) == customIndex)
{
cnt++;
}
i = cnt;
// Find the bar that corresponds to the upper timeframe's pre-latest finished bar.
customIndex = iBarShift(Symbol(), UpperTimeframe, Time[cnt]);
cnt++;
while (iBarShift(Symbol(), UpperTimeframe, Time[cnt]) == customIndex)
{
cnt++;
}
pre_i = cnt;
}
else i = 1; // Non-MTF.
if (AlertOnLevel)
{
if ((LastAlertTimeLevel > 0) && (((RsiMa[pre_i] < AlertLevel) && (RsiMa[i] > AlertLevel)) || ((RsiMa[pre_i] > AlertLevel) && (RsiMa[i] < AlertLevel))) && (Time[i - 1] > LastAlertTimeLevel))
{
string base = "QQE " + Symbol() + ", TF: " + TimeframeToString((ENUM_TIMEFRAMES)Period());
string text = base + ", " + IntegerToString(AlertLevel) + " level Cross Up";
if ((RsiMa[pre_i] > AlertLevel) && (RsiMa[i] < AlertLevel)) text = base + " " + IntegerToString(AlertLevel) + " level Cross Down";
DoAlerts(text);
LastAlertTimeLevel = Time[i - 1];
}
}
if (AlertOnCrossover)
{
if ((LastAlertTimeCross > 0) && (((RsiMa[pre_i] < TrLevelSlow[pre_i]) && (RsiMa[i] > TrLevelSlow[i])) || ((RsiMa[pre_i] > TrLevelSlow[pre_i]) && (RsiMa[i] < TrLevelSlow[i]))) && (Time[i - 1] > LastAlertTimeCross))
{
string base = "QQE " + Symbol() + ", TF: " + TimeframeToString((ENUM_TIMEFRAMES)Period());
string text = base + ", RSI MA crossed Smoothed Line from below.";
if ((RsiMa[pre_i] > TrLevelSlow[pre_i]) && (RsiMa[i] < TrLevelSlow[i])) text = base + ", RSI MA crossed Smoothed Line from above.";
DoAlerts(text);
LastAlertTimeCross = Time[i - 1];
}
}
if (LastAlertTimeLevel == 0) LastAlertTimeLevel = Time[0];
if (LastAlertTimeCross == 0) LastAlertTimeCross = Time[0];
return rates_total;
}
void DoAlerts(const string msgText)
{
if (NativeAlerts) Alert(msgText);
if (EmailAlerts) SendMail(msgText, msgText);
if (NotificationAlerts) SendNotification(msgText);
}
string TimeframeToString(ENUM_TIMEFRAMES P)
{
return StringSubstr(EnumToString(P), 7);
}
//+------------------------------------------------------------------+