forked from vzhomeexperiments/Include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_ReadDataFromDSS.mqh
207 lines (160 loc) · 8.35 KB
/
12_ReadDataFromDSS.mqh
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
//+-------------------------------------------------------------------+
//| 12_ReadDataFromDSS.mqh |
//| Copyright 2020, Vladimir Zhbanko |
//+-------------------------------------------------------------------+
#property copyright "Copyright 2021, Vladimir Zhbanko"
#property link "https://vladdsm.github.io/myblog_attempt/"
#property version "1.001"
#property strict
// function to recieve information from csv files
// version 1.001 date 05.09.2020
//
//+---------------------------------------------------------------------------------+//
//Universal function to read different numeric files from the Decision Support System//
//+---------------------------------------------------------------------------------+//
/*
User guide:
1. Add global variable to EA: e.g.: double AIPriceTriggerPredictionH1;
2. Add function call inside start function to EA: e.g.: AIPriceTriggerPredictionH1 = ReadPriceChangeTriggerFromAI(predictor_periodH1);
3. Adapt Trading Robot conditions to change trading strategy parameters eg.: see Falcon_C
4. Add include call to this file to EAe.g.: #include <12_ReadDataFromDSS.mqh>
Parameters:
@param symbol string, specifying the symbol of the asset
@param chart_period int, specifying the chart period
@param mode string, specifying the file type that needs to be read
@options
mode = "read_change" read predicted price change
mode = "read_trigger" read optimal trigger value
mode = "read_timehold" read optimal time hold in hour bars
mode = "read_maxperf" read achieved model performance value
mode = "read_mt" read value of market type prediction
mode = "read_mt_conf" read confidence value of market type prediction
mode = "read_quantile" read first quantile from all model performances
*/
#define MARKET_NONE 0 //Market not siutable for trading e.g. macroeconomic event or not properly defined
#define MARKET_BUN 1 //Market with bullish character
#define MARKET_BUV 2 //Market with volatile bullish character
#define MARKET_BEN 3 //Market with bearish character
#define MARKET_BEV 4 //Market with volatile bearish character
#define MARKET_RAN 5 //Market with Ranging character
#define MARKET_RAV 6 //Market with volatile Ranging character
double ReadDataFromDSS(string symbol, int chart_period, string mode)
{
/*
- Function reads the file eg: AI_M15_ChangeAUDCAD.csv
- It will output predicted value of asset change
*/
//define internal variables needed
double output = 0; //Variable to store and return predicted price change written in the file
string res = "0"; //Variable to return result of the function
int handle;
string str;
string sep=","; // A separator as a character
ushort u_sep; // The code of the separator character
string f_name; // File name prefix
string result[]; // An array to get string elements
string full_line; // String reserved for a file string
if(mode == "read_change")
{
f_name = "AI_M";
handle=FileOpen(f_name+IntegerToString(chart_period)+"_Change"+symbol+".csv",FILE_READ|FILE_SHARE_READ|FILE_CSV,"@");
if(handle==-1){Comment("Error - file does not exist"); str = "-1"; }
if(FileSize(handle)==0){FileClose(handle); Comment("Error - File AI_M xx is empty"); }
//this will bring the last element
while(!FileIsEnding(handle)) { str=FileReadString(handle); }
FileClose(handle);
//Interpret the file
if(str == "-1"){output = StringToDouble(str); return(output); } //in anomalous case function will return error '-1'
else
{
output = StringToDouble(str);
}
return(output);
//tested pass: OK
}
if(mode == "read_trigger" || mode == "read_timehold" || mode == "read_maxperf" || mode == "read_quantile")
{
f_name = "StrTest-";
handle=FileOpen(f_name+symbol+"M"+IntegerToString(chart_period)+".csv",FILE_READ|FILE_SHARE_READ|FILE_CSV,"@");
if(handle==-1){Comment("Error - file does not exist"); str = "-1"; }
if(FileSize(handle)==0){FileClose(handle); Comment("Error - File StrTest xx is empty"); }
// analyse the content of each string line by line
while(!FileIsEnding(handle))
{
str=FileReadString(handle); //storing content of the current line
//full current line
full_line = StringSubstr(str,0);
//--- Get the separator code
u_sep=StringGetCharacter(sep,0);
//--- Split the string to substrings and store to the array result[]
int k = StringSplit(str,u_sep,result);
// extract content of the string array [for better clarify]
}
FileClose(handle);
//Interpret the file
if(mode == "read_trigger") // test passed? OK ;
{
if(result[0] == "-1"){output = StringToDouble(str); return(output); } //in anomalous case function will return error '-1'
else output = StringToDouble(result[0]);
}
else if(mode == "read_timehold") // test passed? OK ;
{
if(result[1] == "-1"){output = StringToDouble(str); return(output); } //in anomalous case function will return error '-1'
else output = StringToDouble(result[1]);
}
else if(mode == "read_maxperf") // test passed? OK ;
{
if(result[3] == "-1"){output = StringToDouble(str); return(output); } //in anomalous case function will return error '-1'
else output = StringToDouble(result[3]);
}
else if(mode == "read_quantile") // test passed? OK ;
{
if(result[4] == "-1"){output = StringToDouble(str); return(output); } //in anomalous case function will return error '-1'
else output = StringToDouble(result[4]);
}
}
if(mode == "read_mt_conf" || mode == "read_mt")
{
f_name = "AI_MarketType_";
handle=FileOpen(f_name+symbol+IntegerToString(chart_period)+".csv",FILE_READ|FILE_SHARE_READ|FILE_CSV,"@");
if(handle==-1){Comment("Error - file does not exist"); str = "-1"; }
if(FileSize(handle)==0){
FileClose(handle); Comment("Error - File AI_MarketType_ xx is empty");
Sleep(50);
handle=FileOpen(f_name+symbol+IntegerToString(chart_period)+".csv",FILE_READ|FILE_SHARE_READ|FILE_CSV,"@");
if(FileSize(handle)==0)
{
FileClose(handle);
Comment("Tried 2 times but File AI_MarketType_ xx is empty");
}
}
// analyse the content of each string line by line
while(!FileIsEnding(handle))
{
str=FileReadString(handle); //storing content of the current line
//full current line
full_line = StringSubstr(str,0);
//--- Get the separator code
u_sep=StringGetCharacter(sep,0);
//--- Split the string to substrings and store to the array result[]
int k = StringSplit(str,u_sep,result);
// extract content of the string array [for better clarify]
}
FileClose(handle);
if(result[0] == "-1"){output = StringToDouble(str); return(output); } //in anomalous case function will return error '-1'
else if(mode == "read_mt_conf") {output = StringToDouble(result[1]);}
else if(mode == "read_mt")
{
//Assign market variable based on result
if(result[0] == "0" || result[0] == "-1") {output = MARKET_NONE;}
if(result[0] == "1" || result[0] == "BUN"){output = MARKET_BUN; }
if(result[0] == "2" || result[0] == "BUV"){output = MARKET_BUV; }
if(result[0] == "3" || result[0] == "BEN"){output = MARKET_BEN; }
if(result[0] == "4" || result[0] == "BEV"){output = MARKET_BEV; }
if(result[0] == "5" || result[0] == "RAN"){output = MARKET_RAN; }
if(result[0] == "6" || result[0] == "RAV"){output = MARKET_RAV; }
}
//tested pass: ok
}
return(output);
}