Skip to content

Commit

Permalink
Fixed problem with casting string to ENUM_LOG_LEVEL via indicator par…
Browse files Browse the repository at this point in the history
…ams' Get<T>() and Set<T>() methods. Also fixed formatting of files.
  • Loading branch information
nseam committed May 6, 2024
1 parent 9a9d453 commit 6859c3a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
25 changes: 13 additions & 12 deletions EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -114,12 +114,12 @@ class EA : public Taskable<DataParamEntry> {
Init();
// Initialize a trade instance for the current chart and symbol.
Ref<IndicatorBase> _source = Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT);
TradeParams _tparams(0, 1.0f, 0, eparams.Get<ENUM_LOG_LEVEL>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
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());
trade.Set(_Symbol, _trade);
logger.Link(_trade.GetLogger());
logger.SetLevel(eparams.Get<ENUM_LOG_LEVEL>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
//_trade.GetLogger().SetLevel(eparams.Get<ENUM_LOG_LEVEL>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
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 @@ -409,19 +409,20 @@ class EA : public Taskable<DataParamEntry> {
_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<SerializerJson>()),
__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<SerializerJson>()),
__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",
SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString<SerializerJson>()),
__FUNCTION_LINE__, StringFormat("Code: %d, Msg: %s", _LastError, Terminal::GetErrorText(_LastError)));
}
#ifdef __debug_ea__
Print(__FUNCTION_LINE__ + "(): " + SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString<SerializerJson>());
Print(__FUNCTION_LINE__ +
"(): " + SerializerConverter::FromObject(MqlTradeRequestProxy(_request)).ToString<SerializerJson>());
#endif
}
return _result;
Expand Down Expand Up @@ -766,8 +767,8 @@ class EA : public Taskable<DataParamEntry> {
Ref<Strategy> _strat = ((SClass *)NULL).Init(_tf, THIS_PTR);
_strat.Ptr().Set<long>(STRAT_PARAM_ID, _magic_no);
_strat.Ptr().Set<long>(TRADE_PARAM_MAGIC_NO, _magic_no);
_strat.Ptr().Set<ENUM_LOG_LEVEL>(STRAT_PARAM_LOG_LEVEL,
eparams.Get<ENUM_LOG_LEVEL>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
_strat.Ptr().Set<int>(STRAT_PARAM_LOG_LEVEL,
(ENUM_LOG_LEVEL)eparams.Get<int>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_LOG_LEVEL)));
_strat.Ptr().Set<ENUM_TIMEFRAMES>(STRAT_PARAM_TF, _tf);
_strat.Ptr().Set<int>(STRAT_PARAM_TYPE, _type);
_strat.Ptr().OnInit();
Expand Down
4 changes: 2 additions & 2 deletions Exchange/tests/Exchange.test.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ bool TestExchange01() {
exchange REF_DEREF SymbolAdd(symbol02.Ptr(), "Symbol02");

// Attach instances of dummy trades.
Ref<TradeDummy> trade01 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT));
Ref<TradeDummy> trade02 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_CURRENT));
Ref<TradeDummy> trade01 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, Period()));
Ref<TradeDummy> trade02 = new TradeDummy(Platform::FetchDefaultCandleIndicator(_Symbol, Period()));

exchange REF_DEREF TradeAdd(trade01.Ptr(), "Trade01");
exchange REF_DEREF TradeAdd(trade02.Ptr(), "Trade02");
Expand Down
9 changes: 7 additions & 2 deletions Log.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
25 changes: 12 additions & 13 deletions Strategy.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ class Strategy : public Taskable<DataParamEntry> {
virtual void OnInit() {
// Link log instances.
logger.Link(trade.Ptr().GetLogger());
trade.Ptr().GetLogger().SetLevel(sparams.Get<ENUM_LOG_LEVEL>(STRAT_PARAM_LOG_LEVEL));
trade.Ptr().GetLogger().SetLevel((ENUM_LOG_LEVEL)sparams.Get<int>(STRAT_PARAM_LOG_LEVEL));
// Sets strategy stops.
SetStops(THIS_PTR, THIS_PTR);
// trade.SetStrategy(&this); // @fixme
Expand Down Expand Up @@ -926,12 +926,12 @@ class Strategy : public Taskable<DataParamEntry> {
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))
Expand Down Expand Up @@ -1054,15 +1054,14 @@ class Strategy : public Taskable<DataParamEntry> {
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 |=
Expand Down
15 changes: 8 additions & 7 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
// @todo: _order.IsPending()?
_result &= orders_active.Set(_order PTR_DEREF Get<long>(ORDER_PROP_TICKET), _order_ref);
logger.Link(_order.GetLogger());
_order PTR_DEREF GetLogger().SetLevel(tparams.Get<ENUM_LOG_LEVEL>(TRADE_PARAM_LOG_LEVEL));
_order PTR_DEREF GetLogger().SetLevel((ENUM_LOG_LEVEL)tparams.Get<int>(TRADE_PARAM_LOG_LEVEL));
} else {
_result &= orders_history.Set(_order PTR_DEREF Get<long>(ORDER_PROP_TICKET), _order_ref);
}
Expand Down Expand Up @@ -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<long>(ORDER_PROP_TICKET),
_order REF_DEREF Get<unsigned int>(ORDER_PROP_LAST_ERROR),
Terminal::GetErrorText(_order REF_DEREF Get<unsigned int>(ORDER_PROP_LAST_ERROR))),
__FUNCTION_LINE__);
logger.Error(StringFormat("Failed to close the order: %d! Error: %d (%s)",
_order REF_DEREF Get<long>(ORDER_PROP_TICKET),
_order REF_DEREF Get<unsigned int>(ORDER_PROP_LAST_ERROR),
Terminal::GetErrorText(_order REF_DEREF Get<unsigned int>(ORDER_PROP_LAST_ERROR))),
__FUNCTION_LINE__);
continue;
}
} else {
Expand Down Expand Up @@ -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<long>(ORDER_PROP_TICKET),
StringFormat("Failed to close the order: %d! Error: %d (%s)",
_order REF_DEREF Get<long>(ORDER_PROP_TICKET),
_order REF_DEREF Get<unsigned int>(ORDER_PROP_LAST_ERROR),
Terminal::GetErrorText(_order REF_DEREF Get<unsigned int>(ORDER_PROP_LAST_ERROR))),
__FUNCTION_LINE__);
Expand Down

0 comments on commit 6859c3a

Please sign in to comment.