diff --git a/docs/strategy-advanced.md b/docs/strategy-advanced.md index 69e2256a1..aba834c55 100644 --- a/docs/strategy-advanced.md +++ b/docs/strategy-advanced.md @@ -89,3 +89,30 @@ class Awesomestrategy(IStrategy): return True return False ``` + +## Bot loop start callback + +A simple callback which is called at the start of every bot iteration. +This can be used to perform calculations which are pair independent. + + +``` python +import requests + +class Awesomestrategy(IStrategy): + + # ... populate_* methods + + def bot_loop_start(self, **kwargs) -> None: + """ + Called at the start of the bot iteration (one loop). + Might be used to perform pair-independent tasks + (e.g. gather some remote ressource for comparison) + :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. + """ + if self.config['runmode'].value in ('live', 'dry_run'): + # Assign this to the class by using self.* + # can then be used by populate_* methods + self.remote_data = requests.get('https://some_remote_source.example.com') + +``` diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 3ad0d061a..a69178691 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -151,6 +151,8 @@ class FreqtradeBot: self.dataprovider.refresh(self.pairlists.create_pair_list(self.active_pair_whitelist), self.strategy.informative_pairs()) + strategy_safe_wrapper(self.strategy.bot_loop_start)() + self.strategy.analyze(self.active_pair_whitelist) with self._sell_lock: diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 4483e1e8f..2ee567c20 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -194,7 +194,7 @@ class IStrategy(ABC): """ Called at the start of the bot iteration (one loop). Might be used to perform pair-independent tasks - (e.g. like lock pairs with negative profit in the last hour) + (e.g. gather some remote ressource for comparison) :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. """ pass diff --git a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 index 0ca35e117..9af086c77 100644 --- a/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 +++ b/freqtrade/templates/subtemplates/strategy_methods_advanced.j2 @@ -1,4 +1,13 @@ +def bot_loop_start(self, **kwargs) -> None: + """ + Called at the start of the bot iteration (one loop). + Might be used to perform pair-independent tasks + (e.g. gather some remote ressource for comparison) + :param **kwargs: Ensure to keep this here so updates to this won't break your strategy. + """ + pass + def check_buy_timeout(self, pair: str, trade: 'Trade', order: dict, **kwargs) -> bool: """ Check buy timeout function callback. diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 94437d373..a50b4e1db 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -57,6 +57,9 @@ def test_returns_latest_signal(mocker, default_conf, ohlcv_history): def test_trade_no_dataprovider(default_conf, mocker, caplog): strategy = DefaultStrategy({}) + # Delete DP for sure (suffers from test leakage, as we update this in the base class) + if strategy.dp: + del strategy.dp with pytest.raises(OperationalException, match="DataProvider not found."): strategy.get_signal('ETH/BTC', '5m') @@ -418,6 +421,14 @@ def test_is_pair_locked(default_conf): assert not strategy.is_pair_locked(pair) +def test_is_informative_pairs_callback(default_conf): + default_conf.update({'strategy': 'TestStrategyLegacy'}) + strategy = StrategyResolver.load_strategy(default_conf) + # Should return empty + # Uses fallback to base implementation + assert [] == strategy.informative_pairs() + + @pytest.mark.parametrize('error', [ ValueError, KeyError, Exception, ])