From f1993fb2f48b986caef0372f1d6e9e0fe39f3c29 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 18 Jun 2020 08:01:09 +0200 Subject: [PATCH] Pass analyzed dataframe to get_signal --- freqtrade/freqtradebot.py | 8 ++++-- freqtrade/strategy/interface.py | 8 ++---- tests/conftest.py | 2 +- tests/strategy/test_interface.py | 43 ++++++-------------------------- 4 files changed, 16 insertions(+), 45 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 09b794a99..59f4447d7 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -424,7 +424,8 @@ class FreqtradeBot: return False # running get_signal on historical data fetched - (buy, sell) = self.strategy.get_signal(pair, self.strategy.timeframe) + analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(pair, self.strategy.timeframe) + (buy, sell) = self.strategy.get_signal(pair, self.strategy.timeframe, analyzed_df) if buy and not sell: stake_amount = self.get_trade_stake_amount(pair) @@ -705,7 +706,10 @@ class FreqtradeBot: if (config_ask_strategy.get('use_sell_signal', True) or config_ask_strategy.get('ignore_roi_if_buy_signal', False)): - (buy, sell) = self.strategy.get_signal(trade.pair, self.strategy.timeframe) + analyzed_df, _ = self.dataprovider.get_analyzed_dataframe(trade.pair, + self.strategy.timeframe) + + (buy, sell) = self.strategy.get_signal(trade.pair, self.strategy.timeframe, analyzed_df) if config_ask_strategy.get('use_order_book', False): order_book_min = config_ask_strategy.get('order_book_min', 1) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 279453920..969259446 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -406,19 +406,15 @@ class IStrategy(ABC): else: raise StrategyError(f"Dataframe returned from strategy has mismatching {message}.") - def get_signal(self, pair: str, timeframe: str) -> Tuple[bool, bool]: + def get_signal(self, pair: str, timeframe: str, dataframe: DataFrame) -> Tuple[bool, bool]: """ Calculates current signal based based on the buy / sell columns of the dataframe. Used by Bot to get the signal to buy or sell :param pair: pair in format ANT/BTC :param timeframe: timeframe to use + :param dataframe: Analyzed dataframe to get signal from. :return: (Buy, Sell) A bool-tuple indicating buy/sell signal """ - if not self.dp: - raise OperationalException("DataProvider not found.") - - dataframe, _ = self.dp.get_analyzed_dataframe(pair, timeframe) - if not isinstance(dataframe, DataFrame) or dataframe.empty: logger.warning(f'Empty candle (OHLCV) data for pair {pair}') return False, False diff --git a/tests/conftest.py b/tests/conftest.py index c64f443bc..8e8c1bfaa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -163,7 +163,7 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None: :param value: which value IStrategy.get_signal() must return :return: None """ - freqtrade.strategy.get_signal = lambda e, s: value + freqtrade.strategy.get_signal = lambda e, s, x: value freqtrade.exchange.refresh_latest_ohlcv = lambda p: None diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 835465f38..70ae067d7 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -31,40 +31,15 @@ def test_returns_latest_signal(mocker, default_conf, ohlcv_history): mocked_history['buy'] = 0 mocked_history.loc[1, 'sell'] = 1 - mocker.patch.object( - _STRATEGY.dp, 'get_analyzed_dataframe', - return_value=(mocked_history, 0) - ) - - assert _STRATEGY.get_signal('ETH/BTC', '5m') == (False, True) + assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (False, True) mocked_history.loc[1, 'sell'] = 0 mocked_history.loc[1, 'buy'] = 1 - mocker.patch.object( - _STRATEGY.dp, 'get_analyzed_dataframe', - return_value=(mocked_history, 0) - ) - assert _STRATEGY.get_signal('ETH/BTC', '5m') == (True, False) + assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (True, False) mocked_history.loc[1, 'sell'] = 0 mocked_history.loc[1, 'buy'] = 0 - mocker.patch.object( - _STRATEGY.dp, 'get_analyzed_dataframe', - return_value=(mocked_history, 0) - ) - assert _STRATEGY.get_signal('ETH/BTC', '5m') == (False, False) - - -def test_trade_no_dataprovider(default_conf, mocker, caplog): - strategy = DefaultStrategy({}) - # Delete DP for sure (suffers from test leakage, as this is updated in the base class) - if strategy.dp is not None: - strategy.dp = None - with pytest.raises(OperationalException, match="DataProvider not found."): - strategy.get_signal('ETH/BTC', '5m') - - with pytest.raises(OperationalException, match="DataProvider not found."): - strategy.analyze_pair('ETH/BTC') + assert _STRATEGY.get_signal('ETH/BTC', '5m', mocked_history) == (False, False) def test_analyze_pair_empty(default_conf, mocker, caplog, ohlcv_history): @@ -81,18 +56,15 @@ def test_analyze_pair_empty(default_conf, mocker, caplog, ohlcv_history): def test_get_signal_empty(default_conf, mocker, caplog): - mocker.patch.object(_STRATEGY.dp, 'get_analyzed_dataframe', return_value=(DataFrame(), 0)) - assert (False, False) == _STRATEGY.get_signal('foo', default_conf['timeframe']) + assert (False, False) == _STRATEGY.get_signal('foo', default_conf['timeframe'], DataFrame()) assert log_has('Empty candle (OHLCV) data for pair foo', caplog) caplog.clear() - mocker.patch.object(_STRATEGY.dp, 'get_analyzed_dataframe', return_value=(None, 0)) - assert (False, False) == _STRATEGY.get_signal('bar', default_conf['timeframe']) + assert (False, False) == _STRATEGY.get_signal('bar', default_conf['timeframe'], None) assert log_has('Empty candle (OHLCV) data for pair bar', caplog) caplog.clear() - mocker.patch.object(_STRATEGY.dp, 'get_analyzed_dataframe', return_value=(DataFrame([]), 0)) - assert (False, False) == _STRATEGY.get_signal('baz', default_conf['timeframe']) + assert (False, False) == _STRATEGY.get_signal('baz', default_conf['timeframe'], DataFrame([])) assert log_has('Empty candle (OHLCV) data for pair baz', caplog) @@ -126,10 +98,9 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history): mocked_history.loc[1, 'buy'] = 1 caplog.set_level(logging.INFO) - mocker.patch.object(_STRATEGY.dp, 'get_analyzed_dataframe', return_value=(mocked_history, 0)) mocker.patch.object(_STRATEGY, 'assert_df') - assert (False, False) == _STRATEGY.get_signal('xyz', default_conf['timeframe']) + assert (False, False) == _STRATEGY.get_signal('xyz', default_conf['timeframe'], mocked_history) assert log_has('Outdated history for pair xyz. Last tick is 16 minutes old', caplog)