Skip to content

Commit

Permalink
Trade: Moves static method from Account to TradeHistoryStatic struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Apr 27, 2021
1 parent fac0df2 commit 9d8483c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
14 changes: 1 addition & 13 deletions Account.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class Account {
*/
static double CalcInitDeposit() {
double deposit = AccountInfoDouble(ACCOUNT_BALANCE);
for (int i = Account::OrdersHistoryTotal() - 1; i >= 0; i--) {
for (int i = TradeHistoryStatic::HistoryOrdersTotal() - 1; i >= 0; i--) {
if (!Order::TryOrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
int type = Order::OrderType();
// Initial balance not considered.
Expand All @@ -467,18 +467,6 @@ class Account {
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.
*/
Expand Down
2 changes: 1 addition & 1 deletion Orders.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class Orders {
last_time.sell_time = 0;
//---
#ifdef __MQL4__
int orders_total = Account::OrdersHistoryTotal();
int orders_total = TradeHistoryStatic::HistoryOrdersTotal();
for (int i = orders_total - 1; i >= 0; i--) {
if (!Order::TryOrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
return (false);
Expand Down
3 changes: 2 additions & 1 deletion SummaryReport.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Convert.mqh"
#include "Orders.mqh"
#include "Terminal.mqh"
#include "Trade.mqh"

/**
* Class to provide a summary report.
Expand Down Expand Up @@ -143,7 +144,7 @@ class SummaryReport {
int sequence = 0, profitseqs = 0, loss_seqs = 0;
double sequential = 0.0, prev_profit = EMPTY_VALUE, dd_pct, drawdown;
double max_peak = init_deposit, min_peak = init_deposit, balance = init_deposit;
int trades_total = Account::OrdersHistoryTotal();
int trades_total = TradeHistoryStatic::HistoryOrdersTotal();
double profit;

// Initialize summaries.
Expand Down
12 changes: 10 additions & 2 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ class Trade {
/**
* Class constructor.
*/
Trade() : order_last(NULL) { SetName(); };
Trade() : order_last(NULL) { SetName(); OrdersLoadByMagic(); };
Trade(TradeParams &_tparams, ChartParams &_cparams)
: chart(_cparams), tparams(_tparams), order_last(NULL) {
SetName();
OrdersLoadByMagic();
};

/**
Expand Down Expand Up @@ -435,7 +436,7 @@ CDealInfo deal;
HistorySelect(0, TimeCurrent()); // Select history for access.
*/
#endif
int _orders = Account::OrdersHistoryTotal();
int _orders = TradeHistoryStatic::HistoryOrdersTotal();
for (int i = _orders - 1; i >= fmax(0, _orders - ols_orders); i--) {
#ifdef __MQL5__
/* @fixme: Rewrite without using CDealInfo.
Expand Down Expand Up @@ -591,6 +592,13 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
return _result;
}

/**
* Load orders by magic number.
*/
bool OrdersLoadByMagic() {
return true;
}

/**
* Returns the number of market and pending orders.
*
Expand Down
17 changes: 17 additions & 0 deletions Trade.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,20 @@ struct TradeStates {
return SerializerNodeObject;
}
};

// Structure for trade history static methods.
struct TradeHistoryStatic {

/**
* Returns the number of closed orders in the account history loaded into the terminal.
*/
static int HistoryOrdersTotal() {
#ifdef __MQL4__
return ::OrdersHistoryTotal();
#else
::HistorySelect(0, ::TimeCurrent()); // @todo: Use DateTimeStatic().
return ::HistoryOrdersTotal();
#endif
}

};

0 comments on commit 9d8483c

Please sign in to comment.