-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsx4.mq4
352 lines (313 loc) · 19.8 KB
/
sx4.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
//+------------------------------------------------------------------+
//| NNFX_News_Offline.mq4 |
//| Copyright 2020, Gonçalo Esteves |
//| https://github.com/goncaloe |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, cryptek"
#property link "https://github.com/goncaloe"
#property version "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 clrTomato
#property indicator_color2 clrCornflowerBlue
enum DST {
NO_DST=0, // None
US_DST=1, // US DST
EU_DST=2, // European DST
RU_DST=3, // Russian DST
AU_AEDT=4 // Australian AEDT
};
enum GMT {
GMT_M12=-12, // GMT-12
GMT_M11=-11, // GMT-11
GMT_M10=-10, // GMT-10
GMT_M09=-9, // GMT-09
GMT_M08=-8, // GMT-08
GMT_M07=-7, // GMT-07
GMT_M06=-6, // GMT-06
GMT_M05=-5, // GMT-05
GMT_M04=-4, // GMT-04
GMT_M03=-3, // GMT-03
GMT_M02=-2, // GMT-02
GMT_M01=-1, // GMT-01
GMT_P00=0, // GMT+00
GMT_P01=1, // GMT+01
GMT_P02=2, // GMT+02
GMT_P03=3, // GMT+03
GMT_P04=4, // GMT+04
GMT_P05=5, // GMT+05
GMT_P06=6, // GMT+06
GMT_P07=7, // GMT+07
GMT_P08=8, // GMT+08
GMT_P09=9, // GMT+09
GMT_P10=10, // GMT+10
GMT_P11=11, // GMT+11
GMT_P12=12, // GMT+12
};
input GMT GMTOffset = 0; // GMT offset
input DST DSTZone = 0; // DST setting
extern bool drawLine = true; // Draw Lines
string baseCurrency;
string quoteCurrency;
int startBar, endBar;
struct __news_event {
datetime day;
string event;
int impact;
};
//buffers
double buffer1[];
double buffer2[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit(){
int len = StringLen(Symbol()); // for soft4fx
baseCurrency = StringSubstr(Symbol(),len - 6,3);
quoteCurrency = StringSubstr(Symbol(),len - 3,3);
//--- indicator line
SetIndexStyle(0,DRAW_ARROW,EMPTY,3, clrTomato);
SetIndexArrow(0, 159);
SetIndexBuffer(0, buffer1);
SetIndexLabel(0, baseCurrency);
SetIndexStyle(1,DRAW_ARROW,EMPTY,3, clrCornflowerBlue);
SetIndexArrow(1, 159);
SetIndexBuffer(1, buffer2);
SetIndexLabel(1, quoteCurrency);
IndicatorSetDouble(INDICATOR_MINIMUM, 0);
IndicatorSetDouble(INDICATOR_MAXIMUM, 2);
IndicatorShortName("NNFX News (" + baseCurrency + " / " + quoteCurrency + ")");
drawLegend(baseCurrency, 8, 15, clrTomato);
drawLegend(quoteCurrency, 8, 30, clrCornflowerBlue);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
if (drawLine){
ObjectsDeleteAll(0, "News_H_");
}
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[]){
int Counted_bars;
Counted_bars=IndicatorCounted();
int limit = Bars-Counted_bars-1;
if(limit == 0){
return 0;
}
if(baseCurrency == "USD"){
loadUSD(limit, 1);
}
else if(baseCurrency == "EUR"){
loadEUR(limit, 1);
}
else if(baseCurrency == "GBP"){
loadGBP(limit, 1);
}
else if(baseCurrency == "AUD"){
loadAUD(limit, 1);
}
else if(baseCurrency == "CAD"){
loadCAD(limit, 1);
}
else if(baseCurrency == "CHF"){
loadCHF(limit, 1);
}
else if(baseCurrency == "JPY"){
loadJPY(limit, 1);
}
else if(baseCurrency == "NZD"){
loadNZD(limit, 1);
}
if(quoteCurrency == "USD"){
loadUSD(limit, 2);
}
else if(quoteCurrency == "EUR"){
loadEUR(limit, 2);
}
else if(quoteCurrency == "GBP"){
loadGBP(limit, 2);
}
else if(quoteCurrency == "AUD"){
loadAUD(limit, 2);
}
else if(quoteCurrency == "CAD"){
loadCAD(limit, 2);
}
else if(quoteCurrency == "CHF"){
loadCHF(limit, 2);
}
else if(quoteCurrency == "JPY"){
loadJPY(limit, 2);
}
else if(quoteCurrency == "NZD"){
loadNZD(limit, 2);
}
return 0;
}
void drawVLine(datetime d, string desc, int buff){
string name = "News_H_" + TimeToStr(d,TIME_DATE|TIME_MINUTES);
ObjectCreate(name,OBJ_VLINE,0,d,0);
ObjectSet(name,OBJPROP_WIDTH,1);
ObjectSet(name,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(name,OBJPROP_BACK,true);
if(buff == 1){
ObjectSet(name,OBJPROP_COLOR,clrPink);
ObjectSetString(0,name,OBJPROP_TEXT,baseCurrency + ": " + desc);
}
else {
ObjectSet(name,OBJPROP_COLOR,clrLightBlue);
ObjectSetString(0,name,OBJPROP_TEXT,quoteCurrency + ": " + desc);
}
}
void drawLegend(string currency, int xpos, int ypos, color dcolor){
string rect_id = "News_H_leg_" + currency;
if (ObjectFind(rect_id) != 0){
ObjectCreate(rect_id, OBJ_RECTANGLE_LABEL, ChartWindowFind(), 0, 0);
}
ObjectSet(rect_id,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
ObjectSet(rect_id,OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSet(rect_id, OBJPROP_BGCOLOR, dcolor);
ObjectSet(rect_id, OBJPROP_BORDER_TYPE, BORDER_FLAT);
ObjectSet(rect_id, OBJPROP_WIDTH, 0);
ObjectSet(rect_id, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(rect_id,OBJPROP_XDISTANCE,xpos);
ObjectSet(rect_id,OBJPROP_YDISTANCE,ypos);
ObjectSet(rect_id, OBJPROP_XSIZE, 10);
ObjectSet(rect_id, OBJPROP_YSIZE, 10);
string lbl_id = "News_H_lab_" + currency;
if (ObjectFind(lbl_id) != 0){
ObjectCreate(lbl_id, OBJ_LABEL, ChartWindowFind(), 0, 0);
}
ObjectSet(lbl_id,OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSet(lbl_id,OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
ObjectSet(lbl_id,OBJPROP_XDISTANCE,xpos + 10);
ObjectSet(lbl_id,OBJPROP_YDISTANCE,ypos - 2);
ObjectSetText(lbl_id, currency + " Events", 8, "Arial", dcolor);
}
int DSTOffset(datetime t, int DSTType) {
if (isDST(t, DSTType)) {
return 3600;
}
return 0;
}
bool isDST(datetime t, int zone = 0) {
datetime dstStart;
datetime dstEnd;
if (zone == 2 || zone == 3) { // Europe & Russia
if (zone == 3 && t > D'28.03.2011') return (false); // no DST for Russia after 28.03.2011
dstStart = StrToTime((string)TimeYear(t) + ".03.31 01:00");
while (TimeDayOfWeek(dstStart) != 0) { // last Sunday of March
dstStart -= 3600 * 24;
}
dstEnd = StrToTime((string)TimeYear(t) + ".10.31 01:00");
while (TimeDayOfWeek(dstEnd) != 0) { // last Sunday of October
dstEnd -= 3600 * 24;
}
if (t >= dstStart && t < dstEnd) {
return (true);
}
else {
return (false);
}
}
else if (zone == 1) { // US
dstStart = StrToTime((string)TimeYear(t) + ".03.01 00:00"); // should be Saturday 21:00 GMT (New York is at GMT-5 and it changes at 2AM) but it doesn't really matter since we have no market during the weekend
int sundayCount = 0;
while (true) { // second Sunday of March
if (TimeDayOfWeek(dstStart) == 0) {
sundayCount++;
if (sundayCount == 2) break;
}
dstStart += 3600 * 24;
}
dstEnd = StrToTime((string)TimeYear(t) + ".11.01 00:00");
while (TimeDayOfWeek(dstEnd) != 0) { // first Sunday of November
dstEnd += 3600 * 24;
}
if (t >= dstStart && t < dstEnd) {
return (true);
}
else {
return (false);
}
}
else if (zone == 4) { // Australia
datetime nonDstStart = StrToTime((string)TimeYear(t) + ".04.01 01:00");
while (TimeDayOfWeek(nonDstStart) != 0) { // first Sunday of April
nonDstStart += 3600 * 24;
}
datetime nonDstEnd = StrToTime((string)TimeYear(t) + ".10.01 01:00");
while (TimeDayOfWeek(nonDstEnd) != 0) { // first Sunday of October
nonDstEnd += 3600 * 24;
}
if (t >= nonDstStart && t < nonDstEnd) {
return (false);
}
else {
return (true);
}
}
return (false);
}
void setNews(__news_event &news[], int limit, int buff){
int size = ArraySize(news);
int i, k;
datetime d, sd;
int val;
if(Time[0] < news[0].day){
return;
}
else if(Time[limit] > news[size - 1].day){
return;
}
k = size - 1;
for(i = 0; i <= limit; i++){
d = Time[i];
val = EMPTY_VALUE;
while(k >= 0){
while(true){
if(k <= 0){
break;
}
sd = news[k - 1].day;
sd += GMTOffset * 3600;
sd += DSTOffset(sd, DSTZone);
if(sd < d){
break;
}
k--;
}
sd = news[k].day;
sd += GMTOffset * 3600;
sd += DSTOffset(sd, DSTZone);
if(sd >= d && sd < (d + PeriodSeconds())){
val = 1;
if (drawLine){
drawVLine(sd, news[k].event, buff);
}
}
break;
}
if(buff == 1){
buffer1[i] = val;
}
else if(buff == 2){
buffer2[i] = val;
}
}
}
// loadNews functions: