From 914710625940d8ac95a886c6f43c2f001f69af76 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 11 Oct 2020 19:50:37 +0200 Subject: [PATCH] call bot_loop_start() in backtesting to allow setup-code to run --- docs/bot-basics.md | 5 +++-- freqtrade/optimize/backtesting.py | 3 +++ tests/optimize/test_backtesting.py | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/bot-basics.md b/docs/bot-basics.md index 44f493456..86fb18645 100644 --- a/docs/bot-basics.md +++ b/docs/bot-basics.md @@ -49,8 +49,9 @@ This loop will be repeated again and again until the bot is stopped. [backtesting](backtesting.md) or [hyperopt](hyperopt.md) do only part of the above logic, since most of the trading operations are fully simulated. * Load historic data for configured pairlist. -* Calculate indicators (calls `populate_indicators()`). -* Calls `populate_buy_trend()` and `populate_sell_trend()` +* Calls `bot_loop_start()` once. +* Calculate indicators (calls `populate_indicators()` once per pair). +* Calculate buy / sell signals (calls `populate_buy_trend()` and `populate_sell_trend()` once per pair) * Loops per candle simulating entry and exit points. * Generate backtest report output diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 87eb8cb05..cb021c9ff 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -26,6 +26,7 @@ from freqtrade.plugins.pairlistmanager import PairListManager from freqtrade.plugins.protectionmanager import ProtectionManager from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType +from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper logger = logging.getLogger(__name__) @@ -434,6 +435,8 @@ class Backtesting: logger.info("Running backtesting for Strategy %s", strat.get_strategy_name()) self._set_strategy(strat) + strategy_safe_wrapper(self.strategy.bot_loop_start, supress_error=True)() + # Use max_open_trades in backtesting, except --disable-max-market-positions is set if self.config.get('use_max_market_positions', True): # Must come from strategy config, as the strategy may modify this setting. diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 1f8f7cfd8..e55e166d9 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -350,6 +350,7 @@ def test_backtesting_start(default_conf, mocker, testdatadir, caplog) -> None: default_conf['timerange'] = '-1510694220' backtesting = Backtesting(default_conf) + backtesting.strategy.bot_loop_start = MagicMock() backtesting.start() # check the logs, that will contain the backtest result exists = [ @@ -359,6 +360,7 @@ def test_backtesting_start(default_conf, mocker, testdatadir, caplog) -> None: for line in exists: assert log_has(line, caplog) assert backtesting.strategy.dp._pairlists is not None + assert backtesting.strategy.bot_loop_start.call_count == 1 def test_backtesting_start_no_data(default_conf, mocker, caplog, testdatadir) -> None: