forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Market.mqh
312 lines (284 loc) · 10.2 KB
/
Market.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//+------------------------------------------------------------------+
//| 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 MARKET_MQH
#define MARKET_MQH
// Forward declaration.
class Market;
class SymbolInfo;
// Includes.
#include "Market.struct.h"
#include "Math.h"
#include "Order.mqh"
#include "Serializer.mqh"
#include "SymbolInfo.mqh"
#include "Task/TaskCondition.enum.h"
// Structs.
// Market info.
struct MarketData {
int empty;
// Serializers.
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {}
SerializerNodeType Serialize(Serializer &_s) { return SerializerNodeObject; }
};
/**
* Class to provide market information.
*/
class Market : public SymbolInfo {
protected:
// Struct variables.
MarketData minfo;
public:
/**
* Implements class constructor with a parameter.
*/
Market(string _symbol = NULL, Log *_log = NULL) : SymbolInfo(_symbol) {}
/**
* Class deconstructor.
*/
~Market() {}
/* Getters */
/* Functional methods */
/**
* Refresh data in pre-defined variables and series arrays.
*
* @see http://docs.mql4.com/series/refreshrates
*/
static bool RefreshRates() {
// In MQL5 returns true for backward compatibility.
#ifdef __MQL4__
return ::RefreshRates();
#else
return true;
#endif
}
/**
* Returns market data about securities.
*
* @docs
* - https://docs.mql4.com/constants/environment_state/marketinfoconstants
*/
static double MarketInfo(string _symbol, int _type) {
#ifdef __MQL4__
return ::MarketInfo(_symbol, _type);
#else
switch (_type) {
case MODE_LOW:
// Low day price.
return SymbolInfoStatic::SymbolInfoDouble(_symbol, SYMBOL_LASTLOW);
case MODE_HIGH:
// High day price.
return SymbolInfoStatic::SymbolInfoDouble(_symbol, SYMBOL_LASTHIGH);
case MODE_TIME:
// Time of the last quote.
return (double)SymbolInfoStatic::GetQuoteTime(_symbol);
case MODE_BID:
// Last incoming bid price.
return SymbolInfoStatic::GetBid(_symbol);
case MODE_ASK:
// Last incoming ask price.
return SymbolInfoStatic::GetAsk(_symbol);
case MODE_POINT:
// Point size in the quote currency.
return SymbolInfoStatic::GetPointSize(_symbol);
case MODE_DIGITS:
// Symbol digits after decimal point.
return SymbolInfoStatic::GetDigits(_symbol);
case MODE_SPREAD:
// Spread value in points.
return SymbolInfoStatic::GetSpreadInPts(_symbol);
case MODE_STOPLEVEL:
// Stop level in points.
return (double)SymbolInfoStatic::GetTradeStopsLevel(_symbol);
case MODE_LOTSIZE:
// Lot size in the base currency.
return SymbolInfoStatic::GetTradeContractSize(_symbol);
case MODE_TICKVALUE:
// Tick value in the deposit currency.
return SymbolInfoStatic::GetTickValue(_symbol);
case MODE_TICKSIZE:
// Tick size in points.
return SymbolInfoStatic::GetTickSize(_symbol);
case MODE_SWAPLONG:
// Swap of the buy order.
return SymbolInfoStatic::GetSwapLong(_symbol);
case MODE_SWAPSHORT:
// Swap of the sell order.
return SymbolInfoStatic::GetSwapShort(_symbol);
case MODE_LOTSTEP:
// Step for changing lots.
return SymbolInfoStatic::GetVolumeStep(_symbol);
case MODE_MINLOT:
// Minimum permitted amount of a lot.
return SymbolInfoStatic::GetVolumeMin(_symbol);
case MODE_MAXLOT:
// Maximum permitted amount of a lot.
return SymbolInfoStatic::GetVolumeMax(_symbol);
case MODE_SWAPTYPE:
// Swap calculation method.
return (double)SymbolInfoStatic::GetSwapMode(_symbol);
case MODE_PROFITCALCMODE:
// Profit calculation mode.
return (double)SymbolInfoStatic::SymbolInfoInteger(_symbol, SYMBOL_TRADE_CALC_MODE);
case MODE_STARTING:
// @todo: Market starting date.
return (0);
case MODE_EXPIRATION:
// @todo: Market expiration date.
return (0);
case MODE_TRADEALLOWED:
// Trade is allowed for the symbol.
return Terminal::IsTradeAllowed();
case MODE_MARGINCALCMODE:
// @todo: Margin calculation mode.
return (0);
case MODE_MARGININIT:
// Initial margin requirements for 1 lot.
return SymbolInfoStatic::GetMarginInit(_symbol);
case MODE_MARGINMAINTENANCE:
// Margin to maintain open orders calculated for 1 lot.
return SymbolInfoStatic::GetMarginMaintenance(_symbol);
case MODE_MARGINHEDGED:
// @todo: Hedged margin calculated for 1 lot.
return (0);
case MODE_MARGINREQUIRED:
// @todo: Free margin required to open 1 lot for buying.
return (0);
case MODE_FREEZELEVEL:
// Order freeze level in points.
return SymbolInfoStatic::GetFreezeLevel(_symbol);
}
#endif
return (-1);
}
double MarketInfo(int _type) { return MarketInfo(symbol, _type); }
/**
* Get delta value per lot in account currency of a point of symbol.
*
* @see
* - https://www.mql5.com/en/forum/127584
* - https://www.mql5.com/en/forum/133792/page3#512466
* - https://www.mql5.com/en/forum/135345#515262
*/
static double GetDeltaValue(string _symbol) {
// Return tick value in the deposit currency divided by tick size in points.
return SymbolInfoStatic::GetTickValue(_symbol) / SymbolInfoStatic::GetTickSize(_symbol);
}
double GetDeltaValue() { return GetDeltaValue(symbol); }
/**
* Returns the last price change in pips.
*/
double GetLastPriceChangeInPips() {
return fmax(fabs(GetLastAsk() - GetAsk()), fabs(GetLastBid() - GetBid())) * pow(10, GetPipDigits());
}
/* END: Getters */
/* Normalization methods */
/**
* Normalize price value.
*
* Make sure that the price is a multiple of ticksize.
*/
static double NormalizePrice(string _symbol, double p) {
// See:
// - https://www.mql5.com/en/forum/135345 (for non-currency DE30)
// - https://www.mql5.com/en/forum/139338
// - MarketInfo(chart.symbol,MODE_TICKSIZE) returns 0.5
// - MarketInfo(chart.symbol,MODE_DIGITS) return 1
// - Point = 0.1
// Rare fix when a change in tick size leads to a change in tick value.
double _result = round(p / SymbolInfoStatic::GetPointSize(_symbol)) * SymbolInfoStatic::GetTickSize(_symbol);
_result = NormalizeDouble(_result, SymbolInfoStatic::GetDigits(_symbol));
return _result;
}
double NormalizePrice(double p) { return NormalizePrice(symbol, p); }
/* Market state checking */
/**
* Check whether given symbol exists.
*/
static bool SymbolExists(string _symbol = NULL) {
ResetLastError();
SymbolInfoStatic::GetAsk(_symbol);
return GetLastError() != ERR_MARKET_UNKNOWN_SYMBOL;
}
/* Printer methods */
/**
* Returns Market data in textual representation.
*/
string const ToString() {
return StringFormat(string("Pip digits/value: %d/%g, Spread: %d pts (%g pips; %.4f%%), Pts/pip: %d, ") +
"Volume digits: %d, " + "Delta: %g, Last change: %g pips",
GetPipDigits(), GetPipValue(), GetSpreadInPts(), GetSpreadInPips(), GetSpreadInPct(),
GetPointsPerPip(), GetVolumeDigits(), GetDeltaValue(), GetLastPriceChangeInPips());
}
/**
* Returns Market data in CSV format.
*/
string ToCSV(bool _header = false) {
return !_header ? StringFormat(string("%d,%g,%d,%g,%.4f,%d,") + "%g,%d,%.1f,%d," + "%g,%g", GetPipDigits(),
GetPipValue(), GetSpreadInPts(), GetSpreadInPips(), GetSpreadInPct(),
GetPointsPerPip(), GetVolumeDigits(), GetDeltaValue(), GetLastPriceChangeInPips())
: string("Pip Digits,Pip Value,Spread,Pts/pip,") + "Volume digits," + "Delta,Last change (pips)";
}
/* Conditions */
/**
* Checks for market condition.
*
* @param ENUM_MARKET_CONDITION _cond
* Market condition.
* @param MqlParam[] _args
* Condition arguments.
* @return
* Returns true when the condition is met.
*/
bool CheckCondition(ENUM_MARKET_CONDITION _cond, ARRAY_REF(DataParamEntry, _args)) {
switch (_cond) {
case MARKET_COND_IN_PEAK_HOURS:
return DateTimeStatic::Hour() >= 8 && DateTimeStatic::Hour() <= 16;
case MARKET_COND_SPREAD_LE_10:
return GetSpreadInPts() <= 10;
case MARKET_COND_SPREAD_GT_10:
return GetSpreadInPts() > 10;
case MARKET_COND_SPREAD_GT_20:
return GetSpreadInPts() > 20;
default:
GetLogger().Error(StringFormat("Invalid market condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
return false;
}
}
bool CheckCondition(ENUM_MARKET_CONDITION _cond) {
ARRAY(DataParamEntry, _args);
return Market::CheckCondition(_cond, _args);
}
/* Serializers */
/**
* Returns serialized representation of the object instance.
*/
SerializerNodeType Serialize(Serializer &_s) {
_s.PassStruct(THIS_REF, "market-data", minfo);
// _s.PassStruct(THIS_REF, "symbol-info", (SymbolInfo *)this);
return SerializerNodeObject;
}
};
#ifndef __MQL4__
// Defines macros (for MQL4 backward compatibility).
double MarketInfo(string _symbol, int _type) { return Market::MarketInfo(_symbol, _type); }
#endif
#endif // MARKET_MQH