-
Notifications
You must be signed in to change notification settings - Fork 7
/
Trade Assistant.mq4
367 lines (331 loc) · 15.5 KB
/
Trade Assistant.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
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
//+------------------------------------------------------------------+
//| Trade_Assistant.mq4 |
//| Copyright © 2010-2022, EarnForex |
//| https://www.earnforex.com |
//| Based on indicator by Tom Balfe (2008) |
//+------------------------------------------------------------------+
#property copyright "www.EarnForex.com, 2010-2022"
#property link "https://www.earnforex.com/metatrader-indicators/Trade-Assistant/"
#property version "1.02"
#property strict
#property description "Trade Assistant - an indicator that shows simple trade recommendations based on the Stochastic, RSI, and CCI readings."
#property description "Alerts on indicator confluence."
#property description "Supported timeframes: M1, M5, M15, M30, H1, H4, D1, W1, MN1."
#property indicator_separate_window
#property indicator_plots 0
enum enum_candle_to_check
{
Current,
Previous
};
// Input parameters
input enum_candle_to_check CheckCandle = Previous; // CheckCandle: Affects both indications and alerts.
input string Stochastic_Settings = "=== Stochastic Settings ===";
input int PercentK = 8;
input int PercentD = 3;
input int Slowing = 3;
input string RSI_Settings = "=== RSI Settings ===";
input int RSIP1 = 14;
input int RSIP2 = 70;
input string Timeframe_Settings = "=== Timeframe Settings ===";
input bool Enable_M1 = false; // Enable M1
input bool Enable_M5 = true; // Enable M5
input bool Enable_M15 = true; // Enable M15
input bool Enable_M30 = true; // Enable M30
input bool Enable_H1 = true; // Enable H1
input bool Enable_H4 = true; // Enable H4
input bool Enable_D1 = true; // Enable D1
input bool Enable_W1 = true; // Enable W1
input bool Enable_MN1 = false; // Enable MN1
input string My_Alerts = "=== Alerts ===";
input bool EnableNativeAlerts = false;
input bool EnableEmailAlerts = false;
input bool EnablePushAlerts = false;
input string My_Colors = "=== Colors ===";
input color TFColor = clrLightSteelBlue; // Timeframe Color
input color IndicatorColor = clrPaleGoldenrod; // Indicator Color
input color BuyColor = clrLime; // Buy Color
input color SellColor = clrRed; // Sell Color
input color NeutralColor = clrKhaki; // Neutral Color
input string My_Symbols = "=== Wingdings Symbols ===";
input uchar sBuy = 233;
input uchar sSell = 234;
input uchar sWait = 54;
input uchar sCCIAgainstBuy = 238;
input uchar sCCIAgainstSell = 236;
// Global variables
int IndicatorWindow = -1;
string ShortName = "Trade Assistant";
// For alerts
int Confluence[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int Confluence_prev[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
// Spacing
int scaleX = 100, scaleY = 20, offsetX = 90, offsetY = 20, fontSize = 8;
// Internal indicator parameters
ENUM_TIMEFRAMES TF[] = {PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1};
int eCCI[] = {14, 14, 14, 6, 6, 6, 6, 5, 4};
int tCCI[] = {100, 50, 34, 14, 14, 14, 14, 12, 10};
// Text labels
string signalNameStr[] = {"Stoch", "RSI", "Entry CCI", "Trend CCI"};
int OnInit()
{
IndicatorShortName(ShortName);
if ((!Enable_M1) && (!Enable_M5) && (!Enable_M15) && (!Enable_M30) && (!Enable_H1) && (!Enable_H4) && (!Enable_D1) && (!Enable_W1) && (!Enable_MN1)) return INIT_FAILED;
return INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(ChartID(), IndicatorWindow, OBJ_LABEL);
}
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 (IndicatorWindow == -1) // Only once.
{
IndicatorWindow = WindowFind(ShortName);
// Labels and placeholders.
for (int x = 0, x_m = 0; x < 9; x++)
{
if (!TimeframeCheck(TF[x])) continue;
for (int y = 0; y < 4; y++)
{
string suffix = IntegerToString(x) + IntegerToString(y);
// Create timeframe text labels.
string name = "tPs" + suffix;
ObjectCreate(name, OBJ_LABEL, IndicatorWindow, 0, 0);
ObjectSetText(name, StringSubstr(EnumToString(TF[x]), 7), fontSize, "Arial Bold", TFColor);
ObjectSet(name, OBJPROP_CORNER, 0);
ObjectSet(name, OBJPROP_XDISTANCE, x_m * scaleX + offsetX);
ObjectSet(name, OBJPROP_YDISTANCE, y * scaleY + offsetY);
ObjectSet(name, OBJPROP_SELECTABLE, false);
// Create blanks for arrows.
name = "dI" + suffix;
ObjectCreate(name, OBJ_LABEL, IndicatorWindow, 0, 0);
ObjectSetText(name, " ", 10);
ObjectSet(name, OBJPROP_CORNER, 0);
ObjectSet(name, OBJPROP_XDISTANCE, x_m * scaleX + (offsetX + 60));
ObjectSet(name, OBJPROP_YDISTANCE, y * scaleY + offsetY);
ObjectSet(name, OBJPROP_SELECTABLE, false);
// Create blanks for text.
name = "tI" + suffix;
ObjectCreate(name, OBJ_LABEL, IndicatorWindow, 0, 0);
ObjectSetText(name, " ", 9);
ObjectSet(name, OBJPROP_CORNER, 0);
ObjectSet(name, OBJPROP_XDISTANCE, x_m * scaleX + (offsetX + 25));
ObjectSet(name, OBJPROP_YDISTANCE, y * scaleY + offsetY);
ObjectSet(name, OBJPROP_SELECTABLE, false);
}
x_m++; // Multiplier for object position. Increased only for enabled timeframes.
}
// Create indicator text labels.
for (int y = 0; y < 4; y++)
{
string name = "tInd" + IntegerToString(y);
ObjectCreate(name, OBJ_LABEL, IndicatorWindow, 0, 0);
ObjectSetText(name, signalNameStr[y], fontSize, "Arial Bold", IndicatorColor);
ObjectSet(name, OBJPROP_CORNER, 0);
ObjectSet(name, OBJPROP_XDISTANCE, offsetX - 80);
ObjectSet(name, OBJPROP_YDISTANCE, y * scaleY + offsetY);
ObjectSet(name, OBJPROP_SELECTABLE, false);
}
}
// Main indicator values.
for (int x = 0; x < 9; x++)
{
if (!TimeframeCheck(TF[x])) continue;
Confluence[x] = 0; // For alerts.
// Stochastic arrows and text.
string name_arrow = "dI" + IntegerToString(x) + "0";
string name_text = "tI" + IntegerToString(x) + "0";
if ((iStochastic(NULL, TF[x], PercentK, PercentD, Slowing, MODE_SMA, 0, MODE_MAIN, CheckCandle)) >
(iStochastic(NULL, TF[x], PercentK, PercentD, Slowing, MODE_SMA, 0, MODE_SIGNAL, CheckCandle)))
{
ObjectSetText(name_arrow, CharToString(sBuy), fontSize, "Wingdings", BuyColor);
ObjectSetText(name_text, " BUY", 9, "Arial Bold", BuyColor);
Confluence[x]++;
}
else if
((iStochastic(NULL, TF[x], PercentK, PercentD, Slowing, MODE_SMA, 0, MODE_SIGNAL, CheckCandle)) >
(iStochastic(NULL, TF[x], PercentK, PercentD, Slowing, MODE_SMA, 0, MODE_MAIN, CheckCandle)))
{
ObjectSetText(name_arrow, CharToString(sSell), fontSize, "Wingdings", SellColor);
ObjectSetText(name_text, "SELL", 9, "Arial Bold", SellColor);
Confluence[x]--;
}
else
{
ObjectSetText(name_arrow, CharToString(sWait), 10, "Wingdings", NeutralColor);
ObjectSetText(name_text, "WAIT", 9, "Arial Bold", NeutralColor);
}
// RSI arrows and text.
name_arrow = "dI" + IntegerToString(x) + "1";
name_text = "tI" + IntegerToString(x) + "1";
if ((iRSI(NULL, TF[x], RSIP1, PRICE_TYPICAL, 0)) > (iRSI(NULL, TF[x], RSIP2, PRICE_TYPICAL, CheckCandle)))
{
ObjectSetText(name_arrow, CharToString(sBuy), fontSize, "Wingdings", BuyColor);
ObjectSetText(name_text, " BUY", 9, "Arial Bold", BuyColor);
Confluence[x]++;
}
else if
((iRSI(NULL, TF[x], RSIP2, PRICE_TYPICAL, 0)) > (iRSI(NULL, TF[x], RSIP1, PRICE_TYPICAL, CheckCandle)))
{
ObjectSetText(name_arrow, CharToString(sSell), fontSize, "Wingdings", SellColor);
ObjectSetText(name_text, "SELL", 9, "Arial Bold", SellColor);
Confluence[x]--;
}
else
{
ObjectSetText(name_arrow, CharToString(sWait), fontSize, "Wingdings", NeutralColor);
ObjectSetText(name_text, "WAIT", 9, "Arial Bold", NeutralColor);
}
// Entry CCI arrows and text.
name_arrow = "dI" + IntegerToString(x) + "2";
name_text = "tI" + IntegerToString(x) + "2";
if ((iCCI(NULL, TF[x], eCCI[x], PRICE_TYPICAL, CheckCandle)) > 0) // If entry CCI is greater than zero.
{
if ((iCCI(NULL, TF[x], eCCI[x], PRICE_TYPICAL, CheckCandle)) > (iCCI(NULL, TF[x], eCCI[x], PRICE_TYPICAL, CheckCandle + 1)))
{
ObjectSetText(name_arrow, CharToString(sBuy), fontSize, "Wingdings", BuyColor);
ObjectSetText(name_text, " BUY", 9, "Arial Bold", BuyColor);
Confluence[x]++;
}
else
{
ObjectSetText(name_arrow, CharToString(sCCIAgainstBuy), fontSize, "Wingdings", SellColor);
ObjectSetText(name_text, "SELL", 9, "Arial Bold", SellColor);
Confluence[x]--;
}
}
else if
((iCCI(NULL, TF[x], eCCI[x], PRICE_TYPICAL, CheckCandle)) < 0) // If entry CCI is less than zero.
{
if ((iCCI(NULL, TF[x], eCCI[x], PRICE_TYPICAL, CheckCandle)) < (iCCI(NULL, TF[x], eCCI[x], PRICE_TYPICAL, CheckCandle + 1)))
{
ObjectSetText(name_arrow, CharToString(sSell), fontSize, "Wingdings", SellColor);
ObjectSetText(name_text, "SELL", 9, "Arial Bold", SellColor);
Confluence[x]--;
}
else
{
ObjectSetText(name_arrow, CharToString(sCCIAgainstSell), fontSize, "Wingdings", BuyColor);
ObjectSetText(name_text, " BUY", 9, "Arial Bold", BuyColor);
Confluence[x]++;
}
}
else
{
ObjectSetText(name_arrow, CharToString(sWait), 10, "Wingdings", NeutralColor);
ObjectSetText(name_text, "WAIT", 9, "Arial Bold", NeutralColor);
}
// Trend CCI arrows and text.
name_arrow = "dI" + IntegerToString(x) + "3";
name_text = "tI" + IntegerToString(x) + "3";
if ((iCCI(NULL, TF[x], tCCI[x], PRICE_TYPICAL, CheckCandle)) > 0) // If trend CCI greater than zero.
{
if ((iCCI(NULL, TF[x], tCCI[x], PRICE_TYPICAL, CheckCandle)) > (iCCI(NULL, TF[x], tCCI[x], PRICE_TYPICAL, CheckCandle + 1)))
{
ObjectSetText(name_arrow, CharToString(sBuy), fontSize, "Wingdings", BuyColor);
ObjectSetText(name_text, " BUY", 9, "Arial Bold", BuyColor);
Confluence[x]++;
}
else
{
ObjectSetText(name_arrow, CharToString(sCCIAgainstBuy), fontSize, "Wingdings", SellColor);
ObjectSetText(name_text, "SELL", 9, "Arial Bold", SellColor);
Confluence[x]--;
}
}
else if
((iCCI(NULL, TF[x], tCCI[x], PRICE_TYPICAL, CheckCandle)) < 0) // If trend CCI less than zero.
{
if ((iCCI(NULL, TF[x], tCCI[x], PRICE_TYPICAL, CheckCandle)) < (iCCI(NULL, TF[x], tCCI[x], PRICE_TYPICAL, CheckCandle + 1)))
{
ObjectSetText(name_arrow, CharToString(sSell), fontSize, "Wingdings", SellColor);
ObjectSetText(name_text, "SELL", 9, "Arial Bold", SellColor);
Confluence[x]--;
}
else
{
ObjectSetText(name_arrow, CharToString(sCCIAgainstSell), fontSize, "Wingdings", BuyColor);
ObjectSetText(name_text, " BUY", 9, "Arial Bold", BuyColor);
Confluence[x]++;
}
}
else
{
ObjectSetText(name_arrow, CharToString(sWait), 10, "Wingdings", NeutralColor);
ObjectSetText(name_text, "WAIT", 9, "Arial Bold", NeutralColor);
}
}
// Alerts
if ((EnableNativeAlerts) || (EnableEmailAlerts) || (EnablePushAlerts))
{
string buy_text = "Buy confluence:";
string sell_text = "Sell confluence:";
bool need_alert = false, buy = false, sell = false;
static string Text_prev = "";
for (int x = 0; x < 9; x++)
{
if (Confluence[x] == 4)
{
if (buy) buy_text += ",";
buy_text += " " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)TF[x]), 7);
if (Confluence_prev[x] != 4) need_alert = true;
buy = true;
}
else if (Confluence[x] == -4)
{
if (sell) sell_text += ",";
sell_text += " " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)TF[x]), 7);
if (Confluence_prev[x] != -4) need_alert = true;
sell = true;
}
Confluence_prev[x] = Confluence[x];
}
// Confluence alert
if (need_alert)
{
string Text = "TA: " + Symbol() + " - ";
if (buy)
{
Text += buy_text;
if (sell) Text += "; ";
}
if (sell) Text += sell_text;
if (Text != Text_prev)
{
if (EnableNativeAlerts) Alert(Text);
if (EnableEmailAlerts) SendMail("TA Alert", Text);
if (EnablePushAlerts) SendNotification(Text);
Text_prev = Text;
}
}
}
return rates_total;
}
//+------------------------------------------------------------------+
//| Returns true if the given TF is enabled. |
//+------------------------------------------------------------------+
bool TimeframeCheck(ENUM_TIMEFRAMES tf)
{
if ((tf == PERIOD_M1) && (!Enable_M1)) return false;
if ((tf == PERIOD_M5) && (!Enable_M5)) return false;
if ((tf == PERIOD_M15) && (!Enable_M15)) return false;
if ((tf == PERIOD_M30) && (!Enable_M30)) return false;
if ((tf == PERIOD_H1) && (!Enable_H1)) return false;
if ((tf == PERIOD_H4) && (!Enable_H4)) return false;
if ((tf == PERIOD_D1) && (!Enable_D1)) return false;
if ((tf == PERIOD_W1) && (!Enable_W1)) return false;
if ((tf == PERIOD_MN1) && (!Enable_MN1)) return false;
return true;
}
//+------------------------------------------------------------------+