Skip to content

Commit

Permalink
Trade: Improves performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed May 22, 2023
1 parent aab0a45 commit b1c1afe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Strategy.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
20 changes: 17 additions & 3 deletions Trade.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit b1c1afe

Please sign in to comment.