Fix expired candle implementation

Improve and simplify test by passing the current time to the function
This commit is contained in:
Matthias 2021-01-07 07:51:49 +01:00
parent c0f170fdb9
commit b43ef474ad
2 changed files with 16 additions and 9 deletions

View File

@ -483,16 +483,16 @@ class IStrategy(ABC):
latest['date'], pair, str(buy), str(sell)) latest['date'], pair, str(buy), str(sell))
timeframe_seconds = timeframe_to_seconds(timeframe) timeframe_seconds = timeframe_to_seconds(timeframe)
if self.ignore_expired_candle(latest_date=latest_date, if self.ignore_expired_candle(latest_date=latest_date,
current_time=datetime.now(timezone.utc),
timeframe_seconds=timeframe_seconds, timeframe_seconds=timeframe_seconds,
buy=buy): buy=buy):
return False, sell return False, sell
return buy, 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: if self.ignore_buying_expired_candle and buy:
current_time = datetime.now(timezone.utc) - timedelta( time_delta = current_time - (latest_date + timedelta(seconds=timeframe_seconds))
seconds=self.ignore_buying_expired_candle_after)
time_delta = current_time - latest_date + timedelta(seconds=timeframe_seconds)
return time_delta.total_seconds() > self.ignore_buying_expired_candle_after return time_delta.total_seconds() > self.ignore_buying_expired_candle_after
else: else:
return False return False

View File

@ -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) 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'}) default_conf.update({'strategy': 'DefaultStrategy'})
strategy = StrategyResolver.load_strategy(default_conf) strategy = StrategyResolver.load_strategy(default_conf)
strategy.ignore_buying_expired_candle = True strategy.ignore_buying_expired_candle = True
strategy.ignore_buying_expired_candle_after = 60 strategy.ignore_buying_expired_candle_after = 60
ohlcv_history.loc[-1, 'date'] = arrow.utcnow() latest_date = datetime(2020, 12, 30, 7, 0, 0, tzinfo=timezone.utc)
# Take a copy to correctly modify the call # Add 1 candle length as the "latest date" defines candle open.
mocked_history = ohlcv_history.copy() current_time = latest_date + timedelta(seconds=80 + 300)
latest_date = mocked_history['date'].max()
assert strategy.ignore_expired_candle(latest_date=latest_date, assert strategy.ignore_expired_candle(latest_date=latest_date,
current_time=current_time,
timeframe_seconds=300, timeframe_seconds=300,
buy=True) is True 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): def test_assert_df_raise(mocker, caplog, ohlcv_history):
ohlcv_history.loc[1, 'date'] = arrow.utcnow().shift(minutes=-16) ohlcv_history.loc[1, 'date'] = arrow.utcnow().shift(minutes=-16)