forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.mqh
288 lines (227 loc) · 5.32 KB
/
Session.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
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
//+------------------------------------------------------------------+
//| EA31337 - multi-strategy advanced trading robot. |
//| Copyright 2016-2017, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Class to provide storing mechanism for session variables.
*/
class Session {
public:
struct data
{
string key;
string val;
};
int handle;
string filename;
data array[], copyArray[];
public:
void Registry (string path = "") {
if (path != "") {
handle = FileOpen(path, FILE_READ|FILE_CSV|FILE_ANSI, "=");
if (handle != INVALID_HANDLE)
{
int count = 0;
while(FileIsEnding(handle)==false)
{
ArrayResize(array,(count+1),100000);
array[count].key = FileReadString(handle);
array[count].val = FileReadString(handle);
count++;
}
}
FileClose(handle);
filename = path;
}
}
bool Save (string path = "") {
if (path == "")
{
path = filename;
}
handle = FileOpen(path, FILE_WRITE|FILE_CSV, "=");
if(handle != INVALID_HANDLE)
{
int size = ArraySize(array);
if(size > 0)
{
for (int i = 0; i < size; i++)
{
FileWrite(handle, array[i].key, array[i].val);
}
}
FileClose(handle);
return true;
} else {
FileClose(handle);
return false;
}
}
string GetKeys (bool withValues = 0) {
int size = ArraySize(array);
string keys = "Empty";
if(size > 0)
{
keys = "";
for (int i = 0; i < size; i++)
{
keys += array[i].key;
if (withValues == 1) {
keys += "=" + array[i].val;
}
keys += ";";
}
}
return keys;
}
bool Delete (string key) {
int size = ArraySize(array);
if(size > 0)
{
int offset = 0;
for (int i = 0; i < size; i++)
{
if (array[i].key == key)
{
Erase(array, i);
return true;
break;
}
}
}
return false;
}
template <typename T>
void Erase(T& A[], int iPos){
int iLast = ArraySize(A) - 1;
A[iPos].key = A[iLast].key;
A[iPos].val = A[iLast].val;
ArrayResize(A, iLast);
}
string GetValueString (string key) {
int size = ArraySize(array);
if(size > 0)
{
for (int i = 0; i < size; i++)
{
if (array[i].key == key)
{
return(array[i].val);
break;
}
}
}
return(NULL);
}
int GetValueInteger (string key) {
string value = GetValueString(key);
if(value != NULL) {
#ifdef MQL4
return(StrToInteger(value));
#else
return((int) StringToInteger(value));
#endif
} else {
return(NULL);
}
}
double GetValueDouble (string key) {
string value = GetValueString(key);
if(value != NULL) {
#ifdef MQL4
return(StrToDouble(value));
#else
return(StringToDouble(value));
#endif
} else {
return(NULL);
}
}
bool SetValue (string key, string value) {
int size = ArraySize(array);
int i = 0;
if(size > 0)
{
for (;i < size; i++)
{
if (array[i].key == key)
{
array[i].val = value;
return true;
break;
}
}
}
ArrayResize(array, (size+1), 100000);
array[i].key = key;
array[i].val = value;
return true;
}
bool SetValue (string key, double value) {
int size = ArraySize(array);
int i = 0;
if(size > 0)
{
for (;i < size; i++)
{
if (array[i].key == key)
{
array[i].val = DoubleToString(value);
return true;
break;
}
}
}
ArrayResize(array, (size+1), 100000);
array[i].key = key;
array[i].val = DoubleToString(value);
return true;
}
bool SetValue (string key, int value) {
int size = ArraySize(array);
int i = 0;
if(size > 0)
{
for (;i < size; i++)
{
if (array[i].key == key)
{
array[i].val = IntegerToString(value);
return true;
break;
}
}
}
ArrayResize(array, (size+1), 100000);
array[i].key = key;
array[i].val = IntegerToString(value);
return true;
}
/**
* Check whether we're trading within market peak hours.
*/
bool IsPeakHour() {
int hour;
#ifdef __MQL5__
MqlDateTime dt;
TimeCurrent(dt);
hour = dt.hour;
#else
hour = Hour();
#endif
return hour >= 8 && hour <= 16;
}
};