-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQQE.mq5
388 lines (347 loc) · 15.1 KB
/
QQE.mq5
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
//+------------------------------------------------------------------+
//| 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 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 6
#property indicator_plots 2
#property indicator_width1 2
#property indicator_color1 clrDodgerBlue
#property indicator_type1 DRAW_LINE
#property indicator_label1 "RSI MA"
#property indicator_color2 clrYellow
#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_DOT
#property indicator_label2 "Smoothed"
#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:
int QQE_handle;
// Buffers:
double TrLevelSlow[];
double AtrRsi[];
double MaAtrRsi[];
double Rsi[];
double RsiMa[];
double MaMaAtrRsi[];
// Indicator handles
int myRSI;
void OnInit()
{
LastAlertTimeCross = 0;
LastAlertTimeLevel = 0;
Wilders_Period = RSI_Period * 2 - 1;
StartBar = MathMax(SF, Wilders_Period);
SetIndexBuffer(0, RsiMa, INDICATOR_DATA);
SetIndexBuffer(1, TrLevelSlow, INDICATOR_DATA);
SetIndexBuffer(2, AtrRsi, INDICATOR_CALCULATIONS);
SetIndexBuffer(3, MaAtrRsi, INDICATOR_CALCULATIONS);
SetIndexBuffer(4, Rsi, INDICATOR_CALCULATIONS);
SetIndexBuffer(5, MaMaAtrRsi, INDICATOR_CALCULATIONS);
ArraySetAsSeries(RsiMa, true);
ArraySetAsSeries(TrLevelSlow, true);
ArraySetAsSeries(AtrRsi, true);
ArraySetAsSeries(MaAtrRsi, true);
ArraySetAsSeries(Rsi, true);
ArraySetAsSeries(MaMaAtrRsi, true);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, StartBar);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, StartBar);
IndicatorSetInteger(INDICATOR_DIGITS, 2);
myRSI = iRSI(Symbol(), Period(), RSI_Period, PRICE_CLOSE);
if (PeriodSeconds(UpperTimeframe) > PeriodSeconds())
{
string IndicatorFileName = MQLInfoString(MQL_PROGRAM_NAME);
QQE_handle = iCustom(Symbol(), UpperTimeframe, IndicatorFileName, SF, false, false, AlertLevel, false, LevelUpArrow, LevelDnArrow, false, CrossoverUpArrow, CrossoverDnArrow, false, false, false, UpperTimeframe, ObjectPrefix);
}
else
{
QQE_handle = INVALID_HANDLE;
if (PeriodSeconds(UpperTimeframe) < PeriodSeconds())
{
Print("UpperTimeframe should be above the current timeframe.");
}
}
IndicatorSetString(INDICATOR_SHORTNAME, "QQE(" + IntegerToString(SF) + ")");
}
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[])
{
ArraySetAsSeries(Time, true);
ArraySetAsSeries(High, true);
ArraySetAsSeries(Low, true);
if (rates_total <= StartBar) return 0;
int counted = prev_calculated - 1;
if (counted < 1)
{
for (int i = rates_total - StartBar; i < rates_total; i++)
{
TrLevelSlow[i] = 0.0;
AtrRsi[i] = 0.0;
MaAtrRsi[i] = 0.0;
Rsi[i] = 0.0;
RsiMa[i] = 0.0;
MaMaAtrRsi[i] = 0.0;
}
}
bool rec_only_latest_upper_bar = false; // Recalculate only the latest upper timeframe bar.
if ((counted > 0) && (QQE_handle != INVALID_HANDLE))
{
counted = prev_calculated - PeriodSeconds(UpperTimeframe) / PeriodSeconds(); // Make the indicator redraw all current bars that constitute the upper timeframe bar.
rec_only_latest_upper_bar = true;
}
else
{
counted = rates_total - counted - 1;
if (counted > rates_total - StartBar - 1) counted = rates_total - StartBar - 1;
}
if (QQE_handle == INVALID_HANDLE)
{
if (CopyBuffer(myRSI, 0, 0, counted + 2, Rsi) != counted + 2) return 0;
// Fills "counted" cells of RsiMA with EMA of Rsi.
CalculateEMA(counted + 1, SF, Rsi, RsiMa);
for (int i = counted; i >= 0; i--)
{
AtrRsi[i] = MathAbs(RsiMa[i + 1] - RsiMa[i]);
}
}
else
{
for (int i = counted; i >= 0; i--)
{
double buf[1];
if (rec_only_latest_upper_bar)
if (Time[i] < iTime(Symbol(), UpperTimeframe, 0)) continue; // Skip bars older than the upper current bar.
int n = CopyBuffer(QQE_handle, 0, Time[i], 1, buf);
if (n == 1) RsiMa[i] = buf[0];
else return prev_calculated;
}
}
int i = counted + 1;
double tr = 0, rsi1 = 0;
if (QQE_handle == INVALID_HANDLE)
{
// Fills "counted" cells of MaAtrRsi with EMA of AtrRsi.
CalculateEMA(counted, Wilders_Period, AtrRsi, MaAtrRsi);
tr = TrLevelSlow[i];
rsi1 = RsiMa[i];
CalculateEMA(counted, Wilders_Period, MaAtrRsi, MaMaAtrRsi);
}
while (i > 0)
{
i--;
if (QQE_handle == INVALID_HANDLE)
{
double rsi0 = RsiMa[i];
double dar = MaMaAtrRsi[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;
}
TrLevelSlow[i] = tr;
rsi1 = rsi0;
}
else
{
double buf[1];
if (rec_only_latest_upper_bar)
if (Time[i] < iTime(Symbol(), UpperTimeframe, 0)) continue; // Skip bars older than the upper current bar.
int n = CopyBuffer(QQE_handle, 1, Time[i], 1, buf);
if (n == 1) TrLevelSlow[i] = buf[0];
else return prev_calculated;
}
// Arrows:
if ((i > 0) || (QQE_handle != INVALID_HANDLE)) // 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 ((QQE_handle != INVALID_HANDLE) && (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 ((cur_i < rates_total) && (iBarShift(Symbol(), UpperTimeframe, Time[cur_i]) == customIndex))
{
cur_i++;
}
if (cur_i == rates_total) return prev_calculated;
// 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 ((pre_i < rates_total) && (iBarShift(Symbol(), UpperTimeframe, Time[pre_i])) == customIndex)
{
pre_i++;
}
if (pre_i == rates_total) return prev_calculated;
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 (QQE_handle != INVALID_HANDLE)
{
// Find the bar that corresponds to the upper timeframe's latest finished bar.
int cnt = 1;
int customIndex = iBarShift(Symbol(), UpperTimeframe, Time[0]);
while ((cnt < rates_total) && (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 ((cnt < rates_total) && (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 TextNative = IntegerToString(AlertLevel) + " level Cross ";
if ((RsiMa[pre_i] > AlertLevel) && (RsiMa[i] < AlertLevel)) TextNative += "Down";
else TextNative += "Up";
string Text = "QQE " + Symbol() + ", TF: " + TimeframeToString((ENUM_TIMEFRAMES)Period()) + " " + TextNative;
DoAlerts(Text, TextNative);
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 TextNative = "RSI MA crossed Smoothed Line from ";
if ((RsiMa[pre_i] > TrLevelSlow[pre_i]) && (RsiMa[i] < TrLevelSlow[i])) TextNative += "above.";
else TextNative += "below.";
string Text = "QQE " + Symbol() + ", TF: " + TimeframeToString((ENUM_TIMEFRAMES)Period()) + " " + TextNative;
DoAlerts(Text, TextNative);
LastAlertTimeCross = Time[i - 1];
}
}
if (LastAlertTimeLevel == 0) LastAlertTimeLevel = Time[0];
if (LastAlertTimeCross == 0) LastAlertTimeCross = Time[0];
return rates_total;
}
void DoAlerts(string Text, string TextNative)
{
if (NativeAlerts) Alert(TextNative);
if (EmailAlerts) SendMail(Text, Text);
if (NotificationAlerts) SendNotification(Text);
}
string TimeframeToString(ENUM_TIMEFRAMES P)
{
return StringSubstr(EnumToString(P), 7);
}
//+------------------------------------------------------------------+
//| Exponential Moving Average |
//| Fills the buffer array with EMA values. |
//+------------------------------------------------------------------+
void CalculateEMA(int begin, int period, const double &price[], double &result[])
{
double SmoothFactor = 2.0 / (1.0 + period);
for (int i = begin; i >= 0; i--)
{
if (price[i] == EMPTY_VALUE) result[i] = 0;
else result[i] = price[i] * SmoothFactor + result[i + 1] * (1.0 - SmoothFactor);
}
}
//+------------------------------------------------------------------+