def test_generate_optimizer(mocker, hyperopt_conf) -> None: hyperopt_conf.update({ 'spaces': 'all', 'hyperopt_min_trades': 1, }) backtest_result = { 'results': pd.DataFrame({ "pair": ["UNITTEST/BTC", "UNITTEST/BTC", "UNITTEST/BTC", "UNITTEST/BTC"], "profit_ratio": [0.003312, 0.010801, 0.013803, 0.002780], "profit_abs": [0.000003, 0.000011, 0.000014, 0.000003], "open_date": [ Arrow(2017, 11, 14, 19, 32, 00).datetime, Arrow(2017, 11, 14, 21, 36, 00).datetime, Arrow(2017, 11, 14, 22, 12, 00).datetime, Arrow(2017, 11, 14, 22, 44, 00).datetime ], "close_date": [ Arrow(2017, 11, 14, 21, 35, 00).datetime, Arrow(2017, 11, 14, 22, 10, 00).datetime, Arrow(2017, 11, 14, 22, 43, 00).datetime, Arrow(2017, 11, 14, 22, 58, 00).datetime ], "open_rate": [0.002543, 0.003003, 0.003089, 0.003214], "close_rate": [0.002546, 0.003014, 0.003103, 0.003217], "trade_duration": [123, 34, 31, 14], "is_open": [False, False, False, True], "stake_amount": [0.01, 0.01, 0.01, 0.01], "sell_reason": [ SellType.ROI, SellType.STOP_LOSS, SellType.ROI, SellType.FORCE_SELL ] }), 'config': hyperopt_conf, 'locks': [], 'final_balance': 1000, } mocker.patch('freqtrade.optimize.hyperopt.Backtesting.backtest', return_value=backtest_result) mocker.patch('freqtrade.optimize.hyperopt.get_timerange', return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13))) patch_exchange(mocker) mocker.patch('freqtrade.optimize.hyperopt.load', return_value={'XRP/BTC': None}) optimizer_param = { 'adx-value': 0, 'fastd-value': 35, 'mfi-value': 0, 'rsi-value': 0, 'adx-enabled': False, 'fastd-enabled': True, 'mfi-enabled': False, 'rsi-enabled': False, 'trigger': 'macd_cross_signal', 'sell-adx-value': 0, 'sell-fastd-value': 75, 'sell-mfi-value': 0, 'sell-rsi-value': 0, 'sell-adx-enabled': False, 'sell-fastd-enabled': True, 'sell-mfi-enabled': False, 'sell-rsi-enabled': False, 'sell-trigger': 'macd_cross_signal', 'roi_t1': 60.0, 'roi_t2': 30.0, 'roi_t3': 20.0, 'roi_p1': 0.01, 'roi_p2': 0.01, 'roi_p3': 0.1, 'stoploss': -0.4, 'trailing_stop': True, 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset_p1': 0.05, 'trailing_only_offset_is_reached': False, } response_expected = { 'loss': 1.9147239021396234, 'results_explanation': (' 4 trades. 4/0/0 Wins/Draws/Losses. ' 'Avg profit 0.77%. Median profit 0.71%. Total profit ' '0.00003100 BTC ( 0.00\N{GREEK CAPITAL LETTER SIGMA}%). ' 'Avg duration 0:50:00 min.').encode(locale.getpreferredencoding(), 'replace').decode('utf-8'), 'params_details': { 'buy': { 'adx-enabled': False, 'adx-value': 0, 'fastd-enabled': True, 'fastd-value': 35, 'mfi-enabled': False, 'mfi-value': 0, 'rsi-enabled': False, 'rsi-value': 0, 'trigger': 'macd_cross_signal' }, 'roi': { 0: 0.12000000000000001, 20.0: 0.02, 50.0: 0.01, 110.0: 0 }, 'sell': { 'sell-adx-enabled': False, 'sell-adx-value': 0, 'sell-fastd-enabled': True, 'sell-fastd-value': 75, 'sell-mfi-enabled': False, 'sell-mfi-value': 0, 'sell-rsi-enabled': False, 'sell-rsi-value': 0, 'sell-trigger': 'macd_cross_signal' }, 'stoploss': { 'stoploss': -0.4 }, 'trailing': { 'trailing_only_offset_is_reached': False, 'trailing_stop': True, 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset': 0.07 } }, 'params_dict': optimizer_param, 'params_not_optimized': { 'buy': {}, 'sell': {} }, 'results_metrics': ANY, 'total_profit': 3.1e-08 } hyperopt = Hyperopt(hyperopt_conf) hyperopt.min_date = Arrow(2017, 12, 10) hyperopt.max_date = Arrow(2017, 12, 13) hyperopt.init_spaces() hyperopt.dimensions = hyperopt.dimensions generate_optimizer_value = hyperopt.generate_optimizer( list(optimizer_param.values())) assert generate_optimizer_value == response_expected
def test_generate_optimizer(mocker, hyperopt_conf) -> None: hyperopt_conf.update({ 'spaces': 'all', 'hyperopt_min_trades': 1, }) backtest_result = { 'results': pd.DataFrame({ "pair": ["UNITTEST/BTC", "UNITTEST/BTC", "UNITTEST/BTC", "UNITTEST/BTC"], "profit_ratio": [0.003312, 0.010801, 0.013803, 0.002780], "profit_abs": [0.000003, 0.000011, 0.000014, 0.000003], "open_date": [ Arrow(2017, 11, 14, 19, 32, 00).datetime, Arrow(2017, 11, 14, 21, 36, 00).datetime, Arrow(2017, 11, 14, 22, 12, 00).datetime, Arrow(2017, 11, 14, 22, 44, 00).datetime ], "close_date": [ Arrow(2017, 11, 14, 21, 35, 00).datetime, Arrow(2017, 11, 14, 22, 10, 00).datetime, Arrow(2017, 11, 14, 22, 43, 00).datetime, Arrow(2017, 11, 14, 22, 58, 00).datetime ], "open_rate": [0.002543, 0.003003, 0.003089, 0.003214], "close_rate": [0.002546, 0.003014, 0.003103, 0.003217], "trade_duration": [123, 34, 31, 14], "is_open": [False, False, False, True], "stake_amount": [0.01, 0.01, 0.01, 0.01], "sell_reason": [ SellType.ROI, SellType.STOP_LOSS, SellType.ROI, SellType.FORCE_SELL ] }), 'config': hyperopt_conf, 'locks': [], 'rejected_signals': 20, 'timedout_entry_orders': 0, 'timedout_exit_orders': 0, 'final_balance': 1000, } mocker.patch('freqtrade.optimize.hyperopt.Backtesting.backtest', return_value=backtest_result) mocker.patch('freqtrade.optimize.hyperopt.get_timerange', return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13))) patch_exchange(mocker) mocker.patch.object(Path, 'open') mocker.patch( 'freqtrade.configuration.config_validation.validate_config_schema') mocker.patch('freqtrade.optimize.hyperopt.load', return_value={'XRP/BTC': None}) optimizer_param = { 'buy_plusdi': 0.02, 'buy_rsi': 35, 'sell_minusdi': 0.02, 'sell_rsi': 75, 'protection_cooldown_lookback': 20, 'protection_enabled': True, 'roi_t1': 60.0, 'roi_t2': 30.0, 'roi_t3': 20.0, 'roi_p1': 0.01, 'roi_p2': 0.01, 'roi_p3': 0.1, 'stoploss': -0.4, 'trailing_stop': True, 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset_p1': 0.05, 'trailing_only_offset_is_reached': False, } response_expected = { 'loss': 1.9147239021396234, 'results_explanation': (' 4 trades. 4/0/0 Wins/Draws/Losses. ' 'Avg profit 0.77%. Median profit 0.71%. Total profit ' '0.00003100 BTC ( 0.00%). ' 'Avg duration 0:50:00 min.'), 'params_details': { 'buy': { 'buy_plusdi': 0.02, 'buy_rsi': 35, }, 'roi': { "0": 0.12000000000000001, "20.0": 0.02, "50.0": 0.01, "110.0": 0 }, 'protection': { 'protection_cooldown_lookback': 20, 'protection_enabled': True, }, 'sell': { 'sell_minusdi': 0.02, 'sell_rsi': 75, }, 'stoploss': { 'stoploss': -0.4 }, 'trailing': { 'trailing_only_offset_is_reached': False, 'trailing_stop': True, 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset': 0.07 } }, 'params_dict': optimizer_param, 'params_not_optimized': { 'buy': {}, 'protection': {}, 'sell': {} }, 'results_metrics': ANY, 'total_profit': 3.1e-08 } hyperopt = Hyperopt(hyperopt_conf) hyperopt.min_date = Arrow(2017, 12, 10) hyperopt.max_date = Arrow(2017, 12, 13) hyperopt.init_spaces() hyperopt.dimensions = hyperopt.dimensions generate_optimizer_value = hyperopt.generate_optimizer( list(optimizer_param.values())) assert generate_optimizer_value == response_expected
def test_generate_optimizer(mocker, hyperopt_conf) -> None: hyperopt_conf.update({ 'spaces': 'all', 'hyperopt_min_trades': 1, }) trades = [('TRX/BTC', 0.023117, 0.000233, 100)] labels = ['currency', 'profit_percent', 'profit_abs', 'trade_duration'] backtest_result = pd.DataFrame.from_records(trades, columns=labels) mocker.patch('freqtrade.optimize.hyperopt.Backtesting.backtest', MagicMock(return_value=backtest_result)) mocker.patch( 'freqtrade.optimize.hyperopt.get_timerange', MagicMock(return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13)))) patch_exchange(mocker) mocker.patch('freqtrade.optimize.hyperopt.load', MagicMock()) optimizer_param = { 'adx-value': 0, 'fastd-value': 35, 'mfi-value': 0, 'rsi-value': 0, 'adx-enabled': False, 'fastd-enabled': True, 'mfi-enabled': False, 'rsi-enabled': False, 'trigger': 'macd_cross_signal', 'sell-adx-value': 0, 'sell-fastd-value': 75, 'sell-mfi-value': 0, 'sell-rsi-value': 0, 'sell-adx-enabled': False, 'sell-fastd-enabled': True, 'sell-mfi-enabled': False, 'sell-rsi-enabled': False, 'sell-trigger': 'macd_cross_signal', 'roi_t1': 60.0, 'roi_t2': 30.0, 'roi_t3': 20.0, 'roi_p1': 0.01, 'roi_p2': 0.01, 'roi_p3': 0.1, 'stoploss': -0.4, 'trailing_stop': True, 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset_p1': 0.05, 'trailing_only_offset_is_reached': False, } response_expected = { 'loss': 1.9840569076926293, 'results_explanation': (' 1 trades. 1/0/0 Wins/Draws/Losses. ' 'Avg profit 2.31%. Median profit 2.31%. Total profit ' '0.00023300 BTC ( 2.31\N{GREEK CAPITAL LETTER SIGMA}%). ' 'Avg duration 100.0 min.').encode(locale.getpreferredencoding(), 'replace').decode('utf-8'), 'params_details': { 'buy': { 'adx-enabled': False, 'adx-value': 0, 'fastd-enabled': True, 'fastd-value': 35, 'mfi-enabled': False, 'mfi-value': 0, 'rsi-enabled': False, 'rsi-value': 0, 'trigger': 'macd_cross_signal' }, 'roi': { 0: 0.12000000000000001, 20.0: 0.02, 50.0: 0.01, 110.0: 0 }, 'sell': { 'sell-adx-enabled': False, 'sell-adx-value': 0, 'sell-fastd-enabled': True, 'sell-fastd-value': 75, 'sell-mfi-enabled': False, 'sell-mfi-value': 0, 'sell-rsi-enabled': False, 'sell-rsi-value': 0, 'sell-trigger': 'macd_cross_signal' }, 'stoploss': { 'stoploss': -0.4 }, 'trailing': { 'trailing_only_offset_is_reached': False, 'trailing_stop': True, 'trailing_stop_positive': 0.02, 'trailing_stop_positive_offset': 0.07 } }, 'params_dict': optimizer_param, 'results_metrics': { 'avg_profit': 2.3117, 'draws': 0, 'duration': 100.0, 'losses': 0, 'winsdrawslosses': ' 1 0 0', 'median_profit': 2.3117, 'profit': 2.3117, 'total_profit': 0.000233, 'trade_count': 1, 'wins': 1 }, 'total_profit': 0.00023300 } hyperopt = Hyperopt(hyperopt_conf) hyperopt.dimensions = hyperopt.hyperopt_space() generate_optimizer_value = hyperopt.generate_optimizer( list(optimizer_param.values())) assert generate_optimizer_value == response_expected
def test_generate_optimizer(mocker, default_conf) -> None: default_conf.update({'config': 'config.json.example'}) default_conf.update({'timerange': None}) default_conf.update({'spaces': 'all'}) default_conf.update({'hyperopt_min_trades': 1}) trades = [('POWR/BTC', 0.023117, 0.000233, 100)] labels = ['currency', 'profit_percent', 'profit_abs', 'trade_duration'] backtest_result = pd.DataFrame.from_records(trades, columns=labels) mocker.patch('freqtrade.optimize.hyperopt.Backtesting.backtest', MagicMock(return_value=backtest_result)) mocker.patch( 'freqtrade.optimize.hyperopt.get_timeframe', MagicMock(return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13)))) patch_exchange(mocker) mocker.patch('freqtrade.optimize.hyperopt.load', MagicMock()) optimizer_param = { 'adx-value': 0, 'fastd-value': 35, 'mfi-value': 0, 'rsi-value': 0, 'adx-enabled': False, 'fastd-enabled': True, 'mfi-enabled': False, 'rsi-enabled': False, 'trigger': 'macd_cross_signal', 'sell-adx-value': 0, 'sell-fastd-value': 75, 'sell-mfi-value': 0, 'sell-rsi-value': 0, 'sell-adx-enabled': False, 'sell-fastd-enabled': True, 'sell-mfi-enabled': False, 'sell-rsi-enabled': False, 'sell-trigger': 'macd_cross_signal', 'roi_t1': 60.0, 'roi_t2': 30.0, 'roi_t3': 20.0, 'roi_p1': 0.01, 'roi_p2': 0.01, 'roi_p3': 0.1, 'stoploss': -0.4, } response_expected = { 'loss': 1.9840569076926293, 'results_explanation': ' 1 trades. Avg profit 2.31%. Total profit 0.00023300 BTC ' '( 2.31Σ%). Avg duration 100.0 mins.', 'params': optimizer_param, 'total_profit': 0.00023300 } hyperopt = Hyperopt(default_conf) hyperopt.dimensions = hyperopt.hyperopt_space() generate_optimizer_value = hyperopt.generate_optimizer( list(optimizer_param.values())) assert generate_optimizer_value == response_expected