-
Notifications
You must be signed in to change notification settings - Fork 1
/
HalfTrend.mq5
301 lines (291 loc) · 10.6 KB
/
HalfTrend.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
//+------------------------------------------------------------------+
//| HalfTrend.mq5 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 11
#property indicator_plots 5
//--- plot
#property indicator_label1 "UP"
#property indicator_color1 Purple // up[] DodgerBlue
#property indicator_type1 DRAW_LINE
#property indicator_width1 2
#property indicator_label2 "DN"
#property indicator_color2 Red // down[]
#property indicator_type2 DRAW_LINE
#property indicator_width2 2
#property indicator_label3 "ATR-LH"
#property indicator_color3 DodgerBlue,Red // atrlo[],atrhi[]
#property indicator_type3 DRAW_COLOR_HISTOGRAM2
#property indicator_width3 1
#property indicator_label4 "ARR-UP"
#property indicator_color4 DodgerBlue // arrup[]
#property indicator_type4 DRAW_ARROW
#property indicator_width4 1
#property indicator_label5 "ARR-DOWN"
#property indicator_color5 Red // arrdwn[]
#property indicator_type5 DRAW_ARROW
#property indicator_width5 1
input int Amplitude = 2;
input bool ShowBars = false;
input bool ShowArrows = true;
input bool alertsOn = false;
input bool alertsOnCurrent = false;
input bool alertsMessage = true;
input bool alertsSound = true;
input bool alertsEmail = false;
bool nexttrend;
double minhighprice, maxlowprice;
double up[], down[], atrlo[], atrhi[], atrclr[], trend[];
double arrup[], arrdwn[];
int ind_mahi, ind_malo, ind_atr;
double iMAHigh[], iMALow[], iATRx[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, up, INDICATOR_DATA);
SetIndexBuffer(1, down, INDICATOR_DATA);
SetIndexBuffer(2, atrlo, INDICATOR_DATA);
SetIndexBuffer(3, atrhi, INDICATOR_DATA);
SetIndexBuffer(4, atrclr, INDICATOR_COLOR_INDEX);
SetIndexBuffer(5, arrup, INDICATOR_DATA);
SetIndexBuffer(6, arrdwn, INDICATOR_DATA);
SetIndexBuffer(7, trend, INDICATOR_CALCULATIONS);
SetIndexBuffer(8, iMAHigh, INDICATOR_CALCULATIONS);
SetIndexBuffer(9, iMALow, INDICATOR_CALCULATIONS);
SetIndexBuffer(10, iATRx, INDICATOR_CALCULATIONS);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
ArraySetAsSeries(up, true);
ArraySetAsSeries(down, true);
ArraySetAsSeries(atrlo, true);
ArraySetAsSeries(atrhi, true);
ArraySetAsSeries(atrclr, true);
ArraySetAsSeries(arrup, true);
ArraySetAsSeries(arrdwn, true);
ArraySetAsSeries(trend, true);
ArraySetAsSeries(iMAHigh, true);
ArraySetAsSeries(iMALow, true);
ArraySetAsSeries(iATRx, true);
if(!ShowBars)
{
PlotIndexSetInteger(2,PLOT_LINE_COLOR,0,clrNONE);
PlotIndexSetInteger(2,PLOT_LINE_COLOR,1,clrNONE);
}
else
{
PlotIndexSetInteger(2,PLOT_LINE_COLOR,0,clrDodgerBlue);
PlotIndexSetInteger(2,PLOT_LINE_COLOR,1,clrRed);
}
if(!ShowArrows)
{
PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_NONE);
PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_NONE);
}
else
{
PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_ARROW);
PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_ARROW);
PlotIndexSetInteger(3, PLOT_ARROW, 225); //233
PlotIndexSetInteger(4, PLOT_ARROW, 226); //234
}
ind_mahi = iMA(NULL, 0, Amplitude, 0, MODE_SMA, PRICE_HIGH);
ind_malo = iMA(NULL, 0, Amplitude, 0, MODE_SMA, PRICE_LOW);
ind_atr = iATR(NULL, 0, 100);
if(ind_mahi == INVALID_HANDLE || ind_mahi == INVALID_HANDLE || ind_atr == INVALID_HANDLE)
{
PrintFormat("Failed to create handle of the indicators, error code %d", GetLastError());
return(INIT_FAILED);
}
nexttrend = 0;
minhighprice = iHigh(NULL, 0, Bars(NULL, 0) - 1);
maxlowprice = iLow(NULL, 0, Bars(NULL, 0) - 1);
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnCalculate(
const int rates_total, // size of input time series
const int prev_calculated, // number of handled bars at the previous call
const datetime& time[], // Time array
const double& open[], // Open array
const double& high[], // High array
const double& low[], // Low array
const double& close[], // Close array
const long& tick_volume[], // Tick Volume array
const long& volume[], // Real Volume array
const int& spread[] // Spread array
)
{
int i, limit, to_copy;
double atr, lowprice_i, highprice_i, lowma, highma;
ArraySetAsSeries(time, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
if(prev_calculated > rates_total || prev_calculated < 0) to_copy = rates_total;
else
{
to_copy = rates_total - prev_calculated;
if(prev_calculated > 0)
to_copy += 10;
}
if(!RefreshBuffers(iMAHigh, iMALow, iATRx, ind_mahi, ind_malo, ind_atr, to_copy))
return(0);
if(prev_calculated == 0)
limit = rates_total - 2;
else
limit = rates_total - prev_calculated + 1;
for(i = limit; i >= 0; i--)
{
lowprice_i = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, Amplitude, i));
highprice_i = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, Amplitude, i));
lowma = NormalizeDouble(iMALow[i], _Digits);
highma = NormalizeDouble(iMAHigh[i], _Digits);
trend[i] = trend[i + 1];
atr = iATRx[i] / 2;
arrup[i] = EMPTY_VALUE;
arrdwn[i] = EMPTY_VALUE;
if(trend[i + 1] != 1.0)
{
maxlowprice = MathMax(lowprice_i, maxlowprice);
if(highma < maxlowprice && close[i] < low[i + 1])
{
trend[i] = 1.0;
nexttrend = 0;
minhighprice = highprice_i;
}
}
else
{
minhighprice = MathMin(highprice_i, minhighprice);
if(lowma > minhighprice && close[i] > high[i + 1])
{
trend[i] = 0.0;
nexttrend = 1;
maxlowprice = lowprice_i;
}
}
//---
if(trend[i] == 0.0)
{
if(trend[i + 1] != 0.0)
{
up[i] = down[i + 1];
up[i + 1] = up[i];
arrup[i] = up[i] - 2 * atr;
}
else
{
up[i] = MathMax(maxlowprice, up[i + 1]);
}
atrhi[i] = up[i] - atr;
atrlo[i] = up[i];
atrclr[i] = 0;
down[i] = 0.0;
}
else
{
if(trend[i + 1] != 1.0)
{
down[i] = up[i + 1];
down[i + 1] = down[i];
arrdwn[i] = down[i] + 2 * atr;
}
else
{
down[i] = MathMin(minhighprice, down[i + 1]);
}
atrhi[i] = down[i] + atr;
atrlo[i] = down[i];
atrclr[i] = 1;
up[i] = 0.0;
}
}
manageAlerts();
return (rates_total);
}
//+------------------------------------------------------------------+
//| Filling indicator buffers from the indicators |
//+------------------------------------------------------------------+
bool RefreshBuffers(double &hi_buffer[],
double &lo_buffer[],
double &atr_buffer[],
int hi_handle,
int lo_handle,
int atr_handle,
int amount
)
{
//--- reset error code
ResetLastError();
//--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index
if(CopyBuffer(hi_handle, 0, 0, amount, hi_buffer) < 0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the MaHigh indicator, error code %d", GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(false);
}
//--- fill a part of the SignalBuffer array with values from the indicator buffer that has index 1
if(CopyBuffer(lo_handle, 0, 0, amount, lo_buffer) < 0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the MaLow indicator, error code %d", GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(false);
}
//--- fill a part of the StdDevBuffer array with values from the indicator buffer
if(CopyBuffer(atr_handle, 0, 0, amount, atr_buffer) < 0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the ATR indicator, error code %d", GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(false);
}
//--- everything is fine
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void manageAlerts()
{
int whichBar;
if (alertsOn)
{
if (alertsOnCurrent)
whichBar = 0;
else
whichBar = 1;
if (arrup[whichBar] != EMPTY_VALUE) doAlert(whichBar, "up");
if (arrdwn[whichBar] != EMPTY_VALUE) doAlert(whichBar, "down");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void doAlert(int forBar, string doWhat)
{
static string previousAlert = "nothing";
static datetime previousTime;
string message;
if (previousAlert != doWhat || previousTime != iTime(NULL, 0, forBar))
{
previousAlert = doWhat;
previousTime = iTime(NULL, 0, forBar);
message = StringFormat("%s at %s", Symbol(), TimeToString(TimeLocal(), TIME_SECONDS), " HalfTrend signal ", doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(Symbol(), StringFormat("HalfTrend %s", message));
if (alertsSound) PlaySound("alert2.wav");
}
}
//+------------------------------------------------------------------+