forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarket.mqh
621 lines (573 loc) · 22 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 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/>.
*/
// Prevents processing this includes file for the second time.
#ifndef MARKET_MQH
#define MARKET_MQH
// Forward declaration.
class Market;
class SymbolInfo;
// Includes.
#include "Math.mqh"
#include "Order.mqh"
#include "SymbolInfo.mqh"
// Structs.
// Struct for making a snapshot of market values.
struct MarketSnapshot {
datetime dt;
double bid, ask;
double vol;
};
// Market info.
struct MarketData {
double pip_value; // Pip value.
unsigned int pip_digits; // Pip digits (precision).
unsigned int pts_per_pip; // Points per pip.
unsigned int vol_digits; // Volume digits.
};
// Enums.
// Market conditions.
enum ENUM_MARKET_CONDITION {
MARKET_COND_IN_PEAK_HOURS = 1, // Market in peak hours (8-16)
MARKET_COND_SPREAD_LE_10 = 2, // Spread <= 10pts
MARKET_COND_SPREAD_GT_10 = 3, // Spread > 10pts
MARKET_COND_SPREAD_GT_20 = 4, // Spread > 20pts
FINAL_ENUM_MARKET_CONDITION_ENTRY = 5
};
/**
* Class to provide market information.
*/
class Market : public SymbolInfo {
protected:
// Struct variables.
MarketData minfo;
MarketSnapshot snapshots[];
public:
/**
* Implements class constructor with a parameter.
*/
Market(string _symbol = NULL, Log *_log = NULL) :
SymbolInfo(_symbol, Object::IsValid(_log) ? _log : new Log)
{
// @todo: Test symbol with SymbolExists(_symbol)
minfo.pip_digits = GetPipDigits(_symbol);
minfo.pip_value = GetPipValue(_symbol);
minfo.pts_per_pip = GetPointsPerPip(_symbol);
minfo.vol_digits = GetVolumeDigits(_symbol);
}
/**
* Class deconstructor.
*/
~Market() {
}
/* Getters */
/**
* Get pip precision.
*/
static unsigned int GetPipDigits(string _symbol) {
return GetDigits(_symbol) < 4 ? 2 : 4;
}
unsigned int GetPipDigits() {
return minfo.pip_digits;
}
/**
* Get pip value.
*/
static double GetPipValue(string _symbol) {
unsigned int _pdigits = GetPipDigits(_symbol);
return 10 >> _pdigits;
}
double GetPipValue() {
return minfo.pip_value;
}
/**
* Get current spread in points.
*
* @param
* symbol string (optional)
* Currency pair symbol.
*
* @return
* Return symbol trade spread level in points.
*/
static unsigned int GetSpreadInPts(string _symbol) {
return GetSpread(_symbol);
}
unsigned int GetSpreadInPts() {
return GetSpread();
}
/**
* Get current spread in float.
*/
double GetSpreadInPips() {
return (GetAsk() - GetBid()) * pow(10, GetPipDigits());
}
/**
* Get current spread in percent.
*/
static double GetSpreadInPct(string _symbol) {
return 100.0 * (GetAsk(_symbol) - GetBid(_symbol)) / GetAsk(_symbol);
}
double GetSpreadInPct() {
return GetSpreadInPct(symbol);
}
/**
* Get number of points per pip.
*
* To be used to replace Point for trade parameters calculations.
* See: http://forum.mql4.com/30672
*/
static unsigned int GetPointsPerPip(string _symbol) {
return (unsigned int) pow(10, GetDigits(_symbol) - GetPipDigits(_symbol));
}
unsigned int GetPointsPerPip() {
return minfo.pts_per_pip;
}
/**
* Get a market distance in points.
*
* Minimal permissible distance value in points for StopLoss/TakeProfit.
*
* This is due that at placing of a pending order, the open price cannot be too close to the market.
* The minimal distance of the pending price from the current market one in points can be obtained
* using the MarketInfo() function with the MODE_STOPLEVEL parameter.
* Related error messages:
* Error 130 (ERR_INVALID_STOPS) happens In case of false open price of a pending order.
* Error 145 (ERR_TRADE_MODIFY_DENIED) happens when modification of order was too close to market.
*
* @see: https://book.mql4.com/appendix/limits
*/
static long GetTradeDistanceInPts(string _symbol) {
return fmax(GetTradeStopsLevel(_symbol), GetFreezeLevel(_symbol));
}
long GetTradeDistanceInPts() {
return GetTradeDistanceInPts(symbol);
}
/**
* Get a market distance in pips.
*
* Minimal permissible distance value in pips for StopLoss/TakeProfit.
*
* @see: https://book.mql4.com/appendix/limits
*/
static double GetTradeDistanceInPips(string _symbol) {
return (double) (GetTradeDistanceInPts(_symbol) / GetPointsPerPip(_symbol));
}
double GetTradeDistanceInPips() {
return GetTradeDistanceInPips(symbol);
}
/**
* Get a market gap in value.
*
* Minimal permissible distance value in value for StopLoss/TakeProfit.
*
* @see: https://book.mql4.com/appendix/limits
*/
static double GetTradeDistanceInValue(string _symbol) {
return GetTradeDistanceInPts(_symbol) * GetPointSize(_symbol);
}
double GetTradeDistanceInValue() {
return GetTradeDistanceInValue(symbol);
}
/**
* Get a volume precision.
*/
static unsigned int GetVolumeDigits(string _symbol) {
return (unsigned int)
-log10(
fmin(
GetVolumeStep(_symbol),
GetVolumeMin(_symbol)
)
);
}
unsigned int GetVolumeDigits() {
return minfo.vol_digits;
}
/* 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 compability.
#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) {
switch(_type) {
case MODE_LOW: return SymbolInfo::SymbolInfoDouble(_symbol, SYMBOL_LASTLOW); // Low day price.
case MODE_HIGH: return SymbolInfo::SymbolInfoDouble(_symbol, SYMBOL_LASTHIGH); // High day price.
case MODE_TIME: return (double) GetQuoteTime(_symbol); // Time of the last quote.
case MODE_BID: return GetBid(_symbol); // Last incoming bid price.
case MODE_ASK: return GetAsk(_symbol); // Last incoming ask price.
case MODE_POINT: return GetPointSize(_symbol); // Point size in the quote currency.
case MODE_DIGITS: return GetDigits(_symbol); // Symbol digits after decimal point.
case MODE_SPREAD: return GetSpreadInPts(_symbol); // Spread value in points.
case MODE_STOPLEVEL: return (double) GetTradeStopsLevel(_symbol); // Stop level in points.
case MODE_LOTSIZE: return GetTradeContractSize(_symbol); // Lot size in the base currency.
case MODE_TICKVALUE: return GetTickValue(_symbol); // Tick value in the deposit currency.
case MODE_TICKSIZE: return GetTickSize(_symbol); // Tick size in points.
case MODE_SWAPLONG: return GetSwapLong(_symbol); // Swap of the buy order.
case MODE_SWAPSHORT: return GetSwapShort(_symbol); // Swap of the sell order.
case MODE_LOTSTEP: return GetVolumeStep(_symbol); // Step for changing lots.
case MODE_MINLOT: return GetVolumeMin(_symbol); // Minimum permitted amount of a lot.
case MODE_MAXLOT: return GetVolumeMax(_symbol); // Maximum permitted amount of a lot.
case MODE_SWAPTYPE: return (double) GetSwapMode(_symbol); // Swap calculation method.
case MODE_PROFITCALCMODE: return (double) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_TRADE_CALC_MODE); // Profit calculation mode.
case MODE_STARTING: return (0); // @todo: Market starting date.
case MODE_EXPIRATION: return (0); // @todo: Market expiration date.
case MODE_TRADEALLOWED: return Terminal::IsTradeAllowed(); // Trade is allowed for the symbol.
case MODE_MARGINCALCMODE: return (0); // @todo: Margin calculation mode.
case MODE_MARGININIT: return GetMarginInit(_symbol); // Initial margin requirements for 1 lot.
case MODE_MARGINMAINTENANCE: return GetMarginMaintenance(_symbol); // Margin to maintain open orders calculated for 1 lot.
case MODE_MARGINHEDGED: return (0); // @todo: Hedged margin calculated for 1 lot.
case MODE_MARGINREQUIRED: return (0); // @todo: Free margin required to open 1 lot for buying.
case MODE_FREEZELEVEL: return GetFreezeLevel(_symbol); // Order freeze level in points.
}
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://forum.mql4.com/33975
* - https://forum.mql4.com/43064#515262
* - https://forum.mql4.com/41259/page3#512466
* - https://www.mql5.com/en/forum/127584
* - https://www.mql5.com/en/forum/135345
* - https://www.mql5.com/en/forum/133792/page3#512466
*/
static double GetDeltaValue(string _symbol) {
// Return tick value in the deposit currency divided by tick size in points.
return GetTickValue(_symbol) / 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: http://forum.mql4.com/47988
// http://forum.mql4.com/43064#515262 zzuegg reports for non-currency DE30:
// - 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.
return round(p / GetPointSize(_symbol)) * GetTickSize(_symbol);
}
double NormalizePrice(double p) {
return NormalizePrice(symbol, p);
}
/**
* Normalize lot size.
*/
double NormalizeLots(double _lots, bool _ceil = false) {
double _lot_size = _lots;
double _vol_step = GetVolumeStep() > 0.0 ? GetVolumeStep() : GetVolumeMin();
if (_vol_step > 0) {
// Related: http://forum.mql4.com/47988
double _precision = 1 / _vol_step;
// Edge case when step is higher than minimum.
_lot_size = _ceil ? ceil(_lots * _precision) / _precision : floor(_lots * _precision) / _precision;
double _min_lot = fmax(GetVolumeMin(), GetVolumeStep());
_lot_size = fmin(fmax(_lot_size, _min_lot), GetVolumeMax());
}
return _lot_size;
}
/**
* Normalize SL/TP values.
*/
double NormalizeSLTP(double _value, ENUM_ORDER_TYPE _cmd, ENUM_ORDER_TYPE_VALUE _mode) {
switch (_cmd) {
// Buying is done at the Ask price.
// The TakeProfit and StopLoss levels must be at the distance
// of at least SYMBOL_TRADE_STOPS_LEVEL points from the Bid price.
case ORDER_TYPE_BUY:
switch (_mode) {
// Bid - StopLoss >= SYMBOL_TRADE_STOPS_LEVEL (minimum trade distance)
case ORDER_TYPE_SL: return fmin(_value, GetBid() - GetTradeDistanceInValue());
// TakeProfit - Bid >= SYMBOL_TRADE_STOPS_LEVEL (minimum trade distance)
case ORDER_TYPE_TP: return fmax(_value, GetBid() + GetTradeDistanceInValue());
default: Logger().Error(StringFormat("Invalid mode: %s!", EnumToString(_mode), __FUNCTION__));
}
// Selling is done at the Bid price.
// The TakeProfit and StopLoss levels must be at the distance
// of at least SYMBOL_TRADE_STOPS_LEVEL points from the Ask price.
case ORDER_TYPE_SELL:
switch (_mode) {
// StopLoss - Ask >= SYMBOL_TRADE_STOPS_LEVEL (minimum trade distance)
case ORDER_TYPE_SL: return fmax(_value, GetAsk() + GetTradeDistanceInValue());
// Ask - TakeProfit >= SYMBOL_TRADE_STOPS_LEVEL (minimum trade distance)
case ORDER_TYPE_TP: return fmin(_value, GetAsk() - GetTradeDistanceInValue());
default: Logger().Error(StringFormat("Invalid mode: %s!", EnumToString(_mode), __FUNCTION__));
}
default: Logger().Error(StringFormat("Invalid order type: %s!", EnumToString(_cmd), __FUNCTION__));
}
return NULL;
}
double NormalizeSL(double _value, ENUM_ORDER_TYPE _cmd) {
return NormalizeSLTP(_value, _cmd, ORDER_TYPE_SL);
}
double NormalizeTP(double _value, ENUM_ORDER_TYPE _cmd) {
return NormalizeSLTP(_value, _cmd, ORDER_TYPE_TP);
}
/* Market state checking */
/**
* Check whether given symbol exists.
*/
static bool SymbolExists(string _symbol = NULL) {
ResetLastError();
GetAsk(_symbol);
return GetLastError() != ERR_MARKET_UNKNOWN_SYMBOL;
}
/* Printer methods */
/**
* Returns Market data in textual representation.
*/
string ToString() {
return StringFormat(
"Pip digits/value: %d/%g, Spread: %d pts (%g pips; %.4f%%), Pts/pip: %d, " +
"Trade distance: %g (%d pts; %.1f pips), Volume digits: %d, " +
"Delta: %g, Last change: %g pips",
GetPipDigits(), GetPipValue(), GetSpreadInPts(), GetSpreadInPips(), GetSpreadInPct(), GetPointsPerPip(),
GetTradeDistanceInValue(), GetTradeDistanceInPts(), GetTradeDistanceInPips(), GetVolumeDigits(),
GetDeltaValue(), GetLastPriceChangeInPips()
);
}
/**
* Returns Market data in CSV format.
*/
string ToCSV(bool _header = false) {
return
! _header ? StringFormat(
"%d,%g,%d,%g,%.4f,%d," +
"%g,%d,%.1f,%d," +
"%g,%g",
GetPipDigits(), GetPipValue(), GetSpreadInPts(), GetSpreadInPips(), GetSpreadInPct(), GetPointsPerPip(),
GetTradeDistanceInValue(), GetTradeDistanceInPts(), GetTradeDistanceInPips(), GetVolumeDigits(),
GetDeltaValue(), GetLastPriceChangeInPips()
)
:
"Pip Digits,Pip Value,Spread,Pts/pip," +
"Trade Distance (value),Trade Distance (points),Trade Distance (pips), Volume digits," +
"Delta,Last change (pips)";
}
/* Snapshots */
/**
* Create a market snapshot.
*/
bool MakeSnapshot() {
int _size = ArraySize(snapshots);
if (ArrayResize(snapshots, _size + 1, 100)) {
snapshots[_size].dt = TimeCurrent();
snapshots[_size].ask = GetAsk();
snapshots[_size].bid = GetBid();
snapshots[_size].vol = GetSessionVolume();
return true;
} else {
return false;
}
}
/* Other methods */
/**
* Validate whether trade operation is permitted.
*
* @param int cmd
* Trade command.
* @param int sl
* Stop loss price value.
* @param int tp
* Take profit price value.
* @param string symbol
* Currency symbol.
* @return
* Returns true when trade operation is allowed.
*
* @see: https://book.mql4.com/appendix/limits
* @see: https://www.mql5.com/en/articles/2555#invalid_SL_TP_for_position
*/
double TradeOpAllowed(ENUM_ORDER_TYPE _cmd, double sl, double tp) {
double ask = GetAsk();
double bid = GetBid();
double openprice = GetOpenOffer(_cmd);
double closeprice = GetCloseOffer(_cmd);
// The minimum distance of SYMBOL_TRADE_STOPS_LEVEL taken into account.
double distance = GetTradeDistanceInValue();
// bool result;
switch (_cmd) {
case ORDER_TYPE_BUY:
// Buying is done at the Ask price.
// Requirements for Minimum Distance Limitation:
// - Bid - StopLoss >= StopLevel && TakeProfit - Bid >= StopLevel
// - Bid - StopLoss > FreezeLevel && TakeProfit - Bid > FreezeLevel
/*
result = sl > 0 && tp > 0 && bid - sl >= distance && tp - bid >= distance;
PrintFormat("1. Buy: (%g - %g) = %g >= %g; %s", Bid, sl, (bid - sl), distance, result ? "true" : "false");
PrintFormat("2. Buy: (%g - %g) = %g >= %g; %s", tp, Bid, (tp - Bid), distance, result ? "true" : "false");
*/
// The TakeProfit and StopLoss levels must be at the distance of at least SYMBOL_TRADE_STOPS_LEVEL points from the Bid price.
return sl > 0 && tp > 0 &&
bid - sl >= distance &&
tp - bid >= distance;
case ORDER_TYPE_SELL:
// Selling is done at the Bid price.
// Requirements for Minimum Distance Limitation:
// - StopLoss - Ask >= StopLevel && Ask - TakeProfit >= StopLevel
// - StopLoss - Ask > FreezeLevel && Ask - TakeProfit > FreezeLevel
/*
result = sl > 0 && tp > 0 && sl - ask > distance && ask - tp > distance;
PrintFormat("1. Sell: (%g - %g) = %g >= %g; %s",
sl, Ask, (sl - Ask), distance, result ? "true" : "false");
PrintFormat("2. Sell: (%g - %g) = %g >= %g; %s",
Ask, tp, (ask - tp), distance, result ? "true" : "false");
*/
// The TakeProfit and StopLoss levels must be at the distance of at least SYMBOL_TRADE_STOPS_LEVEL points from the Ask price.
return sl > 0 && tp > 0 &&
sl - ask > distance &&
ask - tp > distance;
case ORDER_TYPE_BUY_LIMIT:
// Requirements when performing trade operations:
// - Ask-OpenPrice >= StopLevel && OpenPrice-SL >= StopLevel && TP-OpenPrice >= StopLevel
// - Open Price of a Pending Order is Below the current Ask price.
// - Ask price reaches open price.
return
ask - openprice >= distance &&
openprice - sl >= distance &&
tp - openprice >= distance;
case ORDER_TYPE_SELL_LIMIT:
// Requirements when performing trade operations:
// - OpenPrice-Bid >= StopLevel && SL-OpenPrice >= StopLevel && OpenPrice-TP >= StopLevel
// - Open Price of a Pending Order is Above the current Bid price.
// - Bid price reaches open price.
return
openprice - bid >= distance &&
sl - openprice >= distance &&
openprice - tp >= distance;
case ORDER_TYPE_BUY_STOP:
// Requirements when performing trade operations:
// - OpenPrice-Ask >= StopLevel && OpenPrice-SL >= StopLevel && TP-OpenPrice >= StopLevel
// - Open Price of a Pending Order is Above the current Ask price.
// - Ask price reaches open price.
return
openprice - ask >= distance &&
openprice - sl >= distance &&
tp - openprice >= distance;
case ORDER_TYPE_SELL_STOP:
// Requirements when performing trade operations:
// - Bid-OpenPrice >= StopLevel && SL-OpenPrice >= StopLevel && OpenPrice-TP >= StopLevel
// - Open Price of a Pending Order is Below the current Bid price.
// - Bid price reaches open price.
return
bid - openprice >= distance &&
sl - openprice >= distance &&
openprice - tp >= distance;
default:
return (true);
}
}
/**
* Validate whether trade operation is permitted.
*
* @param int cmd
* Trade command.
* @param int price
* Take profit or stop loss price value.
* @return
* Returns true when trade operation is allowed.
*
* @see: https://book.mql4.com/appendix/limits
*/
double TradeOpAllowed(ENUM_ORDER_TYPE _cmd, double price) {
double distance = GetTradeDistanceInValue();
// bool result;
switch (_cmd) {
case ORDER_TYPE_BUY_STOP:
// OpenPrice-Ask >= StopLevel && OpenPrice-SL >= StopLevel && TP-OpenPrice >= StopLevel
case ORDER_TYPE_BUY_LIMIT:
// Ask-OpenPrice >= StopLevel && OpenPrice-SL >= StopLevel && TP-OpenPrice >= StopLevel
case ORDER_TYPE_BUY:
// Bid-OpenPrice >= StopLevel && SL-OpenPrice >= StopLevel && OpenPrice-TP >= StopLevel
case ORDER_TYPE_SELL_LIMIT:
// OpenPrice-Bid >= StopLevel && SL-OpenPrice >= StopLevel && OpenPrice-TP >= StopLevel
case ORDER_TYPE_SELL_STOP:
// Bid-OpenPrice >= StopLevel && SL-OpenPrice >= StopLevel && OpenPrice-TP >= StopLevel
case ORDER_TYPE_SELL:
// SL-Ask >= StopLevel && Ask-TP >= StopLevel
// OpenPrice-Ask >= StopLevel && OpenPrice-SL >= StopLevel && TP-OpenPrice >= StopLevel
// PrintFormat("%g > %g", fmin(fabs(GetBid() - price), fabs(GetAsk() - price)), distance);
return price > 0 && fmin(fabs(GetBid() - price), fabs(GetAsk() - price)) > distance;
default:
return (true);
}
}
/* 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 Condition(ENUM_MARKET_CONDITION _cond, MqlParam &_args[]) {
switch (_cond) {
case MARKET_COND_IN_PEAK_HOURS:
return DateTime::Hour() >= 8 && DateTime::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:
Logger().Error(StringFormat("Invalid market condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
return false;
}
}
bool Condition(ENUM_MARKET_CONDITION _cond) {
MqlParam _args[] = {};
return Market::Condition(_cond, _args);
}
};
#endif // MARKET_MQH