Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v3.007-dev' into v3.009-dev-new
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Sep 3, 2024
2 parents f17541f + ec070dc commit 6ecae7f
Show file tree
Hide file tree
Showing 50 changed files with 1,557 additions and 600 deletions.
56 changes: 29 additions & 27 deletions EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class EA : public Taskable<DataParamEntry> {
BufferStruct<SymbolInfoEntry> data_symbol;
Dict<string, double> ddata; // Custom user data.
Dict<string, int> idata; // Custom user data.
DictObject<string, Trade> trade;
DictStruct<string, Ref<Trade>> trade;
DictObject<int, BufferStruct<IndicatorDataEntry>> data_indi;
DictObject<int, BufferStruct<StgEntry>> data_stg;
EAParams eparams;
Expand Down Expand Up @@ -114,11 +114,11 @@ class EA : public Taskable<DataParamEntry> {
// Add and process tasks.
Init();
// Initialize a trade instance for the current chart and symbol.
Ref<IndicatorData> _source = Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT);
Ref<IndicatorData> _source = Platform::FetchDefaultCandleIndicator(Platform::GetSymbol(), Platform::GetPeriod());
TradeParams _tparams(0, 1.0f, 0, (ENUM_LOG_LEVEL)eparams.Get<int>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
Trade _trade(_tparams, _source.Ptr());
Ref<Trade> _trade = new Trade(_tparams, _source.Ptr());
trade.Set(_Symbol, _trade);
logger.Link(_trade.GetLogger());
logger.Link(_trade REF_DEREF GetLogger());
logger.SetLevel((ENUM_LOG_LEVEL)eparams.Get<int>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
//_trade.GetLogger().SetLevel((ENUM_LOG_LEVEL)eparams.Get<int>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
}
Expand Down Expand Up @@ -253,7 +253,7 @@ class EA : public Taskable<DataParamEntry> {
*/
template <typename T>
void Set(ENUM_TRADE_PARAM _param, T _value) {
for (DictObjectIterator<string, Trade> iter = trade.Begin(); iter.IsValid(); ++iter) {
for (DictObjectIterator<string, Ref<Trade>> iter = trade.Begin(); iter.IsValid(); ++iter) {
Trade *_trade = iter.Value();
_trade PTR_DEREF Set<T>(_param, _value);
}
Expand All @@ -279,7 +279,7 @@ class EA : public Taskable<DataParamEntry> {
// Ignores already processed signals.
continue;
}
Trade *_trade = trade.GetByKey(_Symbol);
Trade *_trade = trade.GetByKey(_Symbol).Ptr();
Strategy *_strat =
strats.GetByKey(_signal PTR_DEREF Get<int64>(STRUCT_ENUM(TradeSignalEntry, TRADE_SIGNAL_PROP_MAGIC_ID))).Ptr();
_trade_allowed &= _trade.IsTradeAllowed();
Expand Down Expand Up @@ -384,7 +384,7 @@ class EA : public Taskable<DataParamEntry> {
*/
virtual bool TradeRequest(ENUM_ORDER_TYPE _cmd, string _symbol = NULL, Strategy *_strat = NULL) {
bool _result = false;
Trade *_trade = trade.GetByKey(_symbol);
Trade *_trade = trade.GetByKey(_symbol).Ptr();
// Prepare a request.
MqlTradeRequest _request = _trade PTR_DEREF GetTradeOpenRequest(_cmd);
_request.comment = _strat PTR_DEREF GetOrderOpenComment();
Expand Down Expand Up @@ -450,7 +450,7 @@ class EA : public Taskable<DataParamEntry> {
for (DictStructIterator<int64, Ref<Strategy>> iter = strats.Begin(); iter.IsValid(); ++iter) {
bool _can_trade = true;
Strategy *_strat = iter.Value().Ptr();
Trade *_trade = trade.GetByKey(_Symbol);
Trade *_trade = trade.GetByKey(_Symbol).Ptr();
if (_strat PTR_DEREF IsEnabled()) {
if (estate.Get<unsigned int>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) >= DATETIME_MINUTE) {
// Process when new periods started.
Expand Down Expand Up @@ -595,9 +595,10 @@ class EA : public Taskable<DataParamEntry> {
SerializerConverter _stub =
SerializerConverter::MakeStubObject<BufferStruct<IndicatorDataEntry>>(_serializer_flags);

/*
for (DictStructIterator<int64, Ref<Strategy>> iter = strats.Begin(); iter.IsValid(); ++iter) {
ENUM_TIMEFRAMES _itf = iter_tf.Key(); // @fixme
/* @todo
for (DictStructIterator<int64, Ref<Strategy>> _iter = GetStrategies() PTR_DEREF Begin(); _iter.IsValid(); ++_iter)
{ int _sid = (int)_iter.Key(); Strategy *_strat = _iter.Value().Ptr();
// ENUM_TIMEFRAMES _itf = iter_tf.Key(); // @fixme
if (data_indi.KeyExists(_itf)) {
BufferStruct<IndicatorDataEntry> _indi_buff = data_indi.GetByKey(_itf);
Expand Down Expand Up @@ -630,15 +631,16 @@ class EA : public Taskable<DataParamEntry> {
if (eparams.CheckFlagDataStore(EA_DATA_STORE_STRATEGY)) {
SerializerConverter _stub = SerializerConverter::MakeStubObject<BufferStruct<StgEntry>>(_serializer_flags);

/* @fixme
for (DictStructIterator<int64, Ref<Strategy>> iter = strats.Begin(); iter.IsValid(); ++iter) {
ENUM_TIMEFRAMES _stf = iter_tf.Key(); // @fixme
if (data_stg.KeyExists(_stf)) {
string _key_stg = StringFormat("Strategy-%d", _stf);
BufferStruct<StgEntry> _stg_buff = data_stg.GetByKey(_stf);
for (DictStructIterator<int64, Ref<Strategy>> _iter = GetStrategies() PTR_DEREF Begin(); _iter.IsValid();
++_iter) {
int _sid = (int)_iter.Key();
Strategy *_strat = _iter.Value().Ptr();
if (data_stg.KeyExists(_sid)) {
string _key_stg = StringFormat("Strategy-%d", _sid);
BufferStruct<StgEntry> _stg_buff = data_stg.GetByKey(_sid);
SerializerConverter _obj = SerializerConverter::FromObject(_stg_buff, _serializer_flags);

_key_stg += StringFormat("-%d-%d-%d", _stf, _stg_buff.GetMin(), _stg_buff.GetMax());
_key_stg += StringFormat("-%d-%d-%d", _sid, _stg_buff.GetMin(), _stg_buff.GetMax());
if ((_methods & EA_DATA_EXPORT_CSV) != 0) {
_obj.ToFile<SerializerCsv>(_key_stg + ".csv", _serializer_flags, &_stub);
}
Expand All @@ -653,7 +655,6 @@ class EA : public Taskable<DataParamEntry> {
_obj.Clean();
}
}
*/
// Required because of SERIALIZER_FLAG_REUSE_STUB flag.
_stub.Clean();
}
Expand Down Expand Up @@ -818,7 +819,7 @@ class EA : public Taskable<DataParamEntry> {
*/
bool StrategyLoadTrades(Strategy *_strat) {
bool _result = true;
Trade *_trade = trade.GetByKey(_Symbol);
Trade *_trade = trade.GetByKey(_Symbol).Ptr();
// Load active trades.
_result &= _trade.OrdersLoadByMagic(_strat PTR_DEREF Get<int64>(STRAT_PARAM_ID));
// Load strategy-specific order parameters (e.g. conditions).
Expand All @@ -845,8 +846,8 @@ class EA : public Taskable<DataParamEntry> {
bool ProcessTrades() {
bool _result = true;
ResetLastError();
for (DictObjectIterator<string, Trade> titer = trade.Begin(); titer.IsValid(); ++titer) {
Trade *_trade = titer.Value();
for (DictStructIterator<string, Ref<Trade>> titer = trade.Begin(); titer.IsValid(); ++titer) {
Trade *_trade = titer.Value().Ptr();
if (_trade.Get<bool>(TRADE_STATE_ORDERS_ACTIVE) && !_trade.Get<bool>(TRADE_STATE_MARKET_CLOSED)) {
for (DictStructIterator<int64, Ref<Order>> oiter = _trade.GetOrdersActive().Begin(); oiter.IsValid(); ++oiter) {
bool _sl_valid = false, _tp_valid = false;
Expand Down Expand Up @@ -941,7 +942,7 @@ class EA : public Taskable<DataParamEntry> {
bool _result = false;
if (eparams.CheckFlag(EA_PARAM_FLAG_LOTSIZE_AUTO)) {
// Auto calculate lot size for all strategies.
Trade *_trade = trade.GetByKey(_Symbol);
Trade *_trade = trade.GetByKey(_Symbol).Ptr();
_result &= _trade PTR_DEREF Run(TRADE_ACTION_CALC_LOT_SIZE);
Set(STRAT_PARAM_LS, _trade PTR_DEREF Get<float>(TRADE_PARAM_LOT_SIZE));
}
Expand Down Expand Up @@ -1033,13 +1034,13 @@ class EA : public Taskable<DataParamEntry> {
switch (_entry.GetId()) {
case EA_ACTION_DISABLE:
estate.Enable(false);
return true;
break;
case EA_ACTION_ENABLE:
estate.Enable();
return true;
break;
case EA_ACTION_EXPORT_DATA:
DataExport();
return true;
break;
case EA_ACTION_STRATS_EXE_ACTION: {
// Args:
// 1st (i:0) - Strategy's enum action to execute.
Expand All @@ -1051,14 +1052,15 @@ class EA : public Taskable<DataParamEntry> {

_result &= _strat PTR_DEREF Run(_entry_strat);
}
return _result;
break;
}
case EA_ACTION_TASKS_CLEAN:
tasks.GetTasks().Clear();
return tasks.GetTasks().Size() == 0;
default:
GetLogger() PTR_DEREF Error(StringFormat("Invalid EA action: %d!", _entry.GetId(), __FUNCTION_LINE__));
SetUserError(ERR_INVALID_PARAMETER);
_result = false;
}
return _result;
}
Expand Down
44 changes: 44 additions & 0 deletions Exchange/Account/Account.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ enum ENUM_ACC_STAT_TYPE { ACC_VALUE_MIN = 0, ACC_VALUE_MAX = 1, ACC_VALUE_AVG =
/* Account type of index for statistics. */
enum ENUM_ACC_STAT_INDEX { ACC_VALUE_CURR = 0, ACC_VALUE_PREV = 1, FINAL_ENUM_ACC_STAT_INDEX = 2 };

#ifndef __MQL5__
/**
* Enumeration for the margin modes.
*
* @docs
* https://www.mql5.com/en/docs/constants/environment_state/accountinformation
*/
enum ENUM_ACCOUNT_MARGIN_MODE {
ACCOUNT_MARGIN_MODE_RETAIL_NETTING, // Used for the OTC markets to interpret positions in the "netting" mode.
ACCOUNT_MARGIN_MODE_EXCHANGE, // Used for the exchange markets.
ACCOUNT_MARGIN_MODE_RETAIL_HEDGING, // Used for the exchange markets where individual positions are possible.
};
#endif

#ifndef __MQL__
/**
* Enumeration for the current account double values.
Expand Down Expand Up @@ -147,3 +161,33 @@ enum ENUM_ACCOUNT_STOPOUT_MODE {
ACCOUNT_STOPOUT_MODE_MONEY, // Account stop out mode in money.
};
#endif

/**
* Enumeration for the account integer param values.
*
* Used for function AccountInfoInteger().
*
* @docs
* https://www.mql5.com/en/docs/constants/environment_state/accountinformation
*/
enum ENUM_ACCOUNT_PARAM_INTEGER {
ACCOUNT_PARAM_LOGIN = ACCOUNT_LOGIN, // Account number (long).
ACCOUNT_PARAM_TRADE_MODE = ACCOUNT_TRADE_MODE, // Account trade mode (ENUM_ACCOUNT_TRADE_MODE).
ACCOUNT_PARAM_LEVERAGE = ACCOUNT_LEVERAGE, // Account leverage (long).
ACCOUNT_PARAM_LIMIT_ORDERS = ACCOUNT_LIMIT_ORDERS, // Maximum allowed number of active pending orders (int).
ACCOUNT_PARAM_MARGIN_SO_MODE =
ACCOUNT_MARGIN_SO_MODE, // Mode for setting the minimal allowed margin (ENUM_ACCOUNT_STOPOUT_MODE).
ACCOUNT_PARAM_TRADE_ALLOWED = ACCOUNT_TRADE_ALLOWED, // Allowed trade for the current account (bool).
ACCOUNT_PARAM_TRADE_EXPERT = ACCOUNT_TRADE_EXPERT, // Allowed trade for an Expert Advisor (bool).
#ifdef __MQL5__
ACCOUNT_PARAM_MARGIN_MODE = ACCOUNT_MARGIN_MODE, // Margin calculation mode (ENUM_ACCOUNT_MARGIN_MODE).
ACCOUNT_PARAM_CURRENCY_DIGITS =
ACCOUNT_CURRENCY_DIGITS, // The number of decimal places in the account currency (int).
ACCOUNT_PARAM_FIFO_CLOSE =
ACCOUNT_FIFO_CLOSE, // An indication showing that positions can only be closed by FIFO rule (bool).
#else
ACCOUNT_PARAM_MARGIN_MODE = 7, // Margin calculation mode (ENUM_ACCOUNT_MARGIN_MODE).
ACCOUNT_PARAM_CURRENCY_DIGITS, // The number of decimal places in the account currency (int).
ACCOUNT_PARAM_FIFO_CLOSE, // An indication showing that positions can only be closed by FIFO rule (bool).
#endif
};
25 changes: 25 additions & 0 deletions Exchange/Account/Account.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// Includes.
#include "../../Serializer/Serializer.h"
#include "../../Storage/Dict/Buffer/BufferStruct.h"
#include "Account.struct.h"
#include "AccountBase.h"

/**
Expand All @@ -36,6 +37,7 @@
template <typename AS, typename AE>
class Account : public AccountBase {
protected:
AccountParams aparams;
AS state;
BufferStruct<AE> entries;

Expand All @@ -52,8 +54,31 @@ class Account : public AccountBase {
*/
Account() { Init(); }

/**
* Class constructor with parameters.
*/
Account(AccountParams &_aparams) : aparams(_aparams) { Init(); }

/**
* Class copy constructor.
*/
Account(Account &_account) {
state = _account.state;
// @todo: Copy entries.
}

/**
* Class deconstructor.
*/
~Account() {}

/* Serializers */

/**
* Returns serialized representation of the object instance.
*/
virtual SerializerNodeType Serialize(Serializer &_s) {
_s.PassStruct(THIS_REF, "state", state);
return SerializerNodeObject;
}
};
Loading

0 comments on commit 6ecae7f

Please sign in to comment.