-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy patharrows_base.mq4
361 lines (330 loc) · 9.63 KB
/
arrows_base.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
// Arrows base v5.2
#property version "1.0"
#property strict
#property indicator_chart_window
//#property indicator_separate_window
#property indicator_buffers 8
#define ACT_ON_SWITCH
enum SingalMode
{
SingalModeLive, // Live
SingalModeOnBarClose // On bar close
};
enum DisplayType
{
Arrows, // Arrows
ArrowsOnMainChart, // Arrows on main chart
Candles, // Candles color
Lines, // Lines
Text // Label
};
input SingalMode signal_mode = SingalModeLive; // Signal mode
input bool filter_consecutive = false; // Filter consecutive alerts
input DisplayType Type = Arrows; // Presentation Type
input double shift_arrows_pips = 0.1; // Shift arrows
input color up_color = Blue; // Up color
input color down_color = Red; // Down color
input int font_size = 12; // Font size
input int bars_limit = 1000; // Bars limit
#include <conditions/ACondition.mqh>
#include <conditions/ActOnSwitchCondition.mqh>
#include <conditions/AndCondition.mqh>
#include <Streams/PriceStream.mqh>
#include <signaler.mqh>
#include <AlertSignal.mqh>
#include <Streams/CandleStreams.mqh>
#include <Streams/StreamWrapper.mqh>
#include <Actions/AAction.mqh>
class SetLastSignalAction : public AAction
{
int _signal;
public:
SetLastSignalAction(int signal)
{
_signal = signal;
}
virtual bool DoAction(const int period, const datetime date)
{
current_signal_date = date;
current_signal_side = _signal;
return true;
}
};
class LastSignalNotCondition : public AConditionBase
{
int _signal;
public:
LastSignalNotCondition(int signal)
{
_signal = signal;
}
bool IsPass(const int period, const datetime date)
{
return last_signal_side != _signal;
}
};
AlertSignal* conditions[];
Signaler* mainSignaler;
StreamWrapper* customStream;
int last_signal_side;
datetime current_signal_date;
int current_signal_side;
int CreateAlert(int id, ICondition* condition, IAction* action, int code, string codeStr, string message, color clr, PriceType priceType, int sign)
{
int size = ArraySize(conditions);
ArrayResize(conditions, size + 1);
#ifdef ACT_ON_SWITCH
ActOnSwitchCondition* upSwitch = new ActOnSwitchCondition(_Symbol, (ENUM_TIMEFRAMES)_Period, condition);
condition = upSwitch;
#endif
conditions[size] = new AlertSignal(condition, action, _Symbol, (ENUM_TIMEFRAMES)_Period, mainSignaler, signal_mode == SingalModeOnBarClose);
#ifdef ACT_ON_SWITCH
condition.Release();
#endif
switch (Type)
{
case Arrows:
{
id = conditions[size].RegisterStreams(id, message, code, clr, customStream);
}
break;
case ArrowsOnMainChart:
{
SimplePriceStream* highStream = new SimplePriceStream(_Symbol, (ENUM_TIMEFRAMES)_Period, priceType);
highStream.SetShift(shift_arrows_pips * sign);
static int lastId = 1;
id = conditions[size].RegisterArrows(id, message, IndicatorObjPrefix + IntegerToString(lastId++), code, clr, highStream, font_size);
highStream.Release();
}
break;
case Candles:
{
id = conditions[size].RegisterStreams(id, message, clr);
}
break;
case Lines:
{
id = conditions[size].RegisterLines(id, message, IndicatorObjPrefix + IntegerToString(id), clr);
}
break;
case Text:
{
SimplePriceStream* highStream = new SimplePriceStream(_Symbol, (ENUM_TIMEFRAMES)_Period, priceType);
highStream.SetShift(shift_arrows_pips * sign);
static int lastId = 1;
id = conditions[size].RegisterArrows(id, message, IndicatorObjPrefix + IntegerToString(lastId++), codeStr, clr, highStream, font_size);
highStream.Release();
}
break;
}
return id;
}
int CreateAlert(int id, ENUM_TIMEFRAMES tf, color upColor, color downColor)
{
AndCondition* upCondition = new AndCondition();
upCondition.Add(new UpCondition(_Symbol, tf), false);
SetLastSignalAction* upAction = NULL;
if (filter_consecutive)
{
upCondition.Add(new LastSignalNotCondition(1), false);
upAction = new SetLastSignalAction(1);
}
id = CreateAlert(id, upCondition, upAction, 217, "Up", "Up " + TimeframeToString(tf), upColor, PriceLow, -1);
upCondition.Release();
if (upAction != NULL)
{
upAction.Release();
}
AndCondition* downCondition = new AndCondition();
downCondition.Add(new DownCondition(_Symbol, tf), false);
SetLastSignalAction* downAction = NULL;
if (filter_consecutive)
{
downCondition.Add(new LastSignalNotCondition(-1), false);
downAction = new SetLastSignalAction(-1);
}
id = CreateAlert(id, downCondition, downAction, 218, "Down", "Down " + TimeframeToString(tf), downColor, PriceHigh, 1);
downCondition.Release();
if (downAction != NULL)
{
downAction.Release();
}
return id;
}
class UpCondition : public ACondition
{
public:
UpCondition(const string symbol, ENUM_TIMEFRAMES timeframe)
:ACondition(symbol, timeframe)
{
}
bool IsPass(const int period, const datetime date)
{
//TODO: implement
return false;
}
};
class DownCondition : public ACondition
{
public:
DownCondition(const string symbol, ENUM_TIMEFRAMES timeframe)
:ACondition(symbol, timeframe)
{
}
bool IsPass(const int period, const datetime date)
{
//TODO: implement
return false;
}
};
string IndicatorObjPrefix;
bool NamesCollision(const string name)
{
for (int k = ObjectsTotal(); k >= 0; k--)
{
if (StringFind(ObjectName(0, k), name) == 0)
{
return true;
}
}
return false;
}
string GenerateIndicatorPrefix(const string target)
{
for (int i = 0; i < 1000; ++i)
{
string prefix = target + "_" + IntegerToString(i);
if (!NamesCollision(prefix))
{
return prefix;
}
}
return target;
}
string TimeframeToString(ENUM_TIMEFRAMES tf)
{
switch (tf)
{
case PERIOD_M1: return "M1";
case PERIOD_M5: return "M5";
case PERIOD_D1: return "D1";
case PERIOD_H1: return "H1";
case PERIOD_H4: return "H4";
case PERIOD_M15: return "M15";
case PERIOD_M30: return "M30";
case PERIOD_MN1: return "MN1";
case PERIOD_W1: return "W1";
}
return "";
}
int init()
{
if (!IsDllsAllowed() && advanced_alert)
{
Print("Error: Dll calls must be allowed!");
return INIT_FAILED;
}
IndicatorBuffers(8);
IndicatorObjPrefix = GenerateIndicatorPrefix("indi_short");
IndicatorShortName("...");
mainSignaler = new Signaler();
mainSignaler.SetMessagePrefix(_Symbol + "/" + TimeframeToString((ENUM_TIMEFRAMES)_Period) + ": ");
int id = 0;
if (Type == Arrows)
{
customStream = new StreamWrapper(_Symbol, (ENUM_TIMEFRAMES)_Period);
}
{
id = CreateAlert(id, (ENUM_TIMEFRAMES)_Period, up_color, down_color);
}
if (customStream != NULL)
{
id = customStream.RegisterInternalStream(id);
}
return 0;
}
int deinit()
{
if (customStream != NULL)
{
customStream.Release();
customStream = NULL;
}
delete mainSignaler;
mainSignaler = NULL;
for (int i = 0; i < ArraySize(conditions); ++i)
{
delete conditions[i];
}
ArrayResize(conditions, 0);
ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
return 0;
}
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 (prev_calculated <= 0 || prev_calculated > rates_total)
{
//TODO: initialize your streams here
//ArrayInitialize(ll, EMPTY_VALUE);
if (customStream != NULL)
{
customStream.Init();
}
for (int i = 0; i < ArraySize(conditions); ++i)
{
AlertSignal* item = conditions[i];
item.Init();
}
}
bool timeSeries = ArrayGetAsSeries(time);
bool openSeries = ArrayGetAsSeries(open);
bool highSeries = ArrayGetAsSeries(high);
bool lowSeries = ArrayGetAsSeries(low);
bool closeSeries = ArrayGetAsSeries(close);
bool tickVolumeSeries = ArrayGetAsSeries(tick_volume);
ArraySetAsSeries(time, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
ArraySetAsSeries(tick_volume, true);
double point = MarketInfo(_Symbol, MODE_POINT);
int digits = (int)MarketInfo(_Symbol, MODE_DIGITS);
int mult = digits == 3 || digits == 5 ? 10 : 1;
double pipSize = point * mult;
int toSkip = 1;
for (int pos = MathMin(bars_limit, rates_total - 1 - MathMax(prev_calculated - 1, toSkip)); pos >= 0 && !IsStopped(); --pos)
{
if (customStream != NULL)
{
customStream.SetValue(pos, Close[pos]);
}
if (current_signal_date < Time[pos] && current_signal_side != 0)
{
last_signal_side = current_signal_side;
current_signal_date = Time[signal_mode == SingalModeOnBarClose ? pos + 1 : pos];
}
current_signal_side = 0;
for (int i = 0; i < ArraySize(conditions); ++i)
{
AlertSignal* item = conditions[i];
item.Update(pos);
}
}
ArraySetAsSeries(time, timeSeries);
ArraySetAsSeries(open, openSeries);
ArraySetAsSeries(high, highSeries);
ArraySetAsSeries(low, lowSeries);
ArraySetAsSeries(close, closeSeries);
ArraySetAsSeries(tick_volume, tickVolumeSeries);
return rates_total;
}