diff --git a/Strategy.mqh b/Strategy.mqh index 6b38f4784..75007bce1 100644 --- a/Strategy.mqh +++ b/Strategy.mqh @@ -992,7 +992,7 @@ class Strategy : public Object { } if (METHOD(_method_abs, 5)) { // 32 // Process bar open price ticks. - _val = last_tick.time < trade.GetChart().GetBarTime(); + _val = last_tick.time < trade.GetChart().GetBarTime(); // @todo: Improve performance. _res = _method > 0 ? _res & _val : _res | _val; } if (METHOD(_method_abs, 6)) { // 64 diff --git a/Trade.mqh b/Trade.mqh index ef9f07479..c55a92caa 100644 --- a/Trade.mqh +++ b/Trade.mqh @@ -1344,9 +1344,8 @@ HistorySelect(0, TimeCurrent()); // Select history for access. * */ void UpdateStates(bool _force = false) { - static datetime _last_check = 0; - if (_force || _last_check + 60 < TimeCurrent()) { - static unsigned int _states_prev = tstates.GetStates(); + if (_force || tstates.GetLastCheckDiff() > 60) { + unsigned int _states_prev = tstates.GetStates(); // Infrequent checks (each minute). /* Limit checks */ tstates.SetState(TRADE_STATE_PERIOD_LIMIT_REACHED, tparams.IsLimitGe(tstats)); @@ -1378,7 +1377,6 @@ HistorySelect(0, TimeCurrent()); // Select history for access. // Check the permission to trade for the current account. && !Account::IsTradeAllowed()); tstates.SetState(TRADE_STATE_TRADE_TERMINAL_BUSY, Terminal::IsTradeContextBusy()); - _last_check = TimeCurrent(); /* Terminal checks */ // Check if terminal is connected. tstates.SetState(TRADE_STATE_TRADE_TERMINAL_OFFLINE, Terminal::IsRealtime() && !Terminal::IsConnected()); diff --git a/Trade.struct.h b/Trade.struct.h index 5f79eb5aa..13ce9724a 100644 --- a/Trade.struct.h +++ b/Trade.struct.h @@ -106,6 +106,7 @@ struct TradeParams { return limits_stats[(int)_type][(int)_period] > 0 && _value >= limits_stats[(int)_type][(int)_period]; } bool IsLimitGe(TradeStats &_stats) { + // @todo: Improve code performance. for (ENUM_TRADE_STAT_TYPE t = 0; t < FINAL_ENUM_TRADE_STAT_TYPE; t++) { for (ENUM_TRADE_STAT_PERIOD p = 0; p < FINAL_ENUM_TRADE_STAT_PERIOD; p++) { unsigned int _stat_value = _stats.GetOrderStats(t, p); @@ -286,12 +287,22 @@ struct TradeStats { /* Structure for trade states. */ struct TradeStates { protected: - unsigned int states; // @todo: Move to protected. + datetime last_check; + unsigned int states; + + protected: + // Protected methods. + void UpdateCheck() { + // Refresh timestamp for the last access. + last_check = TimeCurrent(); + } + public: // Struct constructor. - TradeStates() : states(0) {} + TradeStates() : last_check(0), states(0) {} // Getters. bool Get(ENUM_TRADE_STATE _prop) { return CheckState(_prop); } + int GetLastCheckDiff() { return (int)(TimeCurrent() - last_check); } static string GetStateMessage(ENUM_TRADE_STATE _state) { switch (_state) { case TRADE_STATE_BARS_NOT_ENOUGH: @@ -329,7 +340,10 @@ struct TradeStates { } return "Unknown!"; } - unsigned int GetStates() { return states; } + unsigned int GetStates() { + UpdateCheck(); + return states; + } // Struct methods for bitwise operations. bool CheckState(unsigned int _states) { return (states & _states) != 0 || states == _states; } bool CheckStatesAll(unsigned int _states) { return (states & _states) == _states; }