From b43ef474ad231c1ac9cf87c760107ba7a5292a04 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 7 Jan 2021 07:51:49 +0100 Subject: [PATCH] Fix expired candle implementation Improve and simplify test by passing the current time to the function --- freqtrade/strategy/interface.py | 8 ++++---- tests/strategy/test_interface.py | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index a18c6c915..b9d05f64f 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -483,16 +483,16 @@ class IStrategy(ABC): latest['date'], pair, str(buy), str(sell)) timeframe_seconds = timeframe_to_seconds(timeframe) if self.ignore_expired_candle(latest_date=latest_date, + current_time=datetime.now(timezone.utc), timeframe_seconds=timeframe_seconds, buy=buy): return False, sell return buy, sell - def ignore_expired_candle(self, latest_date: datetime, timeframe_seconds: int, buy: bool): + def ignore_expired_candle(self, latest_date: datetime, current_time: datetime, + timeframe_seconds: int, buy: bool): if self.ignore_buying_expired_candle and buy: - current_time = datetime.now(timezone.utc) - timedelta( - seconds=self.ignore_buying_expired_candle_after) - time_delta = current_time - latest_date + timedelta(seconds=timeframe_seconds) + time_delta = current_time - (latest_date + timedelta(seconds=timeframe_seconds)) return time_delta.total_seconds() > self.ignore_buying_expired_candle_after else: return False diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index d7d113a4e..a3969d91b 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -106,21 +106,28 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history): assert log_has('Outdated history for pair xyz. Last tick is 16 minutes old', caplog) -def test_ignore_expired_candle(default_conf, ohlcv_history): +def test_ignore_expired_candle(default_conf): default_conf.update({'strategy': 'DefaultStrategy'}) strategy = StrategyResolver.load_strategy(default_conf) strategy.ignore_buying_expired_candle = True strategy.ignore_buying_expired_candle_after = 60 - ohlcv_history.loc[-1, 'date'] = arrow.utcnow() - # Take a copy to correctly modify the call - mocked_history = ohlcv_history.copy() - latest_date = mocked_history['date'].max() + latest_date = datetime(2020, 12, 30, 7, 0, 0, tzinfo=timezone.utc) + # Add 1 candle length as the "latest date" defines candle open. + current_time = latest_date + timedelta(seconds=80 + 300) assert strategy.ignore_expired_candle(latest_date=latest_date, + current_time=current_time, timeframe_seconds=300, buy=True) is True + current_time = latest_date + timedelta(seconds=30 + 300) + + assert not strategy.ignore_expired_candle(latest_date=latest_date, + current_time=current_time, + timeframe_seconds=300, + buy=True) is True + def test_assert_df_raise(mocker, caplog, ohlcv_history): ohlcv_history.loc[1, 'date'] = arrow.utcnow().shift(minutes=-16)