-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRecentHighLowAlert.mq5
More file actions
246 lines (213 loc) · 16.7 KB
/
Copy pathRecentHighLowAlert.mq5
File metadata and controls
246 lines (213 loc) · 16.7 KB
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
//+------------------------------------------------------------------+
//| RecentHighLowAlert.mq5 |
//| Copyright © 2013-2025, EarnForex.com |
//| https://www.earnforex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013-2025, EarnForex.com"
#property link "https://www.earnforex.com/indicators/Recent-High-Low-Alert/"
#property version "1.02"
#property description "Draws lines on the High/Low of the recent N bars from selected timeframe."
#property description "Alerts when the Bid price of the current bar crosses previous High/Low."
#property description "Shift parameter allows displaying High/Low from previous periods."
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_color1 clrDodgerBlue
#property indicator_type1 DRAW_LINE
#property indicator_label1 "High"
#property indicator_color2 clrYellow
#property indicator_type2 DRAW_LINE
#property indicator_label2 "Low"
#define HIGH 1
#define LOW 0
enum enum_candle_to_check
{
Current,
Previous
};
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Timeframe for High/Low calculation
input int N = 20; // Number of bars to analyze
input int Shift = 0; // Shift back in periods (0 = current period)
input bool EnableNativeAlerts = false;
input bool EnableSoundAlerts = false;
input string SoundFile = "alert.wav"; // Sound file for alerts
input bool EnableEmailAlerts = false;
input bool EnablePushAlerts = false;
input enum_candle_to_check TriggerCandle = Previous;
double HighBuf[];
double LowBuf[];
datetime LastHighAlert = D'1970.01.01';
datetime LastLowAlert = D'1970.01.01';
bool PriceAboveHigh = false;
bool PriceBelowLow = false;
bool JustRefreshed = false; // Flag used to mark if an attempt to refresh the chart was made. It is done when higher TF chart returns zero bars.
ENUM_TIMEFRAMES UsedTimeFrame;
//+------------------------------------------------------------------+
//| Custom iBarShift function for when standard iBarShift fails |
//+------------------------------------------------------------------+
int iBarShiftCustom(string symbol, ENUM_TIMEFRAMES tf, datetime time) // Always exact = false.
{
int i = 0;
int bars = iBars(symbol, tf);
while (iTime(symbol, tf, i) > time)
{
i++;
if (i >= bars) return -1;
}
return i;
}
void OnInit()
{
// Determine the timeframe to use
UsedTimeFrame = TimeFrame;
if (TimeFrame == PERIOD_CURRENT)
UsedTimeFrame = Period();
else if (TimeFrame < Period())
{
UsedTimeFrame = Period();
Print("Selected timeframe is lower than current chart timeframe. Using current timeframe instead.");
}
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, N);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, N);
SetIndexBuffer(0, HighBuf, INDICATOR_DATA);
SetIndexBuffer(1, LowBuf, INDICATOR_DATA);
// Update indicator label to show used timeframe
string tf_str = StringSubstr(EnumToString(UsedTimeFrame), 7);
string shift_str = "";
if (Shift > 0) shift_str = " -" + IntegerToString(Shift);
PlotIndexSetString(0, PLOT_LABEL, "High (" + tf_str + shift_str + ")");
PlotIndexSetString(1, PLOT_LABEL, "Low (" + tf_str + shift_str + ")");
JustRefreshed = false;
}
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 (rates_total <= N) return 0;
if (iBars(_Symbol, UsedTimeFrame) == 0)
{
if (JustRefreshed) return rates_total; // Avoid infinite refreshing on the same data in MTF mode.
JustRefreshed = true;
ChartSetSymbolPeriod(0, NULL, 0); // Forces refresh.
return 0; // Higher TF data not loaded yet.
}
// Skip calculated bars
int start = prev_calculated - 1;
// First run
if (start < N) start = N;
for (int i = start; i < rates_total; i++)
{
// Get the bar shift on the selected timeframe that corresponds to current chart time
int tf_shift = iBarShift(_Symbol, UsedTimeFrame, Time[i], false);
// If iBarShift fails, try custom function
if (tf_shift == -1)
{
tf_shift = iBarShiftCustom(_Symbol, UsedTimeFrame, Time[i]);
}
if (tf_shift == -1)
{
return 0;
// Not enough data, use previous values
if (i > 0)
{
HighBuf[i] = HighBuf[i-1];
LowBuf[i] = LowBuf[i-1];
}
continue;
}
// Add the Shift to go back in periods
tf_shift += Shift;
// Check if we have enough bars after applying shift
if (tf_shift + N - 1 >= iBars(_Symbol, UsedTimeFrame))
{
// Not enough data, use previous values
if (i > 0)
{
HighBuf[i] = HighBuf[i-1];
LowBuf[i] = LowBuf[i-1];
}
continue;
}
// Find highest high and lowest low in the last N bars of selected timeframe
double highest = 0;
double lowest = DBL_MAX;
for (int j = 0; j < N; j++)
{
double high_value = iHigh(_Symbol, UsedTimeFrame, tf_shift + j);
double low_value = iLow(_Symbol, UsedTimeFrame, tf_shift + j);
if (high_value > highest) highest = high_value;
if (low_value < lowest) lowest = low_value;
}
HighBuf[i] = highest;
LowBuf[i] = lowest;
}
// Alert checking
double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double current_high = HighBuf[rates_total - 1 - TriggerCandle];
double current_low = LowBuf[rates_total - 1 - TriggerCandle];
// Check if price crossed above high
if (Bid > current_high)
{
// Only alert if we weren't already above high
if (!PriceAboveHigh && (LastHighAlert != Time[rates_total - 1]))
{
SendAlert(HIGH, current_high, Time[rates_total - 1]);
PriceAboveHigh = true;
}
}
else
{
// Price is not above high, reset flag
PriceAboveHigh = false;
}
// Check if price crossed below low
if (Bid < current_low)
{
// Only alert if we weren't already below low
if (!PriceBelowLow && (LastLowAlert != Time[rates_total - 1]))
{
SendAlert(LOW, current_low, Time[rates_total - 1]);
PriceBelowLow = true;
}
}
else
{
// Price is not below low, reset flag
PriceBelowLow = false;
}
return rates_total;
}
//+------------------------------------------------------------------+
//| Issues alerts and remembers the last sent alert time. |
//+------------------------------------------------------------------+
void SendAlert(int direction, double price, datetime time)
{
string alert = "Local ";
string subject;
string tf_str = StringSubstr(EnumToString(UsedTimeFrame), 7);
if (direction == HIGH)
{
alert = alert + "high (" + tf_str + ")";
subject = "High broken @ " + Symbol() + " - " + tf_str;
LastHighAlert = time;
}
else if (direction == LOW)
{
alert = alert + "low (" + tf_str + ")";
subject = "Low broken @ " + Symbol() + " - " + tf_str;
LastLowAlert = time;
}
alert = alert + " broken at " + DoubleToString(price, _Digits) + ".";
if (EnableNativeAlerts) Alert(alert);
if (EnableSoundAlerts) PlaySound(SoundFile);
if (EnableEmailAlerts) SendMail(subject, TimeToString(TimeCurrent(), TIME_DATE | TIME_SECONDS) + " " + alert);
if (EnablePushAlerts) SendNotification(subject + " @ " + DoubleToString(price, _Digits));
}
//+------------------------------------------------------------------+