Skip to content

Commit

Permalink
Account/Orders: Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Apr 27, 2021
1 parent 9d8483c commit 56710c7
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
- MarketTest
- MatrixTest
- OrderTest
- OrdersTest
- StatsTest
- StrategyTest
- StrategyTest-RSI
Expand Down
25 changes: 25 additions & 0 deletions Account.define.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, 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/>.
*
*/

// Defines.
#define ACC_OP_BALANCE 6 // Undocumented balance history statement entry.
#define ACC_OP_CREDIT 7 // Undocumented credit history statement entry.
32 changes: 6 additions & 26 deletions Account.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
class Account;

// Includes.
#include "Account.define.h"
#include "Account.enum.h"
#include "Account.struct.h"
#include "Array.mqh"
#include "BufferStruct.mqh"
#include "Chart.mqh"
#include "Convert.mqh"
#include "Indicator.struct.h"
#include "Orders.mqh"
#include "Trade.struct.h"
#include "Serializer.mqh"
#include "SymbolInfo.mqh"

Expand All @@ -53,34 +54,20 @@ class Account {
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)) {}
start_credit(GetCredit()) {}

/**
* Class deconstructor.
*/
~Account() {
delete trades;
delete history;
delete dummy;
}

/* Entries */
Expand Down Expand Up @@ -439,16 +426,18 @@ class Account {
* 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.
*/
/* @fixme
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);
double deposit = Account::AccountInfoDouble(ACCOUNT_BALANCE);
for (int i = TradeHistoryStatic::HistoryOrdersTotal() - 1; i >= 0; i--) {
if (!Order::TryOrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
int type = Order::OrderType();
Expand Down Expand Up @@ -631,15 +620,6 @@ class Account {
GetMarginFree(), GetMarginAvail());
}

/* Class access methods */

/**
* Returns Orders class to access the current trades.
*/
Orders *Trades() { return trades; }
Orders *History() { return history; }
Orders *Dummy() { return dummy; }

/* Serializers */

/**
Expand Down
14 changes: 3 additions & 11 deletions Orders.mqh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, 31337 Investments Ltd |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

Expand All @@ -22,21 +22,14 @@

// Forward declarations.
class Orders;
#ifdef __MQL5__
// class CDealInfo;
#endif

// Includes.
//#include "Account.mqh"
#include "Account.mqh"
#include "Chart.mqh"
#include "Log.mqh"
#include "Math.h"
#include "Order.mqh"
#include "Terminal.mqh"
#ifdef __MQL5__
//#include <Trade/DealInfo.mqh>
//#include <Trade/Trade.mqh> // @removeme
//#include <Trade/PositionInfo.mqh> // @removeme
#endif

/* Defines */

Expand Down Expand Up @@ -222,7 +215,6 @@ class Orders {
* from all opened orders for the given symbol.
*/
static double TotalSLTP(ENUM_ORDER_TYPE _cmd = NULL, bool sl = true) {
#include "Chart.mqh"
double total_buy_sl = 0, total_buy_tp = 0;
double total_sell_sl = 0, total_sell_tp = 0;
// @todo: Convert to MQL5.
Expand Down
7 changes: 2 additions & 5 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -708,12 +708,14 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
/**
* Calculate available lot size given the risk margin.
*/
/* @fixme
uint CalcMaxLotSize(double risk_margin = 1.0) {
double _avail_margin = account.AccountAvailMargin();
double _opened_lots = GetTrades().GetOpenLots();
// @todo
return 0;
}
*/

/**
* Calculate number of allowed orders to open.
Expand Down Expand Up @@ -1516,11 +1518,6 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
*/
Log *GetLogger() { return GetPointer(logger); }

/**
* Returns pointer to account's trades.
*/
Orders *GetTrades() { return account.Trades(); }

/* Serializers */

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/AccountTest.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* @file
* Test functionality of Dict class.
* Test functionality of Account class.
*/

// Includes.
Expand Down Expand Up @@ -79,7 +79,7 @@ int OnInit() {
Print(acc.IsFreeMargin(ORDER_TYPE_SELL, SymbolInfo::GetVolumeMin(_Symbol)));

assertTrueOrFail(acc.GetDrawdownInPct() == 0.0, "Invalid drawdown value!"); // 0
assertTrueOrFail(acc.GetRiskMarginLevel() == 0.0, "Invalid risk margin level!"); // 0
// assertTrueOrFail(acc.GetRiskMarginLevel() == 0.0, "Invalid risk margin level!"); // 0
assertTrueOrFail(acc.CalcInitDeposit() == _balance, "Invalid calculated initial deposit!"); // 10000

// Print account details.
Expand Down
29 changes: 29 additions & 0 deletions tests/OrdersTest.mq4
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, 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/>.
*
*/

/**
* @file
* Test functionality of Orders class.
*/

// Includes.
#include "OrdersTest.mq5"
37 changes: 37 additions & 0 deletions tests/OrdersTest.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, 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/>.
*
*/


/**
* @file
* Test functionality of Orders class.
*/

// Includes.
#include "../Orders.mqh"

/**
* Implements OnInit().
*/
int OnInit() {
return (INIT_SUCCEEDED);
}

0 comments on commit 56710c7

Please sign in to comment.