Fix tests

This commit is contained in:
Matthias 2021-04-29 20:17:13 +02:00
parent 6aaaad29d7
commit 852f125347
2 changed files with 73 additions and 58 deletions

View File

@ -5,7 +5,7 @@ import re
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Dict, List from typing import Dict, List
from unittest.mock import MagicMock from unittest.mock import ANY, MagicMock
import pandas as pd import pandas as pd
import pytest import pytest
@ -18,10 +18,12 @@ from freqtrade.exceptions import OperationalException
from freqtrade.optimize.hyperopt import Hyperopt from freqtrade.optimize.hyperopt import Hyperopt
from freqtrade.optimize.hyperopt_auto import HyperOptAuto from freqtrade.optimize.hyperopt_auto import HyperOptAuto
from freqtrade.optimize.hyperopt_tools import HyperoptTools from freqtrade.optimize.hyperopt_tools import HyperoptTools
from freqtrade.optimize.optimize_reports import generate_strategy_stats
from freqtrade.optimize.space import SKDecimal from freqtrade.optimize.space import SKDecimal
from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
from freqtrade.state import RunMode from freqtrade.state import RunMode
from freqtrade.strategy.hyper import IntParameter from freqtrade.strategy.hyper import IntParameter
from freqtrade.strategy.interface import SellType
from tests.conftest import (get_args, log_has, log_has_re, patch_exchange, from tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
patched_configuration_load_config_file) patched_configuration_load_config_file)
@ -433,18 +435,41 @@ def test_start_calls_optimizer(mocker, hyperopt_conf, capsys) -> None:
assert hasattr(hyperopt, "position_stacking") assert hasattr(hyperopt, "position_stacking")
def test_format_results(hyperopt): def test_hyperopt_format_results(hyperopt):
# Test with BTC as stake_currency
trades = [ bt_result = {
('ETH/BTC', 2, 2, 123), 'results': pd.DataFrame({"pair": ["UNITTEST/BTC", "UNITTEST/BTC",
('LTC/BTC', 1, 1, 123), "UNITTEST/BTC", "UNITTEST/BTC"],
('XPR/BTC', -1, -2, -246) "profit_ratio": [0.003312, 0.010801, 0.013803, 0.002780],
] "profit_abs": [0.000003, 0.000011, 0.000014, 0.000003],
labels = ['currency', 'profit_ratio', 'profit_abs', 'trade_duration'] "open_date": [Arrow(2017, 11, 14, 19, 32, 00).datetime,
df = pd.DataFrame.from_records(trades, columns=labels) Arrow(2017, 11, 14, 21, 36, 00).datetime,
results_metrics = hyperopt._calculate_results_metrics(df) 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.config,
'locks': [],
'final_balance': 0.02,
'backtest_start_time': 1619718665,
'backtest_end_time': 1619718665,
}
results_metrics = generate_strategy_stats({'XRP/BTC': None}, '', bt_result,
Arrow(2017, 11, 14, 19, 32, 00),
Arrow(2017, 12, 14, 19, 32, 00), market_change=0)
results_explanation = hyperopt._format_results_explanation_string(results_metrics) results_explanation = hyperopt._format_results_explanation_string(results_metrics)
total_profit = results_metrics['total_profit'] total_profit = results_metrics['profit_total_abs']
results = { results = {
'loss': 0.0, 'loss': 0.0,
@ -458,21 +483,9 @@ def test_format_results(hyperopt):
} }
result = HyperoptTools._format_explanation_string(results, 1) result = HyperoptTools._format_explanation_string(results, 1)
assert result.find(' 66.67%') assert ' 0.71%' in result
assert result.find('Total profit 1.00000000 BTC') assert 'Total profit 0.00003100 BTC' in result
assert result.find('2.0000Σ %') assert '0:50:00 min' in result
# Test with EUR as stake_currency
trades = [
('ETH/EUR', 2, 2, 123),
('LTC/EUR', 1, 1, 123),
('XPR/EUR', -1, -2, -246)
]
df = pd.DataFrame.from_records(trades, columns=labels)
results_metrics = hyperopt._calculate_results_metrics(df)
results['total_profit'] = results_metrics['total_profit']
result = HyperoptTools._format_explanation_string(results, 1)
assert result.find('Total profit 1.00000000 EUR')
@pytest.mark.parametrize("spaces, expected_results", [ @pytest.mark.parametrize("spaces, expected_results", [
@ -577,22 +590,32 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None:
'hyperopt_min_trades': 1, 'hyperopt_min_trades': 1,
}) })
trades = [ backtest_result = pd.DataFrame({"pair": ["UNITTEST/BTC", "UNITTEST/BTC",
('TRX/BTC', 0.023117, 0.000233, 100) "UNITTEST/BTC", "UNITTEST/BTC"],
] "profit_ratio": [0.003312, 0.010801, 0.013803, 0.002780],
labels = ['currency', 'profit_ratio', 'profit_abs', 'trade_duration'] "profit_abs": [0.000003, 0.000011, 0.000014, 0.000003],
backtest_result = pd.DataFrame.from_records(trades, columns=labels) "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]
})
mocker.patch( mocker.patch('freqtrade.optimize.hyperopt.Backtesting.backtest', return_value=backtest_result)
'freqtrade.optimize.hyperopt.Backtesting.backtest', mocker.patch('freqtrade.optimize.hyperopt.get_timerange',
MagicMock(return_value=backtest_result) return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13)))
)
mocker.patch(
'freqtrade.optimize.hyperopt.get_timerange',
MagicMock(return_value=(Arrow(2017, 12, 10), Arrow(2017, 12, 13)))
)
patch_exchange(mocker) patch_exchange(mocker)
mocker.patch('freqtrade.optimize.hyperopt.load', MagicMock()) mocker.patch('freqtrade.optimize.hyperopt.load', return_value={'XRP/BTC': None})
optimizer_param = { optimizer_param = {
'adx-value': 0, 'adx-value': 0,
@ -626,11 +649,11 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None:
'trailing_only_offset_is_reached': False, 'trailing_only_offset_is_reached': False,
} }
response_expected = { response_expected = {
'loss': 1.9840569076926293, 'loss': 1.9147239021396234,
'results_explanation': (' 1 trades. 1/0/0 Wins/Draws/Losses. ' 'results_explanation': (' 4 trades. 4/0/0 Wins/Draws/Losses. '
'Avg profit 2.31%. Median profit 2.31%. Total profit ' 'Avg profit 0.77%. Median profit 0.71%. Total profit '
'0.00023300 BTC ( 2.31\N{GREEK CAPITAL LETTER SIGMA}%). ' '0.00003100 BTC ( 0.00\N{GREEK CAPITAL LETTER SIGMA}%). '
'Avg duration 100.0 min.' 'Avg duration 0:50:00 min.'
).encode(locale.getpreferredencoding(), 'replace').decode('utf-8'), ).encode(locale.getpreferredencoding(), 'replace').decode('utf-8'),
'params_details': {'buy': {'adx-enabled': False, 'params_details': {'buy': {'adx-enabled': False,
'adx-value': 0, 'adx-value': 0,
@ -660,17 +683,8 @@ def test_generate_optimizer(mocker, hyperopt_conf) -> None:
'trailing_stop_positive': 0.02, 'trailing_stop_positive': 0.02,
'trailing_stop_positive_offset': 0.07}}, 'trailing_stop_positive_offset': 0.07}},
'params_dict': optimizer_param, 'params_dict': optimizer_param,
'results_metrics': {'avg_profit': 2.3117, 'results_metrics': ANY,
'draws': 0, 'total_profit': 3.1e-08
'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 = Hyperopt(hyperopt_conf)

View File

@ -14,7 +14,8 @@ from freqtrade.edge import PairInfo
from freqtrade.optimize.optimize_reports import (generate_backtest_stats, generate_daily_stats, from freqtrade.optimize.optimize_reports import (generate_backtest_stats, generate_daily_stats,
generate_edge_table, generate_pair_metrics, generate_edge_table, generate_pair_metrics,
generate_sell_reason_stats, generate_sell_reason_stats,
generate_strategy_comparison, generate_trading_stats, store_backtest_stats, generate_strategy_comparison,
generate_trading_stats, store_backtest_stats,
text_table_bt_results, text_table_sell_reason, text_table_bt_results, text_table_sell_reason,
text_table_strategy) text_table_strategy)
from freqtrade.resolvers.strategy_resolver import StrategyResolver from freqtrade.resolvers.strategy_resolver import StrategyResolver