From 067208bc9dab1f58c40c7abeab67ab1a11ac1ac6 Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Sat, 24 Aug 2019 00:10:35 +0300 Subject: [PATCH 1/2] make backtesting an attribute of Hyperopt --- freqtrade/optimize/hyperopt.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index cf4740f1c..62a6ab27b 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -37,7 +37,7 @@ INITIAL_POINTS = 30 MAX_LOSS = 100000 # just a big enough number to be bad result in loss optimization -class Hyperopt(Backtesting): +class Hyperopt: """ Hyperopt class, this class contains all the logic to run a hyperopt simulation @@ -46,7 +46,9 @@ class Hyperopt(Backtesting): hyperopt.start() """ def __init__(self, config: Dict[str, Any]) -> None: - super().__init__(config) + self.config = config + self.backtesting = Backtesting(self.config) + self.custom_hyperopt = HyperOptResolver(self.config).hyperopt self.custom_hyperoptloss = HyperOptLossResolver(self.config).hyperoptloss @@ -70,10 +72,10 @@ class Hyperopt(Backtesting): # Populate functions here (hasattr is slow so should not be run during "regular" operations) if hasattr(self.custom_hyperopt, 'populate_buy_trend'): - self.advise_buy = self.custom_hyperopt.populate_buy_trend # type: ignore + self.backtesting.advise_buy = self.custom_hyperopt.populate_buy_trend # type: ignore if hasattr(self.custom_hyperopt, 'populate_sell_trend'): - self.advise_sell = self.custom_hyperopt.populate_sell_trend # type: ignore + self.backtesting.advise_sell = self.custom_hyperopt.populate_sell_trend # type: ignore # Use max_open_trades for hyperopt as well, except --disable-max-market-positions is set if self.config.get('use_max_market_positions', True): @@ -249,22 +251,22 @@ class Hyperopt(Backtesting): """ params = self.get_args(_params) if self.has_space('roi'): - self.strategy.minimal_roi = self.custom_hyperopt.generate_roi_table(params) + self.backtesting.strategy.minimal_roi = self.custom_hyperopt.generate_roi_table(params) if self.has_space('buy'): - self.advise_buy = self.custom_hyperopt.buy_strategy_generator(params) + self.backtesting.advise_buy = self.custom_hyperopt.buy_strategy_generator(params) if self.has_space('sell'): - self.advise_sell = self.custom_hyperopt.sell_strategy_generator(params) + self.backtesting.advise_sell = self.custom_hyperopt.sell_strategy_generator(params) if self.has_space('stoploss'): - self.strategy.stoploss = params['stoploss'] + self.backtesting.strategy.stoploss = params['stoploss'] processed = load(self.tickerdata_pickle) min_date, max_date = get_timeframe(processed) - results = self.backtest( + results = self.backtesting.backtest( { 'stake_amount': self.config['stake_amount'], 'processed': processed, @@ -345,9 +347,9 @@ class Hyperopt(Backtesting): data = load_data( datadir=Path(self.config['datadir']) if self.config.get('datadir') else None, pairs=self.config['exchange']['pair_whitelist'], - ticker_interval=self.ticker_interval, + ticker_interval=self.backtesting.ticker_interval, refresh_pairs=self.config.get('refresh_pairs', False), - exchange=self.exchange, + exchange=self.backtesting.exchange, timerange=timerange ) @@ -364,15 +366,15 @@ class Hyperopt(Backtesting): (max_date - min_date).days ) - self.strategy.advise_indicators = \ + self.backtesting.strategy.advise_indicators = \ self.custom_hyperopt.populate_indicators # type: ignore - preprocessed = self.strategy.tickerdata_to_dataframe(data) + preprocessed = self.backtesting.strategy.tickerdata_to_dataframe(data) dump(preprocessed, self.tickerdata_pickle) # We don't need exchange instance anymore while running hyperopt - self.exchange = None # type: ignore + self.backtesting.exchange = None # type: ignore self.load_previous_results() From 667a6233108ab2a9ae53865493ee267d7b77921f Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Sat, 24 Aug 2019 00:10:55 +0300 Subject: [PATCH 2/2] adjust tests --- freqtrade/tests/optimize/test_hyperopt.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/freqtrade/tests/optimize/test_hyperopt.py b/freqtrade/tests/optimize/test_hyperopt.py index cfd75fd9d..7b525454c 100644 --- a/freqtrade/tests/optimize/test_hyperopt.py +++ b/freqtrade/tests/optimize/test_hyperopt.py @@ -429,7 +429,7 @@ def test_start_calls_optimizer(mocker, default_conf, caplog, capsys) -> None: 'hyperopt_jobs': 1, }) hyperopt = Hyperopt(default_conf) - hyperopt.strategy.tickerdata_to_dataframe = MagicMock() + hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock() hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.start() @@ -441,8 +441,8 @@ def test_start_calls_optimizer(mocker, default_conf, caplog, capsys) -> None: assert dumper.called # Should be called twice, once for tickerdata, once to save evaluations assert dumper.call_count == 2 - assert hasattr(hyperopt, "advise_sell") - assert hasattr(hyperopt, "advise_buy") + assert hasattr(hyperopt.backtesting, "advise_sell") + assert hasattr(hyperopt.backtesting, "advise_buy") assert hasattr(hyperopt, "max_open_trades") assert hyperopt.max_open_trades == default_conf['max_open_trades'] assert hasattr(hyperopt, "position_stacking") @@ -488,7 +488,7 @@ def test_populate_indicators(hyperopt) -> None: tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m') tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC", fill_missing=True)} - dataframes = hyperopt.strategy.tickerdata_to_dataframe(tickerlist) + dataframes = hyperopt.backtesting.strategy.tickerdata_to_dataframe(tickerlist) dataframe = hyperopt.custom_hyperopt.populate_indicators(dataframes['UNITTEST/BTC'], {'pair': 'UNITTEST/BTC'}) @@ -502,7 +502,7 @@ def test_buy_strategy_generator(hyperopt) -> None: tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m') tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC", fill_missing=True)} - dataframes = hyperopt.strategy.tickerdata_to_dataframe(tickerlist) + dataframes = hyperopt.backtesting.strategy.tickerdata_to_dataframe(tickerlist) dataframe = hyperopt.custom_hyperopt.populate_indicators(dataframes['UNITTEST/BTC'], {'pair': 'UNITTEST/BTC'}) @@ -538,7 +538,7 @@ def test_generate_optimizer(mocker, default_conf) -> None: backtest_result = pd.DataFrame.from_records(trades, columns=labels) mocker.patch( - 'freqtrade.optimize.hyperopt.Hyperopt.backtest', + 'freqtrade.optimize.hyperopt.Backtesting.backtest', MagicMock(return_value=backtest_result) ) mocker.patch( @@ -644,7 +644,7 @@ def test_print_json_spaces_all(mocker, default_conf, caplog, capsys) -> None: }) hyperopt = Hyperopt(default_conf) - hyperopt.strategy.tickerdata_to_dataframe = MagicMock() + hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock() hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.start() @@ -681,7 +681,7 @@ def test_print_json_spaces_roi_stoploss(mocker, default_conf, caplog, capsys) -> }) hyperopt = Hyperopt(default_conf) - hyperopt.strategy.tickerdata_to_dataframe = MagicMock() + hyperopt.backtesting.strategy.tickerdata_to_dataframe = MagicMock() hyperopt.custom_hyperopt.generate_roi_table = MagicMock(return_value={}) hyperopt.start()