Skip to content

Commit

Permalink
Workaround for closing order conditions after orders are loaded from …
Browse files Browse the repository at this point in the history
…active pool (GH-705)
  • Loading branch information
kenorb committed Apr 27, 2024
1 parent 35c5822 commit e4d95af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
15 changes: 14 additions & 1 deletion EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,21 @@ class EA {
* Loads existing trades for the given strategy.
*/
bool StrategyLoadTrades(Strategy *_strat) {
bool _result = true;
Trade *_trade = trade.GetByKey(_Symbol);
return _trade.OrdersLoadByMagic(_strat.Get<long>(STRAT_PARAM_ID));
// Load active trades.
_result &= _trade.OrdersLoadByMagic(_strat.Get<long>(STRAT_PARAM_ID));
// Load strategy-specific order parameters (e.g. conditions).
// This is a temporary workaround for GH-705.
// @todo: To move to Strategy class.
Ref<Order> _order;
for (DictStructIterator<long, Ref<Order>> iter = _trade.GetOrdersActive().Begin(); iter.IsValid(); ++iter) {
_order = iter.Value();
if (_order.IsSet() && _order.Ptr().IsOpen()) {
_strat.OnOrderLoad(_order.Ptr());
}
}
return _result;
}

/* Trade methods */
Expand Down
45 changes: 45 additions & 0 deletions Strategy.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,51 @@ class Strategy : public Object {
virtual void OnInit() {
SetStops(GetPointer(this), GetPointer(this));
// trade.SetStrategy(&this); // @fixme
// Sets strategy's trade spread limit.
trade.Set<float>(TRADE_PARAM_MAX_SPREAD, sparams.Get<float>(STRAT_PARAM_MAX_SPREAD));
// Load active trades.
if (Get<long>(STRAT_PARAM_ID) > 0) {
trade.OrdersLoadByMagic(Get<long>(STRAT_PARAM_ID));
}
Ref<Order> _order;
for (DictStructIterator<long, Ref<Order>> iter = trade.GetOrdersActive().Begin(); iter.IsValid(); ++iter) {
_order = iter.Value();
if (_order.IsSet() && _order.Ptr().IsOpen()) {
Strategy::OnOrderLoad(_order.Ptr());
}
}
}

/**
* Event on strategy's order load.
*
* @param
* _oparams Order parameters to update.
*/
virtual void OnOrderLoad(Order *_order) {
int _index = 0;
ENUM_TIMEFRAMES _stf = Get<ENUM_TIMEFRAMES>(STRAT_PARAM_TF);
unsigned int _stf_secs = ChartTf::TfToSeconds(_stf);
if (sparams.order_close_time != 0) {
long _close_time_arg = sparams.order_close_time > 0 ? sparams.order_close_time * 60
: (int)round(-sparams.order_close_time * _stf_secs);
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_LIFETIME_GT_ARG, _index);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _close_time_arg, _index);
_index++;
}
if (sparams.order_close_loss != 0.0f) {
float _loss_limit = sparams.order_close_loss;
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_IN_LOSS, _index);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _loss_limit, _index);
_index++;
}
if (sparams.order_close_profit != 0.0f) {
float _profit_limit = sparams.order_close_profit;
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_IN_PROFIT, _index);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _profit_limit, _index);
_index++;
}
_order.Set(ORDER_PARAM_UPDATE_FREQ, _stf_secs);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
OrderMoveToHistory(_order.Ptr());
order_last = _order;
} else {
logger.AddLastError(__FUNCTION_LINE__, _order.Ptr().Get<ulong>(ORDER_PROP_LAST_ERROR));
return -1;
logger.AddLastError(__FUNCTION_LINE__, _order.Ptr().Get<unsigned long>(ORDER_PROP_LAST_ERROR));
continue;
}
} else {
OrderMoveToHistory(_order.Ptr());
Expand Down

0 comments on commit e4d95af

Please sign in to comment.