| 1 | from datetime import datetime |
| 2 | from unittest.mock import MagicMock |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from freqtrade.exceptions import OperationalException |
| 7 | from freqtrade.optimize.default_hyperopt_loss import ShortTradeDurHyperOptLoss |
| 8 | from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver |
| 9 | |
| 10 | |
| 11 | def test_hyperoptlossresolver_noname(default_conf): |
| 12 | with pytest.raises(OperationalException, |
| 13 | match="No Hyperopt loss set. Please use `--hyperopt-loss` to specify " |
| 14 | "the Hyperopt-Loss class to use."): |
| 15 | HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 16 | |
| 17 | |
| 18 | def test_hyperoptlossresolver(mocker, default_conf) -> None: |
| 19 | |
| 20 | hl = ShortTradeDurHyperOptLoss |
| 21 | mocker.patch( |
| 22 | 'freqtrade.resolvers.hyperopt_resolver.HyperOptLossResolver.load_object', |
| 23 | MagicMock(return_value=hl()) |
| 24 | ) |
| 25 | default_conf.update({'hyperopt_loss': 'SharpeHyperOptLossDaily'}) |
| 26 | x = HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 27 | assert hasattr(x, "hyperopt_loss_function") |
| 28 | |
| 29 | |
| 30 | def test_hyperoptlossresolver_wrongname(default_conf) -> None: |
| 31 | default_conf.update({'hyperopt_loss': "NonExistingLossClass"}) |
| 32 | |
| 33 | with pytest.raises(OperationalException, match=r'Impossible to load HyperoptLoss.*'): |
| 34 | HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 35 | |
| 36 | |
| 37 | def test_loss_calculation_prefer_correct_trade_count(hyperopt_conf, hyperopt_results) -> None: |
| 38 | hl = HyperOptLossResolver.load_hyperoptloss(hyperopt_conf) |
| 39 | correct = hl.hyperopt_loss_function(hyperopt_results, 600, |
| 40 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 41 | over = hl.hyperopt_loss_function(hyperopt_results, 600 + 100, |
| 42 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 43 | under = hl.hyperopt_loss_function(hyperopt_results, 600 - 100, |
| 44 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 45 | assert over > correct |
| 46 | assert under > correct |
| 47 | |
| 48 | |
| 49 | def test_loss_calculation_prefer_shorter_trades(hyperopt_conf, hyperopt_results) -> None: |
| 50 | resultsb = hyperopt_results.copy() |
| 51 | resultsb.loc[1, 'trade_duration'] = 20 |
| 52 | |
| 53 | hl = HyperOptLossResolver.load_hyperoptloss(hyperopt_conf) |
| 54 | longer = hl.hyperopt_loss_function(hyperopt_results, 100, |
| 55 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 56 | shorter = hl.hyperopt_loss_function(resultsb, 100, |
| 57 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 58 | assert shorter < longer |
| 59 | |
| 60 | |
| 61 | def test_loss_calculation_has_limited_profit(hyperopt_conf, hyperopt_results) -> None: |
| 62 | results_over = hyperopt_results.copy() |
| 63 | results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2 |
| 64 | results_under = hyperopt_results.copy() |
| 65 | results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2 |
| 66 | |
| 67 | hl = HyperOptLossResolver.load_hyperoptloss(hyperopt_conf) |
| 68 | correct = hl.hyperopt_loss_function(hyperopt_results, 600, |
| 69 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 70 | over = hl.hyperopt_loss_function(results_over, 600, |
| 71 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 72 | under = hl.hyperopt_loss_function(results_under, 600, |
| 73 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 74 | assert over < correct |
| 75 | assert under > correct |
| 76 | |
| 77 | |
| 78 | def test_sharpe_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None: |
| 79 | results_over = hyperopt_results.copy() |
| 80 | results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2 |
| 81 | results_under = hyperopt_results.copy() |
| 82 | results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2 |
| 83 | |
| 84 | default_conf.update({'hyperopt_loss': 'SharpeHyperOptLoss'}) |
| 85 | hl = HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 86 | correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results), |
| 87 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 88 | over = hl.hyperopt_loss_function(results_over, len(hyperopt_results), |
| 89 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 90 | under = hl.hyperopt_loss_function(results_under, len(hyperopt_results), |
| 91 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 92 | assert over < correct |
| 93 | assert under > correct |
| 94 | |
| 95 | |
| 96 | def test_sharpe_loss_daily_prefers_higher_profits(default_conf, hyperopt_results) -> None: |
| 97 | results_over = hyperopt_results.copy() |
| 98 | results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2 |
| 99 | results_under = hyperopt_results.copy() |
| 100 | results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2 |
| 101 | |
| 102 | default_conf.update({'hyperopt_loss': 'SharpeHyperOptLossDaily'}) |
| 103 | hl = HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 104 | correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results), |
| 105 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 106 | over = hl.hyperopt_loss_function(results_over, len(hyperopt_results), |
| 107 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 108 | under = hl.hyperopt_loss_function(results_under, len(hyperopt_results), |
| 109 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 110 | assert over < correct |
| 111 | assert under > correct |
| 112 | |
| 113 | |
| 114 | def test_sortino_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None: |
| 115 | results_over = hyperopt_results.copy() |
| 116 | results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2 |
| 117 | results_under = hyperopt_results.copy() |
| 118 | results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2 |
| 119 | |
| 120 | default_conf.update({'hyperopt_loss': 'SortinoHyperOptLoss'}) |
| 121 | hl = HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 122 | correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results), |
| 123 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 124 | over = hl.hyperopt_loss_function(results_over, len(hyperopt_results), |
| 125 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 126 | under = hl.hyperopt_loss_function(results_under, len(hyperopt_results), |
| 127 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 128 | assert over < correct |
| 129 | assert under > correct |
| 130 | |
| 131 | |
| 132 | def test_sortino_loss_daily_prefers_higher_profits(default_conf, hyperopt_results) -> None: |
| 133 | results_over = hyperopt_results.copy() |
| 134 | results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2 |
| 135 | results_under = hyperopt_results.copy() |
| 136 | results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2 |
| 137 | |
| 138 | default_conf.update({'hyperopt_loss': 'SortinoHyperOptLossDaily'}) |
| 139 | hl = HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 140 | correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results), |
| 141 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 142 | over = hl.hyperopt_loss_function(results_over, len(hyperopt_results), |
| 143 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 144 | under = hl.hyperopt_loss_function(results_under, len(hyperopt_results), |
| 145 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 146 | assert over < correct |
| 147 | assert under > correct |
| 148 | |
| 149 | |
| 150 | def test_onlyprofit_loss_prefers_higher_profits(default_conf, hyperopt_results) -> None: |
| 151 | results_over = hyperopt_results.copy() |
| 152 | results_over['profit_ratio'] = hyperopt_results['profit_ratio'] * 2 |
| 153 | results_under = hyperopt_results.copy() |
| 154 | results_under['profit_ratio'] = hyperopt_results['profit_ratio'] / 2 |
| 155 | |
| 156 | default_conf.update({'hyperopt_loss': 'OnlyProfitHyperOptLoss'}) |
| 157 | hl = HyperOptLossResolver.load_hyperoptloss(default_conf) |
| 158 | correct = hl.hyperopt_loss_function(hyperopt_results, len(hyperopt_results), |
| 159 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 160 | over = hl.hyperopt_loss_function(results_over, len(hyperopt_results), |
| 161 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 162 | under = hl.hyperopt_loss_function(results_under, len(hyperopt_results), |
| 163 | datetime(2019, 1, 1), datetime(2019, 5, 1)) |
| 164 | assert over < correct |
| 165 | assert under > correct |