-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRecentHighLowAlert.mq4
More file actions
218 lines (188 loc) · 14.5 KB
/
Copy pathRecentHighLowAlert.mq4
File metadata and controls
218 lines (188 loc) · 14.5 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
//+------------------------------------------------------------------+
//| RecentHighLowAlert.mq4 |
//| 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 strict
#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_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;
ENUM_TIMEFRAMES UsedTimeFrame;
void OnInit()
{
// Determine the timeframe to use
UsedTimeFrame = TimeFrame;
if (TimeFrame == PERIOD_CURRENT)
UsedTimeFrame = (ENUM_TIMEFRAMES)Period();
else if (TimeFrame < Period())
{
UsedTimeFrame = (ENUM_TIMEFRAMES)Period();
Print("Selected timeframe is lower than current chart timeframe. Using current timeframe instead.");
}
SetIndexDrawBegin(0, N);
SetIndexDrawBegin(1, N);
SetIndexBuffer(0, HighBuf);
SetIndexBuffer(1, LowBuf);
// Update indicator label to show used timeframe
string tf_str = StringSubstr(EnumToString(UsedTimeFrame), 7);
string shift_str = "";
if (Shift > 0) shift_str = " -" + IntegerToString(Shift);
SetIndexLabel(0, "High (" + tf_str + shift_str + ")");
SetIndexLabel(1, "Low (" + tf_str + shift_str + ")");
}
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[])
{
// Too few bars to do anything.
if (Bars <= N) return 0;
//Print("Bars = ", Bars);
//Print("prev_calculated = ", prev_calculated);
int counted = prev_calculated;
if (counted > 0) counted--;
int limit = Bars - counted;
if (limit > Bars - N - 1) limit = Bars - N - 1;
for (int i = limit - 1; i >= 0; 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 (tf_shift == -1)
{
return 0;
// Not enough data, use previous values
if (i < Bars - 1)
{
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))
{
if (iBars(_Symbol, UsedTimeFrame) == 0) return 0; // Higher TF data not loaded yet.
// Not enough data, use previous values
if (i < Bars - 1)
{
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 current_high = HighBuf[TriggerCandle];
double current_low = LowBuf[TriggerCandle];
// Check if price crossed above high
if (Bid > current_high)
{
// Only alert if we weren't already above high
if (!PriceAboveHigh && (LastHighAlert != Time[0]))
{
SendAlert(HIGH, current_high);
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[0]))
{
SendAlert(LOW, current_low);
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)
{
string alert = "Local " + Symbol() + " @ " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7);
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[0];
}
else if (direction == LOW)
{
alert = alert + "low (" + tf_str + ")";
subject = "Low broken @ " + Symbol() + " - " + tf_str;
LastLowAlert = Time[0];
}
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(alert);
}
//+------------------------------------------------------------------+