-
Notifications
You must be signed in to change notification settings - Fork 3
/
TradersDynamicIndex.cs
346 lines (298 loc) · 19.3 KB
/
TradersDynamicIndex.cs
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
// -------------------------------------------------------------------------------
// Based on TradersDynamicIndex.mq4 by Dean Malone
//
// Shows trend direction, strength, and volatility.
// Green line - RSI Price line.
// Red line - Trade Signal line.
// Blue lines - Volatility Band.
// Yellow line - Market Base line.
//
// Version 1.07
// Copyright 2023, EarnForex.com
// https://www.earnforex.com/metatrader-indicators/Traders-Dynamic-Index/
// -------------------------------------------------------------------------------
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Levels(32, 50, 68)]
[Indicator(AccessRights = AccessRights.None)]
public class TradersDynamicIndex : Indicator
{
public enum ENUM_CANDLE_TO_CHECK
{
Current = 0,
Previous = 1
}
[Parameter("RSI Period (8-25)", DefaultValue = 13, MinValue = 1)]
public int RSI_Period { get; set; }
[Parameter()]
public DataSeries Source { get; set; }
[Parameter("Volatility Band (20-40)", DefaultValue = 34, MinValue = 1)]
public int Volatility_Band { get; set; }
[Parameter("Standard Deviations (1-3)", DefaultValue = 1.6185, MinValue = 0)]
public double StdDev { get; set; }
[Parameter("RSI Price MA Period", DefaultValue = 2, MinValue = 1)]
public int RSI_Price_Line { get; set; }
[Parameter("Price MA Type", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType RSI_Price_Type { get; set; }
[Parameter("Trade Signal MA Period", DefaultValue = 7, MinValue = 1)]
public int Trade_Signal_Line { get; set; }
[Parameter("Signal MA Type", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType Trade_Signal_Type { get; set; }
[Parameter("Upper timeframe")]
public TimeFrame UpperTimeframe { get; set; }
[Parameter("Enable email alerts", DefaultValue = false)]
public bool EnableEmailAlerts { get; set; }
[Parameter("AlertEmail: Email From", DefaultValue = "")]
public string AlertEmailFrom { get; set; }
[Parameter("AlertEmail: Email To", DefaultValue = "")]
public string AlertEmailTo { get; set; }
[Parameter("Enable arrows", DefaultValue = false)]
public bool EnableArrowAlerts { get; set; }
[Parameter("Enable alerts when the red line crosses the yellow line?", DefaultValue = false)]
public bool EnableRedYellowCrossAlert { get; set; }
[Parameter("Enable alerts when the green line crosses the blue line above 68 or below 32?", DefaultValue = false)]
public bool EnableHookAlert { get; set; }
[Parameter("Enable alerts when the green line crosses the red line?", DefaultValue = false)]
public bool EnableGreenRedCrossAlert { get; set; }
[Parameter("Enable alerts when the yellow line crosses the green line?", DefaultValue = false)]
public bool EnableYellowGreenCrossAlert { get; set; }
[Parameter(DefaultValue = ENUM_CANDLE_TO_CHECK.Previous)]
public ENUM_CANDLE_TO_CHECK TriggerCandle { get; set; }
[Parameter(DefaultValue = "Green")]
public string RedYellowCrossArrowBullishColor { get; set; }
[Parameter(DefaultValue = "Red")]
public string RedYellowCrossArrowBearishColor { get; set; }
[Parameter(DefaultValue = "Green")]
public string HookArrowBullishColor { get; set; }
[Parameter(DefaultValue = "Red")]
public string HookArrowBearishColor { get; set; }
[Parameter(DefaultValue = "Green")]
public string GreenRedCrossArrowBullishColor { get; set; }
[Parameter(DefaultValue = "Red")]
public string GreenRedCrossArrowBearishColor { get; set; }
[Parameter(DefaultValue = "Green")]
public string YellowGreenCrossArrowBullishColor { get; set; }
[Parameter(DefaultValue = "Red")]
public string YellowGreenCrossArrowBearishColor { get; set; }
[Parameter(DefaultValue = "TDI-")]
public string ArrowPrefix { get; set; }
[Output("Upper Volatility Band", LineColor = "MediumBlue")]
public IndicatorDataSeries UpZone { get; set; }
[Output("Lower Volatility Band", LineColor = "MediumBlue")]
public IndicatorDataSeries DnZone { get; set; }
[Output("Middle Volatility Band", LineColor = "Yellow", Thickness = 2)]
public IndicatorDataSeries MdZone { get; set; }
[Output("RSI Price Line", LineColor = "Green", Thickness = 2)]
public IndicatorDataSeries MaBuf { get; set; }
[Output("Trade Signal Line", LineColor = "Red", Thickness = 2)]
public IndicatorDataSeries MbBuf { get; set; }
// Output buffers:
private RelativeStrengthIndex RSI;
private MovingAverage MA_Price;
private MovingAverage MA_Signal;
private BollingerBands VolatilityBands;
// MTF:
private bool UseUpperTimeFrame;
private Bars customBars;
// Alerts:
private DateTime LastAlertTimeRedYellow, LastAlertTimeHook, LastAlertTimeGreenRed, LastAlertTimeYellowGreen, unix_epoch;
private int prev_index = -1;
private int int_tc;
protected override void Initialize()
{
if (UpperTimeframe <= TimeFrame)
{
Print("UpperTimeframe <= current timeframe. Ignored.");
UseUpperTimeFrame = false;
customBars = Bars;
}
else
{
UseUpperTimeFrame = true;
customBars = MarketData.GetBars(UpperTimeframe);
}
RSI = Indicators.RelativeStrengthIndex(customBars.ClosePrices, RSI_Period);
VolatilityBands = Indicators.BollingerBands(RSI.Result, Volatility_Band, StdDev, MovingAverageType.Simple);
MA_Price = Indicators.MovingAverage(RSI.Result, RSI_Price_Line, RSI_Price_Type);
MA_Signal = Indicators.MovingAverage(RSI.Result, Trade_Signal_Line, Trade_Signal_Type);
unix_epoch = new DateTime(1970, 1, 1, 0, 0, 0);
LastAlertTimeRedYellow = unix_epoch;
LastAlertTimeHook = unix_epoch;
LastAlertTimeGreenRed = unix_epoch;
LastAlertTimeYellowGreen = unix_epoch;
int_tc = (int)TriggerCandle;
}
public override void Calculate(int index)
{
int customIndex = index;
int cnt = 0; // How many bars of the current timeframe should be recalculated.
if (UseUpperTimeFrame)
{
customIndex = customBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// Find how many current timeframe bars should be recalculated:
while (customBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - cnt]) == customIndex)
{
cnt++;
}
}
else
{
cnt = 1; // Non-MTF.
if (customIndex <= RSI_Period) return; // Too early to calculate anything.
}
for (int i = 0; i < cnt; i++) // Making sure to rewrite previous lower timeframe candles with the updated upper one.
{
UpZone[index - i] = VolatilityBands.Top[customIndex];
DnZone[index - i] = VolatilityBands.Bottom[customIndex];
MdZone[index - i] = VolatilityBands.Main[customIndex];
MaBuf[index - i] = MA_Price.Result[customIndex];
MbBuf[index - i] = MA_Signal.Result[customIndex];
}
// Arrows
if (EnableArrowAlerts)
{
int lower_i = Bars.OpenTimes.GetIndexByTime(customBars.OpenTimes[customIndex - int_tc]);
if (EnableRedYellowCrossAlert)
{
if ((MA_Signal.Result[customIndex - int_tc] > VolatilityBands.Main[customIndex - int_tc]) && (MA_Signal.Result[customIndex - int_tc - 1] <= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Chart.DrawIcon(ArrowPrefix + "RY" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.UpTriangle, lower_i, Bars.LowPrices[lower_i], Color.FromName(RedYellowCrossArrowBullishColor));
}
else if ((MA_Signal.Result[customIndex - int_tc] < VolatilityBands.Main[customIndex - int_tc]) && (MA_Signal.Result[customIndex - int_tc - 1] >= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Chart.DrawIcon(ArrowPrefix + "RY" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.DownTriangle, lower_i, Bars.HighPrices[lower_i], Color.FromName(RedYellowCrossArrowBearishColor));
}
}
if (EnableHookAlert)
{
if ((MA_Price.Result[customIndex - int_tc] < VolatilityBands.Top[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] >= VolatilityBands.Top[customIndex - int_tc - 1]) && ((MA_Price.Result[customIndex - int_tc] > 68) || (MA_Price.Result[customIndex - int_tc - 1] > 68)) && ((VolatilityBands.Top[customIndex - int_tc] > 68) || (VolatilityBands.Top[customIndex - int_tc - 1] > 68)))
{
Chart.DrawIcon(ArrowPrefix + "H" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.DownArrow, lower_i, Bars.HighPrices[lower_i], Color.FromName(HookArrowBearishColor));
}
else if ((MA_Price.Result[customIndex - int_tc] >= VolatilityBands.Bottom[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] < VolatilityBands.Bottom[customIndex - int_tc - 1]) && ((MA_Price.Result[customIndex - int_tc] < 32) || (MA_Price.Result[customIndex - int_tc - 1] < 32)) && ((VolatilityBands.Bottom[customIndex - int_tc] < 32) || (VolatilityBands.Bottom[customIndex - int_tc - 1] < 32)))
{
Chart.DrawIcon(ArrowPrefix + "H" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.UpArrow, lower_i, Bars.LowPrices[lower_i], Color.FromName(HookArrowBullishColor));
}
}
if (EnableGreenRedCrossAlert)
{
if ((MA_Price.Result[customIndex - int_tc] < MA_Signal.Result[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] >= MA_Signal.Result[customIndex - int_tc - 1]))
{
Chart.DrawIcon(ArrowPrefix + "GR" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.Diamond, lower_i, Bars.HighPrices[lower_i], Color.FromName(HookArrowBearishColor));
}
else if ((MA_Price.Result[customIndex - int_tc] > MA_Signal.Result[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] <= MA_Signal.Result[customIndex - int_tc - 1]))
{
Chart.DrawIcon(ArrowPrefix + "GR" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.Diamond, lower_i, Bars.LowPrices[lower_i], Color.FromName(HookArrowBullishColor));
}
}
if (EnableYellowGreenCrossAlert)
{
if ((MA_Price.Result[customIndex - int_tc] < VolatilityBands.Main[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] >= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Chart.DrawIcon(ArrowPrefix + "YG" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.Star, lower_i, Bars.HighPrices[lower_i], Color.FromName(HookArrowBearishColor));
}
else if ((MA_Price.Result[customIndex - int_tc] > VolatilityBands.Main[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] <= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Chart.DrawIcon(ArrowPrefix + "YG" + Bars.OpenTimes[lower_i].ToString(), ChartIconType.Star, lower_i, Bars.LowPrices[lower_i], Color.FromName(HookArrowBullishColor));
}
}
}
// Alerts
if (!EnableEmailAlerts) return; // No need to go further.
if ((!EnableRedYellowCrossAlert) && (!EnableHookAlert) && (!EnableGreenRedCrossAlert) && (!EnableYellowGreenCrossAlert)) return;
string Text = "";
if ((EnableRedYellowCrossAlert) && (LastAlertTimeRedYellow > unix_epoch) && (customBars.OpenTimes.LastValue > LastAlertTimeRedYellow))
{
if ((MA_Signal.Result[customIndex - int_tc] > VolatilityBands.Main[customIndex - int_tc]) && (MA_Signal.Result[customIndex - int_tc - 1] <= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Bullish cross";
DoAlert(customIndex, Text);
LastAlertTimeRedYellow = customBars.OpenTimes.LastValue;
}
else if ((MA_Signal.Result[customIndex - int_tc] < VolatilityBands.Main[customIndex - int_tc]) && (MA_Signal.Result[customIndex - int_tc - 1] >= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Bearish cross";
DoAlert(customIndex, Text);
LastAlertTimeRedYellow = customBars.OpenTimes.LastValue;
}
}
if ((EnableHookAlert) && (LastAlertTimeHook > unix_epoch) && (customBars.OpenTimes.LastValue > LastAlertTimeHook))
{
if ((MA_Price.Result[customIndex - int_tc] < VolatilityBands.Top[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] >= VolatilityBands.Top[customIndex - int_tc - 1]) && ((MA_Price.Result[customIndex - int_tc] > 68) || (MA_Price.Result[customIndex - int_tc - 1] > 68)) && ((VolatilityBands.Top[customIndex - int_tc] > 68) || (VolatilityBands.Top[customIndex - int_tc - 1] > 68)))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Hook cross down";
DoAlert(customIndex, Text);
LastAlertTimeHook = customBars.OpenTimes.LastValue;
}
else if ((MA_Price.Result[customIndex - int_tc] >= VolatilityBands.Bottom[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] < VolatilityBands.Bottom[customIndex - int_tc - 1]) && ((MA_Price.Result[customIndex - int_tc] < 32) || (MA_Price.Result[customIndex - int_tc - 1] < 32)) && ((VolatilityBands.Bottom[customIndex - int_tc] < 32) || (VolatilityBands.Bottom[customIndex - int_tc - 1] < 32)))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Hook cross up";
DoAlert(customIndex, Text);
LastAlertTimeHook = customBars.OpenTimes.LastValue;
}
}
if ((EnableGreenRedCrossAlert) && (LastAlertTimeGreenRed > unix_epoch) && (customBars.OpenTimes.LastValue > LastAlertTimeGreenRed))
{
if ((MA_Price.Result[customIndex - int_tc] < MA_Signal.Result[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] >= MA_Signal.Result[customIndex - int_tc - 1]))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Green line crossed red one from above";
DoAlert(customIndex, Text);
LastAlertTimeGreenRed = customBars.OpenTimes.LastValue;
}
else if ((MA_Price.Result[customIndex - int_tc] > MA_Signal.Result[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] <= MA_Signal.Result[customIndex - int_tc - 1]))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Green line crossed red one from below";
DoAlert(customIndex, Text);
LastAlertTimeGreenRed = customBars.OpenTimes.LastValue;
}
}
if ((EnableYellowGreenCrossAlert) && (LastAlertTimeYellowGreen > unix_epoch) && (customBars.OpenTimes.LastValue > LastAlertTimeYellowGreen))
{
if ((MA_Price.Result[customIndex - int_tc] < VolatilityBands.Main[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] >= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Green line crossed yellow one from above";
DoAlert(customIndex, Text);
LastAlertTimeYellowGreen = customBars.OpenTimes.LastValue;
}
else if ((MA_Price.Result[customIndex - int_tc] > VolatilityBands.Main[customIndex - int_tc]) && (MA_Price.Result[customIndex - int_tc - 1] <= VolatilityBands.Main[customIndex - int_tc - 1]))
{
Text = "TDI Alert: " + Symbol.Name + " - " + TimeFrame.Name + " - Green line crossed yellow one from below";
DoAlert(customIndex, Text);
LastAlertTimeYellowGreen = customBars.OpenTimes.LastValue;
}
}
if ((LastAlertTimeRedYellow == unix_epoch) && (prev_index == index)) LastAlertTimeRedYellow = customBars.OpenTimes.LastValue;
if ((LastAlertTimeHook == unix_epoch) && (prev_index == index)) LastAlertTimeHook = customBars.OpenTimes.LastValue;
if ((LastAlertTimeGreenRed == unix_epoch) && (prev_index == index)) LastAlertTimeGreenRed = customBars.OpenTimes.LastValue;
if ((LastAlertTimeYellowGreen == unix_epoch) && (prev_index == index)) LastAlertTimeYellowGreen = customBars.OpenTimes.LastValue;
prev_index = index;
}
protected override void OnDestroy()
{
if (EnableArrowAlerts)
{
var icons = Chart.FindAllObjects(ChartObjectType.Icon);
for (int i = icons.Length - 1; i >= 0; i--)
{
if (icons[i].Name.StartsWith(ArrowPrefix))
{
Chart.RemoveObject(icons[i].Name);
}
}
}
}
private void DoAlert(int customIndex, string text)
{
text += "\nCurrent rate = " + Symbol.Bid.ToString() + "/" + Symbol.Ask.ToString() +
"\nIndicator buffers:\nVB High = " + VolatilityBands.Top[customIndex - int_tc].ToString() +
"\nMarket Base Line = " + VolatilityBands.Main[customIndex - int_tc].ToString() +
"\nVB Low = " + VolatilityBands.Bottom[customIndex - int_tc].ToString() +
"\nRSI Price Line = " + MA_Price.Result[customIndex - int_tc].ToString() +
"\nTrade Signal Line = " + MA_Signal.Result[customIndex - int_tc].ToString();
Notifications.SendEmail(AlertEmailFrom, AlertEmailTo, "TDI Alert - " + Symbol.Name + " @ " + TimeFrame.Name, text);
}
}
}