From 6859c3a321e191639388b20bcc8a75c6a06dc0a7 Mon Sep 17 00:00:00 2001 From: Adrian Kierzkowski Date: Mon, 6 May 2024 21:39:30 +0200 Subject: [PATCH] Fixed problem with casting string to ENUM_LOG_LEVEL via indicator params' Get() and Set() methods. Also fixed formatting of files. --- EA.mqh | 25 +++++++++++++------------ Exchange/tests/Exchange.test.mq5 | 4 ++-- Log.mqh | 9 +++++++-- Strategy.mqh | 25 ++++++++++++------------- Trade.mqh | 15 ++++++++------- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/EA.mqh b/EA.mqh index 3d7bdb6d4..16cccca33 100644 --- a/EA.mqh +++ b/EA.mqh @@ -30,8 +30,8 @@ #define EA_MQH // Includes. -#include "Chart.mqh" #include "./Chart.struct.static.h" +#include "Chart.mqh" #include "Data.struct.h" #include "Dict.mqh" #include "DictObject.mqh" @@ -114,12 +114,12 @@ class EA : public Taskable { Init(); // Initialize a trade instance for the current chart and symbol. Ref _source = Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT); - TradeParams _tparams(0, 1.0f, 0, eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); + TradeParams _tparams(0, 1.0f, 0, (ENUM_LOG_LEVEL)eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); Trade _trade(_tparams, _source.Ptr()); trade.Set(_Symbol, _trade); logger.Link(_trade.GetLogger()); - logger.SetLevel(eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); - //_trade.GetLogger().SetLevel(eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); + logger.SetLevel((ENUM_LOG_LEVEL)eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); + //_trade.GetLogger().SetLevel((ENUM_LOG_LEVEL)eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); } /** @@ -409,11 +409,11 @@ class EA : public Taskable { _strat.OnOrderOpen(_oparams); // Send the request. _result = _trade.RequestSend(_request, _oparams); - if (!_result) { // && _strade.IsTradeRecommended( - logger.Debug( - StringFormat("Error while sending a trade request! Entry: %s", - SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString()), - __FUNCTION_LINE__, StringFormat("Code: %d, Msg: %s", _LastError, Terminal::GetErrorText(_LastError))); + if (!_result) { // && _strade.IsTradeRecommended( + logger.Debug( + StringFormat("Error while sending a trade request! Entry: %s", + SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString()), + __FUNCTION_LINE__, StringFormat("Code: %d, Msg: %s", _LastError, Terminal::GetErrorText(_LastError))); if (_trade.IsTradeRecommended()) { logger.Debug( StringFormat("Error while sending a trade request! Entry: %s", @@ -421,7 +421,8 @@ class EA : public Taskable { __FUNCTION_LINE__, StringFormat("Code: %d, Msg: %s", _LastError, Terminal::GetErrorText(_LastError))); } #ifdef __debug_ea__ - Print(__FUNCTION_LINE__ + "(): " + SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString()); + Print(__FUNCTION_LINE__ + + "(): " + SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString()); #endif } return _result; @@ -766,8 +767,8 @@ class EA : public Taskable { Ref _strat = ((SClass *)NULL).Init(_tf, THIS_PTR); _strat.Ptr().Set(STRAT_PARAM_ID, _magic_no); _strat.Ptr().Set(TRADE_PARAM_MAGIC_NO, _magic_no); - _strat.Ptr().Set(STRAT_PARAM_LOG_LEVEL, - eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); + _strat.Ptr().Set(STRAT_PARAM_LOG_LEVEL, + (ENUM_LOG_LEVEL)eparams.Get(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL))); _strat.Ptr().Set(STRAT_PARAM_TF, _tf); _strat.Ptr().Set(STRAT_PARAM_TYPE, _type); _strat.Ptr().OnInit(); diff --git a/Exchange/tests/Exchange.test.mq5 b/Exchange/tests/Exchange.test.mq5 index 498c0c791..797c44274 100644 --- a/Exchange/tests/Exchange.test.mq5 +++ b/Exchange/tests/Exchange.test.mq5 @@ -96,8 +96,8 @@ bool TestExchange01() { exchange REF_DEREF SymbolAdd(symbol02.Ptr(), "Symbol02"); // Attach instances of dummy trades. - Ref trade01 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT)); - Ref trade02 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT)); + Ref trade01 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, Period())); + Ref trade02 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, Period())); exchange REF_DEREF TradeAdd(trade01.Ptr(), "Trade01"); exchange REF_DEREF TradeAdd(trade02.Ptr(), "Trade02"); diff --git a/Log.mqh b/Log.mqh index c55c526d6..13bc13fd4 100644 --- a/Log.mqh +++ b/Log.mqh @@ -76,8 +76,7 @@ class Log : public Object { /** * Class copy constructor. */ - Log(const Log &_log) : filename(_log.filename), last_entry(_log.last_entry), log_level(_log.log_level) { - } + Log(const Log &_log) : filename(_log.filename), last_entry(_log.last_entry), log_level(_log.log_level) {} /** * Class deconstructor. @@ -345,4 +344,10 @@ bool Log::AddLastError(string prefix, long suffix) { return Add(V_ERROR, Terminal::GetLastErrorText(), prefix, StringFormat("%d", suffix)); } +// Specialization of StringToType() for enum. +void StringToType(string _value, ENUM_LOG_LEVEL &_out) { + // Maybe parse the string? + _out = V_NONE; +} + #endif diff --git a/Strategy.mqh b/Strategy.mqh index 115b7660a..79ab81eba 100644 --- a/Strategy.mqh +++ b/Strategy.mqh @@ -668,7 +668,7 @@ class Strategy : public Taskable { virtual void OnInit() { // Link log instances. logger.Link(trade.Ptr().GetLogger()); - trade.Ptr().GetLogger().SetLevel(sparams.Get(STRAT_PARAM_LOG_LEVEL)); + trade.Ptr().GetLogger().SetLevel((ENUM_LOG_LEVEL)sparams.Get(STRAT_PARAM_LOG_LEVEL)); // Sets strategy stops. SetStops(THIS_PTR, THIS_PTR); // trade.SetStrategy(&this); // @fixme @@ -926,12 +926,12 @@ class Strategy : public Taskable { bool _result = true; if (_method != 0) { int _shift = _method / 64; - if (METHOD(_method, 0)) _result &= !trade REF_DEREF HasBarOrder(_cmd, _shift); // 1 - if (METHOD(_method, 1)) _result &= IsTrend(_cmd); // 2 - if (METHOD(_method, 2)) _result &= trade REF_DEREF IsPivot(_cmd, _shift); // 4 - if (METHOD(_method, 3)) _result &= !trade REF_DEREF HasOrderOppositeType(_cmd); // 8 - if (METHOD(_method, 4)) _result &= trade REF_DEREF IsPeak(_cmd, _shift); // 16 - if (METHOD(_method, 5)) _result &= !trade REF_DEREF HasOrderBetter(_cmd); // 32 + if (METHOD(_method, 0)) _result &= !trade REF_DEREF HasBarOrder(_cmd, _shift); // 1 + if (METHOD(_method, 1)) _result &= IsTrend(_cmd); // 2 + if (METHOD(_method, 2)) _result &= trade REF_DEREF IsPivot(_cmd, _shift); // 4 + if (METHOD(_method, 3)) _result &= !trade REF_DEREF HasOrderOppositeType(_cmd); // 8 + if (METHOD(_method, 4)) _result &= trade REF_DEREF IsPeak(_cmd, _shift); // 16 + if (METHOD(_method, 5)) _result &= !trade REF_DEREF HasOrderBetter(_cmd); // 32 if (METHOD(_method, 6)) _result &= trade REF_DEREF CalcActiveProfitInValue() <= 0.0f; // 64 /* if (METHOD(_method, 6)) @@ -1054,15 +1054,14 @@ class Strategy : public Taskable { virtual bool SignalCloseFilter(ENUM_ORDER_TYPE _cmd, int _method = 0, int _shift = 0) { bool _result = _method == 0; if (_method != 0) { - if (METHOD(_method, 0)) _result |= _result || !trade REF_DEREF HasBarOrder(_cmd, _shift); // 1 - if (METHOD(_method, 1)) _result |= _result || !IsTrend(_cmd); // 2 + if (METHOD(_method, 1)) _result |= _result || !IsTrend(_cmd); // 2 if (METHOD(_method, 2)) _result |= _result || !trade REF_DEREF IsPivot(_cmd, _shift); // 4 if (METHOD(_method, 3)) - _result |= _result || Open[_shift] > High[_shift + 1] || Open[_shift] < Low[_shift + 1]; // 8 - if (METHOD(_method, 4)) _result |= _result || trade REF_DEREF IsPeak(_cmd, _shift); // 16 - if (METHOD(_method, 5)) _result |= _result || trade REF_DEREF HasOrderBetter(_cmd); // 32 - if (METHOD(_method, 6)) _result |= _result || trade REF_DEREF CalcActiveProfitInValue() > 0.0f; // 64 + _result |= _result || Open[_shift] > High[_shift + 1] || Open[_shift] < Low[_shift + 1]; // 8 + if (METHOD(_method, 4)) _result |= _result || trade REF_DEREF IsPeak(_cmd, _shift); // 16 + if (METHOD(_method, 5)) _result |= _result || trade REF_DEREF HasOrderBetter(_cmd); // 32 + if (METHOD(_method, 6)) _result |= _result || trade REF_DEREF CalcActiveProfitInValue() > 0.0f; // 64 /* if (METHOD(_method, 6)) _result |= diff --git a/Trade.mqh b/Trade.mqh index 4c4daf932..4ec0a0ec5 100644 --- a/Trade.mqh +++ b/Trade.mqh @@ -830,7 +830,7 @@ HistorySelect(0, TimeCurrent()); // Select history for access. // @todo: _order.IsPending()? _result &= orders_active.Set(_order PTR_DEREF Get(ORDER_PROP_TICKET), _order_ref); logger.Link(_order.GetLogger()); - _order PTR_DEREF GetLogger().SetLevel(tparams.Get(TRADE_PARAM_LOG_LEVEL)); + _order PTR_DEREF GetLogger().SetLevel((ENUM_LOG_LEVEL)tparams.Get(TRADE_PARAM_LOG_LEVEL)); } else { _result &= orders_history.Set(_order PTR_DEREF Get(ORDER_PROP_TICKET), _order_ref); } @@ -894,11 +894,11 @@ HistorySelect(0, TimeCurrent()); // Select history for access. OrderMoveToHistory(_order.Ptr()); order_last = _order; } else { - logger.Error( - StringFormat("Failed to close the order: %d! Error: %d (%s)", _order REF_DEREF Get(ORDER_PROP_TICKET), - _order REF_DEREF Get(ORDER_PROP_LAST_ERROR), - Terminal::GetErrorText(_order REF_DEREF Get(ORDER_PROP_LAST_ERROR))), - __FUNCTION_LINE__); + logger.Error(StringFormat("Failed to close the order: %d! Error: %d (%s)", + _order REF_DEREF Get(ORDER_PROP_TICKET), + _order REF_DEREF Get(ORDER_PROP_LAST_ERROR), + Terminal::GetErrorText(_order REF_DEREF Get(ORDER_PROP_LAST_ERROR))), + __FUNCTION_LINE__); continue; } } else { @@ -932,7 +932,8 @@ HistorySelect(0, TimeCurrent()); // Select history for access. order_last = _order; } else { logger.Error( - StringFormat("Failed to close the order: %d! Error: %d (%s)", _order REF_DEREF Get(ORDER_PROP_TICKET), + StringFormat("Failed to close the order: %d! Error: %d (%s)", + _order REF_DEREF Get(ORDER_PROP_TICKET), _order REF_DEREF Get(ORDER_PROP_LAST_ERROR), Terminal::GetErrorText(_order REF_DEREF Get(ORDER_PROP_LAST_ERROR))), __FUNCTION_LINE__);