forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.mqh
760 lines (688 loc) · 24 KB
/
Account.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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//+------------------------------------------------------------------+
//| 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 ACCOUNT_MQH
#define ACCOUNT_MQH
// Forward class declaration.
class Account;
// Includes.
#include "Array.mqh"
#include "Chart.mqh"
#include "Convert.mqh"
#include "Orders.mqh"
#include "SymbolInfo.mqh"
// Enums.
enum ENUM_ACC_STAT_VALUE {
ACC_BALANCE = 0,
ACC_CREDIT = 1,
ACC_EQUITY = 2,
ACC_PROFIT = 3,
ACC_MARGIN_USED = 4,
ACC_MARGIN_FREE = 5,
FINAL_ENUM_ACC_STAT_VALUE = 6
};
enum ENUM_ACC_STAT_PERIOD {
ACC_DAILY = 0,
ACC_WEEKLY = 1,
ACC_MONTHLY = 2,
FINAL_ENUM_ACC_STAT_PERIOD = 3
};
enum ENUM_ACC_STAT_TYPE {
ACC_VALUE_MIN = 0,
ACC_VALUE_MAX = 1,
ACC_VALUE_AVG = 2,
FINAL_ENUM_ACC_STAT_TYPE = 3
};
enum ENUM_ACC_STAT_INDEX {
ACC_VALUE_CURR = 0,
ACC_VALUE_PREV = 1,
FINAL_ENUM_ACC_STAT_INDEX = 2
};
// Account conditions.
enum ENUM_ACCOUNT_CONDITION {
ACCOUNT_COND_NONE = 0, // Empty condition.
/* @todo
ACCOUNT_COND_BALM_GT_YEARLY, // Current month's balance highest of the year
ACCOUNT_COND_BALM_LT_YEARLY, // Current month's balance lowest of the year
ACCOUNT_COND_BALT_GT_WEEKLY, // Today's balance highest of the week
ACCOUNT_COND_BALT_IN_LOSS, // Today's balance in loss
ACCOUNT_COND_BALT_IN_PROFIT, // Today's balance in profit
ACCOUNT_COND_BALT_LT_WEEKLY, // Today's balance lowest of the week
ACCOUNT_COND_BALW_GT_MONTHLY, // Current week's balance highest of the month
ACCOUNT_COND_BALW_LT_MONTHLY, // Current week's balance lowest of the month
ACCOUNT_COND_BALY_IN_LOSS, // Previous day in loss
ACCOUNT_COND_BALY_IN_PROFIT, // Previous day in profit
*/
ACCOUNT_COND_BAL_IN_LOSS, // Total balance in loss
ACCOUNT_COND_BAL_IN_PROFIT, // Total balance in profit
ACCOUNT_COND_EQUITY_01PC_HIGH, // Equity 1% high
ACCOUNT_COND_EQUITY_01PC_LOW, // Equity 1% low
ACCOUNT_COND_EQUITY_05PC_HIGH, // Equity 5% high
ACCOUNT_COND_EQUITY_05PC_LOW, // Equity 5% low
ACCOUNT_COND_EQUITY_10PC_HIGH, // Equity 10% high
ACCOUNT_COND_EQUITY_10PC_LOW, // Equity 10% low
ACCOUNT_COND_EQUITY_20PC_HIGH, // Equity 20% high
ACCOUNT_COND_EQUITY_20PC_LOW, // Equity 20% low
ACCOUNT_COND_EQUITY_IN_LOSS, // Equity in loss
ACCOUNT_COND_EQUITY_IN_PROFIT, // Equity in profit
/* @todo
ACCOUNT_COND_MARGIN_CALL_10PC, // Margin Call (10% margin left)
ACCOUNT_COND_MARGIN_CALL_20PC, // Margin Call (20% margin left)
*/
ACCOUNT_COND_MARGIN_USED_10PC, // Margin Used in 10%
ACCOUNT_COND_MARGIN_USED_20PC, // Margin Used in 20%
ACCOUNT_COND_MARGIN_USED_50PC, // Margin Used in 50%
ACCOUNT_COND_MARGIN_USED_80PC, // Margin Used in 80%
ACCOUNT_COND_MARGIN_USED_99PC, // Margin Used in 99%
FINAL_ACCOUNT_CONDITION_ENTRY
};
// Class structs.
// Struct for making a snapshot of user account values.
struct AccountSnapshot {
datetime dtime;
double balance;
double credit;
double equity;
double profit;
double margin_used;
double margin_free;
double margin_avail;
};
/**
* Class to provide functions that return parameters of the current account.
*/
class Account {
protected:
// Struct variables.
AccountSnapshot snapshots[];
// Variables.
double init_balance, start_balance, start_credit;
// Store daily, weekly and monthly account statistics.
double acc_stats[FINAL_ENUM_ACC_STAT_VALUE][FINAL_ENUM_ACC_STAT_PERIOD][FINAL_ENUM_ACC_STAT_TYPE][FINAL_ENUM_ACC_STAT_INDEX];
// Class variables.
Orders *trades;
Orders *history;
Orders *dummy;
public:
// Defines.
#define ACC_OP_BALANCE 6 // Undocumented balance history statement entry.
#define ACC_OP_CREDIT 7 // Undocumented credit history statement entry.
/**
* Class constructor.
*/
Account() :
init_balance(CalcInitDeposit()),
start_balance(GetBalance()),
start_credit(GetCredit()),
trades(new Orders(ORDERS_POOL_TRADES)),
history(new Orders(ORDERS_POOL_HISTORY)),
dummy(new Orders(ORDERS_POOL_DUMMY))
{}
/**
* Class deconstructor.
*/
~Account() {
delete trades;
delete history;
delete dummy;
}
/* MT account methods */
/**
* Returns the current account name.
*/
static string AccountName() {
return AccountInfoString(ACCOUNT_NAME);
}
string GetAccountName() {
return AccountName();
}
/**
* Returns the connected server name.
*/
static string AccountServer() {
return AccountInfoString(ACCOUNT_SERVER);
}
string GetServerName() {
return AccountServer();
}
/**
* Returns currency name of the current account.
*/
static string AccountCurrency() {
return AccountInfoString(ACCOUNT_CURRENCY);
}
string GetCurrency() {
return AccountCurrency();
}
/**
* Returns the brokerage company name where the current account was registered.
*/
static string AccountCompany() {
return AccountInfoString(ACCOUNT_COMPANY);
}
string GetCompanyName() {
return AccountCompany();
}
/* Double getters */
/**
* Returns balance value of the current account.
*/
static double AccountBalance() {
return AccountInfoDouble(ACCOUNT_BALANCE);
}
double GetBalance() {
return UpdateStats(ACC_BALANCE, AccountBalance());
}
/**
* Returns credit value of the current account.
*/
static double AccountCredit() {
return AccountInfoDouble(ACCOUNT_CREDIT);
}
double GetCredit() {
return UpdateStats(ACC_CREDIT, AccountCredit());
}
/**
* Returns profit value of the current account.
*/
static double AccountProfit() {
return AccountInfoDouble(ACCOUNT_PROFIT);
}
double GetProfit() {
return UpdateStats(ACC_PROFIT, AccountProfit());
}
/**
* Returns equity value of the current account.
*/
static double AccountEquity() {
return AccountInfoDouble(ACCOUNT_EQUITY);
}
double GetEquity() {
return UpdateStats(ACC_EQUITY, AccountEquity());
}
/**
* Returns margin value of the current account.
*/
static double AccountMargin() {
return AccountInfoDouble(ACCOUNT_MARGIN);
}
double GetMarginUsed() {
return UpdateStats(ACC_MARGIN_USED, AccountMargin());
}
/**
* Returns free margin value of the current account.
*/
static double AccountFreeMargin() {
return AccountInfoDouble(ACCOUNT_MARGIN_FREE);
}
double GetMarginFree() {
return UpdateStats(ACC_MARGIN_FREE, AccountFreeMargin());
}
/**
* Returns the current account number.
*/
static long AccountNumber() {
return AccountInfoInteger(ACCOUNT_LOGIN);
}
long GetLogin() {
return AccountNumber();
}
/**
* Returns leverage of the current account.
*/
static long AccountLeverage() {
return AccountInfoInteger(ACCOUNT_LEVERAGE);
}
long GetLeverage() {
return AccountLeverage();
}
/**
* Returns the calculation mode for the Stop Out level.
*/
static int AccountStopoutMode() {
return (int) AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE);
}
int GetStopoutMode() {
return AccountStopoutMode();
}
/**
* Returns the value of the Stop Out level.
*
* Depending on the set ACCOUNT_MARGIN_SO_MODE,
* is expressed in percents or in the deposit currency.
*/
static double AccountStopoutLevel() {
return AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);
}
double GetStopoutLevel() {
return AccountStopoutLevel();
}
/**
* Get a maximum allowed number of active pending orders set by broker.
*
* @return
* Returns the limit orders (0 for unlimited).
*/
static long AccountLimitOrders() {
return AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);
}
long GetLimitOrders(uint _max = 999) {
long _limit = AccountLimitOrders();
return _limit > 0 ? _limit : _max;
}
/* Other account methods */
/**
* Get account total balance (including credit).
*/
static double AccountTotalBalance() {
return AccountBalance() + AccountCredit();
}
double GetTotalBalance() {
return GetBalance() + GetCredit();
}
/**
* Get account available margin.
*/
static double AccountAvailMargin() {
return fmin(AccountFreeMargin(), AccountTotalBalance());
}
double GetMarginAvail() {
return AccountAvailMargin();
}
/**
* Returns the calculation mode of free margin allowed to open orders on the current account.
*/
static double AccountFreeMarginMode() {
#ifdef __MQL4__
/*
* The calculation mode can take the following values:
* 0 - floating profit/loss is not used for calculation;
* 1 - both floating profit and loss on opened orders on the current account are used for free margin calculation;
* 2 - only profit value is used for calculation, the current loss on opened orders is not considered;
* 3 - only loss value is used for calculation, the current loss on opened orders is not considered.
*/
return ::AccountFreeMarginMode();
#else
// @todo: Not implemented yet.
return NULL;
#endif
}
double GetAccountFreeMarginMode() {
return AccountFreeMarginMode();
}
/* State checkers */
/**
* Indicates if an Expert Advisor is allowed to trade on the account.
*/
static bool IsExpertEnabled() {
return (bool) AccountInfoInteger(ACCOUNT_TRADE_EXPERT);
}
/**
* Check the permission to trade for the current account.
*/
static bool IsTradeAllowed() {
return (bool) AccountInfoInteger(ACCOUNT_TRADE_ALLOWED);
}
/**
* Check if the Expert Advisor runs on a demo account.
*/
bool IsDemo() {
#ifdef __MQL4__
return ::IsDemo();
#else // __MQL5__
return AccountInfoInteger(ACCOUNT_TRADE_MODE) == ACCOUNT_TRADE_MODE_DEMO;
#endif
}
/**
* Returns type of account (Demo or Live).
*/
string GetType() {
return GetServerName() != "" ? (IsDemo() ? "Demo" : "Live") : "Off-line";
}
/* Setters */
double UpdateStats(ENUM_ACC_STAT_VALUE _type, double _value) {
static datetime _last_check = TimeCurrent();
bool _stats_rotate = false;
for (uint _pindex = 0; _pindex < FINAL_ENUM_ACC_STAT_PERIOD; _pindex++) {
acc_stats[_type][_pindex][ACC_VALUE_MIN][ACC_VALUE_CURR] = fmin(acc_stats[_type][_pindex][ACC_VALUE_MIN][ACC_VALUE_CURR], _value);
acc_stats[_type][_pindex][ACC_VALUE_MAX][ACC_VALUE_CURR] = fmin(acc_stats[_type][_pindex][ACC_VALUE_MAX][ACC_VALUE_CURR], _value);
acc_stats[_type][_pindex][ACC_VALUE_AVG][ACC_VALUE_CURR] = (acc_stats[_type][_pindex][ACC_VALUE_AVG][ACC_VALUE_CURR] + _value) / 2;
switch (_pindex) {
case ACC_DAILY: _stats_rotate = _last_check < Chart::iTime(_Symbol, PERIOD_D1); break;
case ACC_WEEKLY: _stats_rotate = _last_check < Chart::iTime(_Symbol, PERIOD_W1); break;
case ACC_MONTHLY: _stats_rotate = _last_check < Chart::iTime(_Symbol, PERIOD_MN1); break;
}
if (_stats_rotate) {
acc_stats[_type][_pindex][ACC_VALUE_MIN][ACC_VALUE_PREV] = acc_stats[_type][_pindex][ACC_VALUE_MIN][ACC_VALUE_CURR];
acc_stats[_type][_pindex][ACC_VALUE_MAX][ACC_VALUE_PREV] = acc_stats[_type][_pindex][ACC_VALUE_MAX][ACC_VALUE_CURR];
acc_stats[_type][_pindex][ACC_VALUE_AVG][ACC_VALUE_PREV] = acc_stats[_type][_pindex][ACC_VALUE_AVG][ACC_VALUE_CURR];
acc_stats[_type][_pindex][ACC_VALUE_MIN][ACC_VALUE_CURR] = _value;
acc_stats[_type][_pindex][ACC_VALUE_MAX][ACC_VALUE_CURR] = _value;
acc_stats[_type][_pindex][ACC_VALUE_AVG][ACC_VALUE_CURR] = _value;
_last_check = TimeCurrent();
}
}
return _value;
}
/* Class getters */
/**
* Get account init balance.
*/
double GetInitBalance() {
return init_balance;
}
/**
* Get account start balance.
*/
double GetStartBalance() {
return start_balance;
}
/**
* Get account init credit.
*/
double GetStartCredit() {
return start_credit;
}
/* Calculation methods */
/**
* Get account stopout level in range: 0.0 - 1.0 where 1.0 is 100%.
*
* Note:
* - if(AccountEquity()/AccountMargin()*100 < AccountStopoutLevel()) { BrokerClosesOrders(); }
*/
static double GetAccountStopoutLevel() {
int mode = AccountStopoutMode();
double level = AccountStopoutLevel();
if (mode == 0 && level > 0) {
// Calculation of percentage ratio between margin and equity.
return (double) level / 100;
} else if (mode == 1) {
// Comparison of the free margin level to the absolute value.
return 1.0;
} else {
// @todo: Add logging.
//if (verbose) PrintFormat("%s(): Not supported mode (%d).", __FUNCTION__, mode);
}
return 1.0;
}
/**
* Returns free margin that remains after the specified order has been opened at the current price on the current account.
*
* @return
* Free margin that remains after the specified order has been opened at the current price on the current account.
* If the free margin is insufficient, an error 134 (ERR_NOT_ENOUGH_MONEY) will be generated.
*/
static double AccountFreeMarginCheck(string _symbol, ENUM_ORDER_TYPE _cmd, double _volume) {
// Notes:
// AccountFreeMarginCheck = FreeMargin - Margin1Lot * Lot;
// FreeMargin = Equity - Margin;
// Equity = Balance + Profit;
// FreeMargin = Balance + Profit - Margin;
// AccountFreeMarginCheck = Balance + Profit - Margin - Margin1Lot * Lot;
#ifdef __MQL4__
return ::AccountFreeMarginCheck(_symbol, _cmd, _volume);
#else
// @see: CAccountInfo::FreeMarginCheck
double _margin;
return (::OrderCalcMargin(_cmd, _symbol, _volume,
SymbolInfo::SymbolInfoDouble(_symbol, (_cmd == ORDER_TYPE_BUY) ? SYMBOL_ASK : SYMBOL_BID), _margin) ?
AccountInfoDouble(ACCOUNT_MARGIN_FREE) - _margin : -1);
#endif
}
double GetAccountFreeMarginCheck(ENUM_ORDER_TYPE _cmd, double _volume) {
return AccountFreeMarginCheck(_Symbol, _cmd, _volume);
}
/**
* Get current account drawdown in percent.
*/
static double GetDrawdownInPct() {
return (100 / AccountTotalBalance()) * (AccountTotalBalance() - AccountEquity());
}
/**
* Get current account risk margin level.
*
* The risk is calculated based on the stop loss sum of opened orders.
*
* @return
* Returns value from 0.0 (no risk) and 1.0 (100% risk).
* The risk higher than 1.0 means that the risk is extremely high.
*/
double GetRiskMarginLevel(ENUM_ORDER_TYPE _cmd = NULL) {
double _avail_margin = AccountAvailMargin() * Convert::ValueToMoney(trades.TotalSL(_cmd));
return _avail_margin > 0 ? 1 / _avail_margin : 0;
}
/**
* Calculates initial deposit based on the current balance and previous orders.
*/
static double CalcInitDeposit() {
double deposit = AccountInfoDouble(ACCOUNT_BALANCE);
for (int i = Account::OrdersHistoryTotal() - 1; i >= 0; i--) {
if (!Order::OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
int type = Order::OrderType();
// Initial balance not considered.
if (i == 0 && type == ACC_OP_BALANCE) break;
if (type == ORDER_TYPE_BUY || type == ORDER_TYPE_SELL) {
// Calculate profit.
double profit = Order::OrderProfit() + Order::OrderCommission() + Order::OrderSwap();
// Calculate decrease balance.
deposit -= profit;
}
if (type == ACC_OP_BALANCE || type == ACC_OP_CREDIT) {
deposit -= Order::OrderProfit();
}
}
return deposit;
}
/**
* Returns the number of closed orders in the account history loaded into the terminal.
*/
static int OrdersHistoryTotal() {
#ifdef __MQL4__
return ::OrdersHistoryTotal();
#else
::HistorySelect(0, TimeCurrent());
return ::HistoryOrdersTotal();
#endif
}
/**
* Calculate total profit.
*/
double GetTotalProfit() {
/* @todo
double total_profit = 0;
for (int id = 0; id < ArrayRange(stats, 0); id++) {
total_profit += stats[id][TOTAL_NET_PROFIT];
}
return total_profit;
*/
return 0;
}
/**
* Returns min/max/avg daily/weekly/monthly account balance/equity/margin.
*/
double GetStatValue(ENUM_ACC_STAT_VALUE _value_type, ENUM_ACC_STAT_PERIOD _period, ENUM_ACC_STAT_TYPE _stat_type, ENUM_ACC_STAT_INDEX _shift = ACC_VALUE_CURR) {
// @fixme
return acc_stats[_value_type][_period][_stat_type][_shift];
}
/* State checkers */
/**
* Check account free margin.
*
* @return
* Returns true, when free margin is sufficient, false when insufficient or on error.
*/
bool IsFreeMargin(ENUM_ORDER_TYPE _cmd, double size_of_lot, string _symbol = NULL) {
bool _res = true;
double margin = AccountFreeMarginCheck(_symbol, _cmd, size_of_lot);
if (GetLastError() == 134 /* NOT_ENOUGH_MONEY */) _res = false;
return (_res);
}
/* Conditions */
/**
* Checks for account condition.
*
* @param ENUM_ACCOUNT_CONDITION _cond
* Account condition.
* @return
* Returns true when the condition is met.
*/
bool Condition(ENUM_ACCOUNT_CONDITION _cond, MqlParam &_args[]) {
switch (_cond) {
/* @todo
case ACCOUNT_COND_BALM_GT_YEARLY:
// @todo
return false;
case ACCOUNT_COND_BALM_LT_YEARLY:
// @todo
return false;
case ACCOUNT_COND_BALT_GT_WEEKLY:
return
trade.Account().GetStatValue(ACC_BALANCE, ACC_DAILY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX)))) >
trade.Account().GetStatValue(ACC_BALANCE, ACC_WEEKLY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX))));
case ACCOUNT_COND_BALT_IN_LOSS:
// @todo
return false;
case ACCOUNT_COND_BALT_IN_PROFIT:
// @todo
return false;
case ACCOUNT_COND_BALT_LT_WEEKLY:
return
trade.Account().GetStatValue(ACC_BALANCE, ACC_DAILY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX)))) <
trade.Account().GetStatValue(ACC_BALANCE, ACC_WEEKLY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX))));
case ACCOUNT_COND_BALW_GT_MONTHLY:
return
trade.Account().GetStatValue(ACC_BALANCE, ACC_WEEKLY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX)))) >
trade.Account().GetStatValue(ACC_BALANCE, ACC_MONTHLY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX))));
case ACCOUNT_COND_BALW_LT_MONTHLY:
return
trade.Account().GetStatValue(ACC_BALANCE, ACC_WEEKLY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, ACC_VALUE_MAX)))) <
trade.Account().GetStatValue(ACC_BALANCE, ACC_MONTHLY, (ENUM_ACC_STAT_TYPE) fmin(0, fmax(FINAL_ENUM_ACC_STAT_TYPE - 1, GetArg(_index, 0, 1))));
case ACCOUNT_COND_BALY_IN_LOSS:
return trade.Account().GetProfit() < trade.Account().GetProfit() / 100 * (100 - GetArg(_index, 0, 10));
case ACCOUNT_COND_BALY_IN_PROFIT:
return trade.Account().GetProfit() > trade.Account().GetProfit() / 100 * (100 + GetArg(_index, 0, 10));
*/
case ACCOUNT_COND_BAL_IN_LOSS:
return GetBalance() < start_balance;
case ACCOUNT_COND_BAL_IN_PROFIT:
return GetBalance() > start_balance;
case ACCOUNT_COND_EQUITY_01PC_HIGH:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 101;
case ACCOUNT_COND_EQUITY_01PC_LOW:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 99;
case ACCOUNT_COND_EQUITY_05PC_HIGH:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 105;
case ACCOUNT_COND_EQUITY_05PC_LOW:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 95;
case ACCOUNT_COND_EQUITY_10PC_HIGH:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 110;
case ACCOUNT_COND_EQUITY_10PC_LOW:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 90;
case ACCOUNT_COND_EQUITY_20PC_HIGH:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 120;
case ACCOUNT_COND_EQUITY_20PC_LOW:
return AccountEquity() > (AccountBalance() + AccountCredit()) / 100 * 80;
case ACCOUNT_COND_EQUITY_IN_LOSS:
return GetEquity() < GetTotalBalance();
case ACCOUNT_COND_EQUITY_IN_PROFIT:
return GetEquity() > GetTotalBalance();
/*
case ACCOUNT_COND_MARGIN_CALL_10PC:
// @todo
return false;
case ACCOUNT_COND_MARGIN_CALL_20PC:
// @todo
return false;
*/
case ACCOUNT_COND_MARGIN_USED_10PC:
return AccountMargin() >= AccountEquity() / 100 * 10;
case ACCOUNT_COND_MARGIN_USED_20PC:
return AccountMargin() >= AccountEquity() / 100 * 20;
case ACCOUNT_COND_MARGIN_USED_50PC:
return AccountMargin() >= AccountEquity() / 100 * 50;
case ACCOUNT_COND_MARGIN_USED_80PC:
return AccountMargin() >= AccountEquity() / 100 * 80;
case ACCOUNT_COND_MARGIN_USED_99PC:
return AccountMargin() >= AccountEquity() / 100 * 99;
default:
//logger.Error(StringFormat("Invalid account condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
#ifdef __debug__
Print(StringFormat("%s: Error: Invalid account condition: %d!", __FUNCTION__, _cond));
#endif
return false;
}
}
bool Condition(ENUM_ACCOUNT_CONDITION _cond) {
MqlParam _args[] = {};
return Account::Condition(_cond, _args);
}
/* Printers */
/**
* Returns text info about the account.
*/
string ToString() {
return StringFormat(
"Type: %s, Server/Company/Name: %s/%s/%s, Currency: %s, Balance: %g, Credit: %g, Equity: %g, Profit: %g, Margin Used/Free/Avail: %g/%g/%g, Orders limit: %g: Leverage: 1:%d, StopOut Level: %d (Mode: %d)",
GetType(), GetServerName(),GetCompanyName(), GetAccountName(), GetCurrency(), GetBalance(), GetCredit(), GetEquity(),
GetProfit(), GetMarginUsed(), GetMarginFree(), GetMarginAvail(), GetLimitOrders(), GetLeverage(), GetStopoutLevel(), GetStopoutMode()
);
}
/**
* Returns info about the account in CSV format.
*/
string ToCSV() {
return StringFormat(
"%g,%g,%g,%g,%g,%g",
GetTotalBalance(), GetEquity(), GetProfit(), GetMarginUsed(), GetMarginFree(), GetMarginAvail()
);
}
/* Snapshots */
/**
* Create a market snapshot.
*/
bool MakeSnapshot() {
uint _size = Array::ArraySize(snapshots);
if (ArrayResize(snapshots, _size + 1, 100)) {
snapshots[_size].dtime = TimeCurrent();
snapshots[_size].balance = GetBalance();
snapshots[_size].credit = GetCredit();
snapshots[_size].equity = GetEquity();
snapshots[_size].profit = GetProfit();
snapshots[_size].margin_used = GetMarginUsed();
snapshots[_size].margin_free = GetMarginFree();
snapshots[_size].margin_avail = GetMarginAvail();
return true;
} else {
return false;
}
}
/* Class access methods */
/**
* Returns Orders class to access the current trades.
*/
Orders *Trades() {
return trades;
}
Orders *History() {
return history;
}
Orders *Dummy() {
return dummy;
}
};
#endif // ACCOUNT_MQH