-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
BufferFXT.mqh
292 lines (275 loc) · 12.1 KB
/
BufferFXT.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
289
290
291
292
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, EA31337 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/>.
*/
// Prevents processing this includes file for the second time.
#ifndef BUFFER_FXT_MQH
#define BUFFER_FXT_MQH
// Includes.
#include "Account/AccountMt.h"
#include "Chart.mqh"
#include "DictStruct.mqh"
#include "Object.mqh"
// Defines.
#define FXT_VERSION 405
// Profit calculation mode.
#define PROFIT_CALC_FOREX 0 // Default.
#define PROFIT_CALC_CFD 1
#define PROFIT_CALC_FUTURES 2
// Type of swap.
#define SWAP_BY_POINTS 0 // Default.
#define SWAP_BY_BASECURRENCY 1
#define SWAP_BY_INTEREST 2
#define SWAP_BY_MARGINCURRENCY 3
// Free margin calculation mode.
#define MARGIN_DONT_USE 0
#define MARGIN_USE_ALL 1 // Default.
#define MARGIN_USE_PROFIT 2
#define MARGIN_USE_LOSS 3
// Margin calculation mode.
#define MARGIN_CALC_FOREX 0 // Default.
#define MARGIN_CALC_CFD 1
#define MARGIN_CALC_FUTURES 2
#define MARGIN_CALC_CFDINDEX 3
// Check mode for Stop Out level (AccountStopoutMode).
#define MARGIN_TYPE_PERCENT 0
#define MARGIN_TYPE_CURRENCY 1
// Basic commission type.
#define COMM_TYPE_MONEY 0
#define COMM_TYPE_PIPS 1
#define COMM_TYPE_PERCENT 2
// Commission per lot or per deal.
#define COMMISSION_PER_LOT 0
#define COMMISSION_PER_DEAL 1
// Structs.
struct BufferFXTEntry {
datetime otm; // Bar datetime.
double open; // OHLCV values.
double high;
double low;
double close;
long volume;
int ctm; // The current time within a bar.
int flag; // Flag to launch an expert (0 - bar will be modified, but the expert will not be launched).
public:
bool operator==(const BufferFXTEntry &_s) {
// @fixme
return false;
}
string ToJSON() {
// @fixme
return "{}";
}
};
// FXT file header.
struct BufferFXTHeader {
int version; // Header version: 405
char copyright[64]; // Copyright/description.
char description[128]; // Account server name.
// 196
char symbol[12]; // Symbol pair.
int period; // Period of data aggregation in minutes (timeframe).
int model; // Model type: 0 - every tick, 1 - control points, 2 - bar open.
int bars; // Bars - number of modeled bars in history.
int fromdate; // Modelling start date - date of the first tick.
int todate; // Modelling end date - date of the last tick.
int totalTicks; // Total ticks. Add 4 bytes to align to the next double?
double modelquality; // Modeling quality (max. 99.9).
// 240
// Market symbol properties.
char currency[12]; // Base currency (12 bytes). Same as: StringLeft(symbol, 3)
int spread; // Spread in points. Same as: MarketInfo(MODE_SPREAD)
int digits; // Digits (default: 5). Same as: MarketInfo(MODE_DIGITS)
int padding1; // Padding space - add 4 bytes to align to the next double.
double point; // Point size (e.g. 0.00001). Same as: MarketInfo(MODE_POINT)
int lot_min; // Minimal lot size in centi lots (hundredths). Same as: MarketInfo(MODE_MINLOT)*100
int lot_max; // Maximal lot size in centi lots (hundredths). Same as: MarketInfo(MODE_MAXLOT)*100
int lot_step; // Lot step in centi lots (hundredths). Same as: MarketInfo(MODE_LOTSTEP)*100
int stops_level; // Stops level value (orders stop distance in points). Same as: MarketInfo(MODE_STOPLEVEL)
int gtc_pendings; // GTC (Good till cancel) - instruction to close pending orders at end of day (default: False).
int padding2; // Padding space - add 4 bytes to align to the next double.
// 296
// Profit calculation parameters.
double contract_size; // Contract size (e.g. 100000). Same as: MarketInfo(MODE_LOTSIZE)
double tick_value; // Tick value in quote currency (empty). Same as: MarketInfo(MODE_TICKVALUE)
double tick_size; // Size of one tick (empty). Same as: MarketInfo(MODE_TICKSIZE)
int profit_mode; // Profit calculation mode { PROFIT_CALC_FOREX=0, PROFIT_CALC_CFD=1, PROFIT_CALC_FUTURES=2 }. Same
// as: MarketInfo(MODE_PROFITCALCMODE)
// 324
// Swap calculation.
int swap_enable; // Enable swaps (default: True).
int swap_type; // Type of swap { SWAP_BY_POINTS=0, SWAP_BY_BASECURRENCY=1, SWAP_BY_INTEREST=2,
// SWAP_BY_MARGINCURRENCY=3 }. Same as: MarketInfo(MODE_SWAPTYPE)
int padding3; // Padding space - add 4 bytes to align to the next double.
double swap_long; // Swap of the buy order - long overnight swap value. Same as: MarketInfo(MODE_SWAPLONG)
double swap_short; // Swap of the sell order - short overnight swap value. Same as: MarketInfo(MODE_SWAPSHORT)
int swap_rollover3days; // Day of week to charge 3 days swap rollover. Default: WEDNESDAY (3). Same as:
// MarketInfo(SYMBOL_SWAP_ROLLOVER3DAYS)
// 356
// Margin calculation.
int leverage; // Account leverage (default: 100). Same as: AccountLeverage()
int free_margin_mode; // Free margin calculation mode { MARGIN_DONT_USE=0, MARGIN_USE_ALL=1, MARGIN_USE_PROFIT=2,
// MARGIN_USE_LOSS=3 }. Same as: AccountFreeMarginMode()
int margin_mode; // Margin calculation mode { MARGIN_CALC_FOREX=0, MARGIN_CALC_CFD=1, MARGIN_CALC_FUTURES=2,
// MARGIN_CALC_CFDINDEX=3 }. Same as: MarketInfo(MODE_MARGINCALCMODE)
int margin_stopout; // Margin Stop Out level (default: 30). Same as: AccountStopoutLevel()
int margin_stopout_mode; // Check mode for Stop Out level { MARGIN_TYPE_PERCENT=0, MARGIN_TYPE_CURRENCY=1 }. Same as:
// AccountStopoutMode()
double margin_initial; // Initial margin requirement (in units). Same as: MarketInfo(MODE_MARGININIT)
double margin_maintenance; // Maintenance margin requirement (in units). Same as: MarketInfo(MODE_MARGINMAINTENANCE)
double margin_hedged; // Hedged margin requirement for positions (in units). Same as: MarketInfo(MODE_MARGINHEDGED)
double margin_divider; // Margin divider used for leverage calculation.
char margin_currency[12]; // Margin currency. Same as: AccountCurrency().
int padding4; // Padding space - add 4 bytes to align to the next double.
// 424
// Commission calculation.
double comm_base; // Basic commission rate.
int comm_type; // Basic commission type { COMM_TYPE_MONEY=0, COMM_TYPE_PIPS=1, COMM_TYPE_PERCENT=2 }.
int comm_lots; // Commission per lot or per deal { COMMISSION_PER_LOT=0, COMMISSION_PER_DEAL=1 }
// 440
// For internal use.
int from_bar; // Index of the first bar at which modeling started (0 for the first bar).
int to_bar; // Index of the last bar at which modeling started (0 for the last bar).
int start_period_m1; // Bar index where modeling started using M1 bars (0 for the first bar).
int start_period_m5; // Bar index where modeling started using M5 bars (0 for the first bar).
int start_period_m15; // Bar index where modeling started using M15 bars (0 for the first bar).
int start_period_m30; // Bar index where modeling started using M30 bars (0 for the first bar).
int start_period_h1; // Bar index where modeling started using H1 bars (0 for the first bar).
int start_period_h4; // Bar index where modeling started using H4 bars (0 for the first bar).
int set_from; // Begin date from tester settings (must be zero).
int set_to; // End date from tester settings (must be zero).
// 480
//----
int freeze_level; // Order freeze level in points. Same as: MarketInfo(MODE_FREEZELEVEL)
int generating_errors; // Number of errors during model generation which needs to be fixed before testing.
// 488
//----
int reserved[60]; // Reserved - space for future use.
// Struct constructor.
BufferFXTHeader(Chart *_c, AccountMt *_a)
: version(405),
period(_c.Get<ENUM_TIMEFRAMES>(CHART_PARAM_TF)),
model(0),
bars(0),
fromdate(0),
todate(0),
totalTicks(0),
modelquality(0),
spread((int)_c.GetSpread()),
digits((int)_c.GetDigits()),
point(_c.GetPointSize()),
lot_min(int(_c.GetVolumeMin() * 100)),
lot_max(int(_c.GetVolumeMax() * 100)),
lot_step(int(_c.GetVolumeStep() * 100)),
stops_level(0), // @todo: Add MODE_STOPLEVEL to Account.
gtc_pendings(false),
contract_size(10000),
tick_value(_c.GetTickValue()),
tick_size(_c.GetTickSize()),
profit_mode(PROFIT_CALC_FOREX),
swap_enable(true),
swap_type(SWAP_BY_POINTS), // @todo: Add _c.GetSwapType() to SymbolInfo.
swap_long(_c.GetSwapLong()),
swap_short(_c.GetSwapShort()),
swap_rollover3days(3),
leverage((int)_a.GetLeverage()),
free_margin_mode(MARGIN_DONT_USE),
margin_mode(MARGIN_CALC_FOREX),
margin_stopout(30), // @fixme: _a.GetStopoutLevel() based on ACCOUNT_MARGIN_SO_CALL.
margin_stopout_mode(_a.GetStopoutMode()),
margin_initial(_c.GetMarginInit()),
margin_maintenance(_c.GetMarginMaintenance()),
margin_hedged(0),
margin_divider(0),
comm_base(0.0),
comm_type(COMM_TYPE_MONEY),
comm_lots(COMMISSION_PER_LOT),
from_bar(0),
to_bar(0),
start_period_m1(0),
start_period_m5(0),
start_period_m15(0),
start_period_m30(0),
start_period_h1(0),
start_period_h4(0),
set_from(0),
set_to(0),
freeze_level((int)_c.GetFreezeLevel()),
generating_errors(0) {
ArrayInitialize(copyright, 0);
// currency = StringSubstr(_m.GetSymbol(), 0, 3); // @fixme
ArrayInitialize(description, 0);
ArrayInitialize(margin_currency, 0);
ArrayInitialize(reserved, 0);
// symbol = _m.GetSymbol(); // @fixme
}
};
struct BufferFXTParams {
AccountMt *account;
Chart *chart;
// Struct constructor.
void BufferFXTParams(Chart *_chart = NULL, AccountMt *_account = NULL)
: account(Object::IsValid(_account) ? _account : new AccountMt),
chart(Object::IsValid(_chart) ? _chart : new Chart) {}
// Struct deconstructor.
void ~BufferFXTParams() {
delete account;
delete chart;
}
};
string ToJSON(BufferFXTEntry &_value, const bool, const unsigned int) { return _value.ToJSON(); };
/**
* Implements class to store tick data.
*/
class BufferFXT : public DictStruct<long, BufferFXTEntry> {
protected:
BufferFXTParams params;
public:
/**
* Class constructor.
*/
BufferFXT() {}
BufferFXT(const BufferFXTParams &_params) { params = _params; }
/**
* Class deconstructor.
*/
~BufferFXT() {}
/**
* Adds new entry.
*/
void Add(BufferFXTEntry &_value, long _dt = 0) {
_dt = _dt > 0 ? _dt : TimeCurrent();
Set(_dt, _value);
}
/**
* Adds new entry.
*/
void Add(MqlTick &_value) {
// @todo: Parse MqlTick.
// Set(_dt, _value);
}
/**
* Save data into file.
*/
void SaveToFile() {
BufferFXTHeader header(params.chart, params.account);
// @todo: Save BufferFXTHeader, then foreach BufferFXTEntry.
// @see: https://docs.mql4.com/files/filewritestruct
}
};
#endif // BUFFER_FXT_MQH