diff --git a/docs/hyperopt.md b/docs/hyperopt.md index 96f9ff177..c5a52553b 100644 --- a/docs/hyperopt.md +++ b/docs/hyperopt.md @@ -48,7 +48,7 @@ usage: freqtrade hyperopt [-h] [-v] [--logfile FILE] [-V] [-c PATH] [-d PATH] [--hyperopt-path PATH] [--eps] [--dmmp] [--enable-protections] [--dry-run-wallet DRY_RUN_WALLET] [-e INT] - [--spaces {all,buy,sell,roi,stoploss,trailing,default} [{all,buy,sell,roi,stoploss,trailing,default} ...]] + [--spaces {all,buy,sell,roi,stoploss,trailing,protection,default} [{all,buy,sell,roi,stoploss,trailing,protection,default} ...]] [--print-all] [--no-color] [--print-json] [-j JOBS] [--random-state INT] [--min-trades INT] [--hyperopt-loss NAME] [--disable-param-export] @@ -92,7 +92,7 @@ optional arguments: Starting balance, used for backtesting / hyperopt and dry-runs. -e INT, --epochs INT Specify number of epochs (default: 100). - --spaces {all,buy,sell,roi,stoploss,trailing,default} [{all,buy,sell,roi,stoploss,trailing,default} ...] + --spaces {all,buy,sell,roi,stoploss,trailing,protection,default} [{all,buy,sell,roi,stoploss,trailing,protection,default} ...] Specify which parameters to hyperopt. Space-separated list. --print-all Print all results, not only the best ones. @@ -576,7 +576,8 @@ Legal values are: * `roi`: just optimize the minimal profit table for your strategy * `stoploss`: search for the best stoploss value * `trailing`: search for the best trailing stop values -* `default`: `all` except `trailing` +* `protection`: search for the best protection parameters (read the [protections section](#optimizing-protections) on how to properly define these) +* `default`: `all` except `trailing` and `protection` * space-separated list of any of the above values for example `--spaces roi stoploss` The default Hyperopt Search Space, used when no `--space` command line option is specified, does not include the `trailing` hyperspace. We recommend you to run optimization for the `trailing` hyperspace separately, when the best parameters for other hyperspaces were found, validated and pasted into your custom strategy. diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index dbd72aca4..79ee94149 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1038,7 +1038,7 @@ class Exchange: logger.debug(f"Using Last {conf_strategy['price_side'].capitalize()} / Last Price") ticker = self.fetch_ticker(pair) ticker_rate = ticker[conf_strategy['price_side']] - if ticker['last']: + if ticker['last'] and ticker_rate: if side == 'buy' and ticker_rate > ticker['last']: balance = conf_strategy['ask_last_balance'] ticker_rate = ticker_rate + balance * (ticker['last'] - ticker_rate) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 901900121..e0b35df32 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -107,13 +107,25 @@ class Hyperopt: # Populate "fallback" functions here # (hasattr is slow so should not be run during "regular" operations) if hasattr(self.custom_hyperopt, 'populate_indicators'): - self.backtesting.strategy.advise_indicators = ( # type: ignore + logger.warning( + "DEPRECATED: Using `populate_indicators()` in the hyperopt file is deprecated. " + "Please move these methods to your strategy." + ) + self.backtesting.strategy.populate_indicators = ( # type: ignore self.custom_hyperopt.populate_indicators) # type: ignore if hasattr(self.custom_hyperopt, 'populate_buy_trend'): - self.backtesting.strategy.advise_buy = ( # type: ignore + logger.warning( + "DEPRECATED: Using `populate_buy_trend()` in the hyperopt file is deprecated. " + "Please move these methods to your strategy." + ) + self.backtesting.strategy.populate_buy_trend = ( # type: ignore self.custom_hyperopt.populate_buy_trend) # type: ignore if hasattr(self.custom_hyperopt, 'populate_sell_trend'): - self.backtesting.strategy.advise_sell = ( # type: ignore + logger.warning( + "DEPRECATED: Using `populate_sell_trend()` in the hyperopt file is deprecated. " + "Please move these methods to your strategy." + ) + self.backtesting.strategy.populate_sell_trend = ( # type: ignore self.custom_hyperopt.populate_sell_trend) # type: ignore # Use max_open_trades for hyperopt as well, except --disable-max-market-positions is set diff --git a/freqtrade/optimize/hyperopt_auto.py b/freqtrade/optimize/hyperopt_auto.py index 03f7dd21e..43e92d9c6 100644 --- a/freqtrade/optimize/hyperopt_auto.py +++ b/freqtrade/optimize/hyperopt_auto.py @@ -74,7 +74,7 @@ class HyperOptAuto(IHyperOpt): return self._get_indicator_space('sell', 'sell_indicator_space') def protection_space(self) -> List['Dimension']: - return self._get_indicator_space('protection', 'indicator_space') + return self._get_indicator_space('protection', 'protection_space') def generate_roi_table(self, params: Dict) -> Dict[int, float]: return self._get_func('generate_roi_table')(params) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 27eeed39b..f6ac4c459 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -1851,6 +1851,31 @@ def test_get_sell_rate(default_conf, mocker, caplog, side, bid, ask, assert log_has("Using cached sell rate for ETH/BTC.", caplog) +@pytest.mark.parametrize("entry,side,ask,bid,last,last_ab,expected", [ + ('buy', 'ask', None, 4, 4, 0, 4), # ask not available + ('buy', 'ask', None, None, 4, 0, 4), # ask not available + ('buy', 'bid', 6, None, 4, 0, 5), # bid not available + ('buy', 'bid', None, None, 4, 0, 5), # No rate available + ('sell', 'ask', None, 4, 4, 0, 4), # ask not available + ('sell', 'ask', None, None, 4, 0, 4), # ask not available + ('sell', 'bid', 6, None, 4, 0, 5), # bid not available + ('sell', 'bid', None, None, 4, 0, 5), # bid not available +]) +def test_get_ticker_rate_error(mocker, entry, default_conf, caplog, side, ask, bid, + last, last_ab, expected) -> None: + caplog.set_level(logging.DEBUG) + default_conf['bid_strategy']['ask_last_balance'] = last_ab + default_conf['bid_strategy']['price_side'] = side + default_conf['ask_strategy']['price_side'] = side + default_conf['ask_strategy']['ask_last_balance'] = last_ab + exchange = get_patched_exchange(mocker, default_conf) + mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', + return_value={'ask': ask, 'last': last, 'bid': bid}) + + with pytest.raises(PricingError): + exchange.get_rate('ETH/BTC', refresh=True, side=entry) + + @pytest.mark.parametrize('side,expected', [ ('bid', 0.043936), # Value from order_book_l2 fiture - bids side ('ask', 0.043949), # Value from order_book_l2 fiture - asks side diff --git a/tests/optimize/test_hyperopt_tools.py b/tests/optimize/test_hyperopt_tools.py index cbcb13384..e845b2a77 100644 --- a/tests/optimize/test_hyperopt_tools.py +++ b/tests/optimize/test_hyperopt_tools.py @@ -64,34 +64,53 @@ def test_load_previous_results2(mocker, testdatadir, caplog) -> None: @pytest.mark.parametrize("spaces, expected_results", [ (['buy'], - {'buy': True, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False}), + {'buy': True, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False, + 'protection': False}), (['sell'], - {'buy': False, 'sell': True, 'roi': False, 'stoploss': False, 'trailing': False}), + {'buy': False, 'sell': True, 'roi': False, 'stoploss': False, 'trailing': False, + 'protection': False}), (['roi'], - {'buy': False, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False}), + {'buy': False, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False, + 'protection': False}), (['stoploss'], - {'buy': False, 'sell': False, 'roi': False, 'stoploss': True, 'trailing': False}), + {'buy': False, 'sell': False, 'roi': False, 'stoploss': True, 'trailing': False, + 'protection': False}), (['trailing'], - {'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': True}), + {'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': True, + 'protection': False}), (['buy', 'sell', 'roi', 'stoploss'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, + 'protection': False}), (['buy', 'sell', 'roi', 'stoploss', 'trailing'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, + 'protection': False}), (['buy', 'roi'], - {'buy': True, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False}), + {'buy': True, 'sell': False, 'roi': True, 'stoploss': False, 'trailing': False, + 'protection': False}), (['all'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, + 'protection': True}), (['default'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, + 'protection': False}), (['default', 'trailing'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, + 'protection': False}), (['all', 'buy'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, + 'protection': True}), (['default', 'buy'], - {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False}), + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': False, + 'protection': False}), + (['all'], + {'buy': True, 'sell': True, 'roi': True, 'stoploss': True, 'trailing': True, + 'protection': True}), + (['protection'], + {'buy': False, 'sell': False, 'roi': False, 'stoploss': False, 'trailing': False, + 'protection': True}), ]) def test_has_space(hyperopt_conf, spaces, expected_results): - for s in ['buy', 'sell', 'roi', 'stoploss', 'trailing']: + for s in ['buy', 'sell', 'roi', 'stoploss', 'trailing', 'protection']: hyperopt_conf.update({'spaces': spaces}) assert HyperoptTools.has_space(hyperopt_conf, s) == expected_results[s]