-
Notifications
You must be signed in to change notification settings - Fork 46
/
DataWriter_v4.02.mq4
544 lines (455 loc) · 25.4 KB
/
DataWriter_v4.02.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//+------------------------------------------------------------------+
//| DataWriter.mq4 |
//| Copyright 2018, Vladimir Zhbanko |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Vladimir Zhbanko"
#property link "https://vladdsm.github.io/myblog_attempt/"
#property version "4.02"
#property strict
#define EANAME "DataWriter_v4.02"
/*
PURPOSE: Retrieve price and Indicator data for an asset
USE: Data will be used for Decision Support System in R
HOW TO USE:
https://www.udemy.com/course/your-home-trading-environment/?referralCode=9EAD4CC112A476678658
https://www.udemy.com/course/your-trading-robot/?referralCode=529DCD0085D40BEC410C
*/
extern string Header1 = "-----EA Main Settings-------";
extern int UseBarsHistory = 14300;
extern int UseBarsCollect = 14200;
extern ENUM_TIMEFRAMES chartPeriod = 15; // Choose the timeframe to retrive the data
extern bool CollectClosePrice = True;
extern bool CollectOpenPrice = False;
extern bool CollectLowerPrice = False;
extern bool CollectHigherPrice = False;
extern bool CollectRSI = True;
extern bool CollectBullPower = False;
extern bool CollectBearPower = False;
extern bool CollectATR8 = False;
extern bool CollectMACD = True;
extern bool CollectStoch = False;
extern bool ShowScreenComments = True;
string FileNamePrx1 = "AI_CP";
string FileNamePrx2 = "AI_OP";
string FileNamePrx3 = "AI_LP";
string FileNamePrx4 = "AI_HP";
string FileNameRsi1 = "AI_RSI";
string FileNameBull = "AI_BullPow";
string FileNameBear = "AI_BearPow";
string FileNameAtr1 = "AI_Atr8";
string FileNameMacd = "AI_Macd";
string FileNameStoch = "AI_Stoch";
string commentText;
string Pairs[] = {"EURUSD","GBPUSD","AUDUSD","NZDUSD","USDCAD","USDCHF","USDJPY","EURGBP",
"EURJPY","EURCHF","EURNZD","EURCAD","EURAUD","GBPAUD","GBPCAD","GBPCHF",
"GBPJPY","GBPNZD","AUDCAD","AUDCHF","AUDJPY","AUDNZD","CADJPY","CHFJPY",
"NZDJPY","NZDCAD","NZDCHF","CADCHF"};
/*
Content:
1. Function writeDataCP collect Close Price data
2. Function writeDataOP collect Open Price data
3. Function writeDataLP collect Low Price data
4. Function writeDataHP collect High Price data
5. Function writeDataRSI collect Rsi data
6. Function writeDataBullPow collect BullPower data
7. Function writeDataBearPow collect BearPower data
8. Function writeDataAtr collect Atr data
9. Function writeDataMacd collect MACD data
10.Function writeDataStoch collect Stoch data
*/
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//useful to generate files when market is closed
if(CollectClosePrice)writeDataCP(FileNamePrx1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record open price data
if(CollectOpenPrice)writeDataOP(FileNamePrx2 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record low price data
if(CollectLowerPrice)writeDataHP(FileNamePrx3 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record high price data
if(CollectHigherPrice)writeDataLP(FileNamePrx4 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record rsi indicator data
if(CollectRSI)writeDataRSI(FileNameRsi1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record bull power data
if(CollectBullPower)writeDataBullPow(FileNameBull + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record bear power data
if(CollectBearPower)writeDataBearPow(FileNameBear + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record atr data
if(CollectATR8)writeDataAtr(FileNameAtr1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record macd data
if(CollectMACD)writeDataMacd(FileNameMacd + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//record Stochastic data
if(CollectStoch)writeDataStoch(FileNameStoch + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
//show dashboard
if (ShowScreenComments) Comment("\n Initial files should be written, they will be updated on every new bar ...");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// should generate unique time every minute https://www.mql5.com/en/forum/133366
static datetime Time0;
if(Time0 == Time[0])
{
}
else
{
//comment
commentText = StringConcatenate("\n",EANAME);
// record time to variable
Time0 = Time[0];
//code that only executed in the beginning and once every bar
//record close price data
if(CollectClosePrice)
{
writeDataCP(FileNamePrx1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNamePrx1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record open price data
if(CollectOpenPrice)
{
writeDataOP(FileNamePrx2 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNamePrx2 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record low price data
if(CollectLowerPrice)
{
writeDataHP(FileNamePrx3 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNamePrx3 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record high price data
if(CollectHigherPrice)
{
writeDataLP(FileNamePrx4 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNamePrx4 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record rsi indicator data
if(CollectRSI)
{
writeDataRSI(FileNameRsi1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNameRsi1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record bull power data
if(CollectBullPower)
{
writeDataBullPow(FileNameBull + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNameBull + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record bear power data
if(CollectBearPower)
{
writeDataBearPow(FileNameBear + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNameBear + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record atr data
if(CollectATR8)
{
writeDataAtr(FileNameAtr1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNameAtr1 + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record macd data
if(CollectMACD)
{
writeDataMacd(FileNameMacd + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNameMacd + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
//record Stochastic data
if(CollectStoch)
{
writeDataStoch(FileNameStoch + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv", chartPeriod, UseBarsCollect);
commentText = commentText + StringConcatenate("\n","Collecting...", FileNameStoch + string(chartPeriod) + "-" + string(UseBarsCollect) + ".csv");
}
if(ShowScreenComments) Comment(commentText);
}
}
//+------------------------------------------------------------------+
void writeDataCP(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs close price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iClose(Pairs[c],chartPeriod,j),5);
data = data + ","+pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataOP(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs open bar price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iOpen(Pairs[c],chartPeriod,j),5);
data = data + ","+pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataHP(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs lowest bar price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iHigh(Pairs[c],chartPeriod,j),5);
data = data + ","+pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataLP(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs lowest bar price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iLow(Pairs[c],chartPeriod,j),5);
data = data + ","+pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataRSI(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs close price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iRSI(Pairs[c],chartPeriod, 8, PRICE_MEDIAN, j),3);
data = data + ","+pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataBullPow(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs close price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iBullsPower(Pairs[c],chartPeriod, 8, PRICE_MEDIAN, j),5);
data = data + ","+pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataBearPow(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs close price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iBearsPower(Pairs[c],chartPeriod, 8, PRICE_MEDIAN, j),5);
data = data + ","+ pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataAtr(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs close price to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iATR(Pairs[c],chartPeriod, 8, j),5);
data = data + ","+ pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataMacd(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs MACD to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iMACD(Pairs[c],chartPeriod, 12, 26, 9, 0, 0, j),8);
data = data + ","+ pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}
void writeDataStoch(string filename, int charPer, int barsCollect)
// function to record 28 currencies pairs Stochastic indicator to the file (file to be used by all R scripts)
{
string data; //identifier that will be used to collect data string
datetime TIME; //Time index
// delete file if it's exist
FileDelete(filename);
// open file handle
int handle = FileOpen(filename,FILE_CSV|FILE_READ|FILE_WRITE);
FileSeek(handle,0,SEEK_SET);
// generate data now using for loop
//----Fill the arrays
//loop j calculates surfaces and angles from beginning of the day
for(int j = 0; j < barsCollect; j++) //j scrolls through bars of the day
{
TIME = iTime(Symbol(), chartPeriod, j); //Time of the bar of the applied chart symbol
data = string(TIME);
for(int c = 0; c < ArraySize(Pairs); c++)
{
string pairdata = DoubleToStr(iStochastic(Pairs[c],chartPeriod,34, 13, 8, 0, 0, 0, j),8);
data = data + "," + pairdata;
}
FileWrite(handle,data); //write data to the file during each for loop iteration
}
//
FileClose(handle); //close file when data write is over
//---------------------------------------------------------------------------------------------
}