Update tests to properly mock fee

This commit is contained in:
Matthias
2024-04-27 19:52:48 +02:00
parent 3a2e3215b9
commit f259270e9c
6 changed files with 20 additions and 24 deletions

View File

@@ -241,7 +241,7 @@ def patch_exchange(
if api_mock:
mocker.patch(f'{EXMS}._init_ccxt', return_value=api_mock)
else:
mocker.patch(f'{EXMS}.get_fee', return_value=0.025)
mocker.patch(f'{EXMS}.get_fee', return_value=0.0025)
mocker.patch(f'{EXMS}._init_ccxt', MagicMock())
mocker.patch(f'{EXMS}.timeframes', PropertyMock(
return_value=['5m', '15m', '1h', '1d']))

View File

@@ -298,12 +298,12 @@ def test_backtesting_init_no_timeframe(mocker, default_conf, caplog) -> None:
def test_data_with_fee(default_conf, mocker) -> None:
patch_exchange(mocker)
default_conf['fee'] = 0.1234
default_conf['fee'] = 0.01234
fee_mock = mocker.patch(f'{EXMS}.get_fee', MagicMock(return_value=0.5))
backtesting = Backtesting(default_conf)
backtesting._set_strategy(backtesting.strategylist[0])
assert backtesting.fee == 0.1234
assert backtesting.fee == 0.01234
assert fee_mock.call_count == 0
default_conf['fee'] = 0.0
@@ -620,12 +620,11 @@ def test_backtest__enter_trade_futures(default_conf_usdt, fee, mocker) -> None:
assert trade is None
def test_backtest__check_trade_exit(default_conf, fee, mocker) -> None:
def test_backtest__check_trade_exit(default_conf, mocker) -> None:
default_conf['use_exit_signal'] = False
mocker.patch(f'{EXMS}.get_fee', fee)
patch_exchange(mocker)
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001)
mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf'))
patch_exchange(mocker)
default_conf['timeframe_detail'] = '1m'
default_conf['max_open_trades'] = 2
backtesting = Backtesting(default_conf)
@@ -683,14 +682,13 @@ def test_backtest__check_trade_exit(default_conf, fee, mocker) -> None:
assert res is None
def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None:
def test_backtest_one(default_conf, mocker, testdatadir) -> None:
default_conf['use_exit_signal'] = False
default_conf['max_open_trades'] = 10
mocker.patch(f'{EXMS}.get_fee', fee)
patch_exchange(mocker)
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001)
mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf'))
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
backtesting._set_strategy(backtesting.strategylist[0])
pair = 'UNITTEST/BTC'
@@ -777,14 +775,13 @@ def test_backtest_one(default_conf, fee, mocker, testdatadir) -> None:
@pytest.mark.parametrize('use_detail', [True, False])
def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_detail) -> None:
def test_backtest_one_detail(default_conf_usdt, mocker, testdatadir, use_detail) -> None:
default_conf_usdt['use_exit_signal'] = False
mocker.patch(f'{EXMS}.get_fee', fee)
patch_exchange(mocker)
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001)
mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf'))
if use_detail:
default_conf_usdt['timeframe_detail'] = '1m'
patch_exchange(mocker)
def advise_entry(df, *args, **kwargs):
# Mock function to force several entries
@@ -864,14 +861,14 @@ def test_backtest_one_detail(default_conf_usdt, fee, mocker, testdatadir, use_de
(False, -0.01780296, 5),
])
def test_backtest_one_detail_futures(
default_conf_usdt, fee, mocker, testdatadir, use_detail, exp_funding_fee,
default_conf_usdt, mocker, testdatadir, use_detail, exp_funding_fee,
exp_ff_updates) -> None:
default_conf_usdt['use_exit_signal'] = False
default_conf_usdt['trading_mode'] = 'futures'
default_conf_usdt['margin_mode'] = 'isolated'
default_conf_usdt['candle_type_def'] = CandleType.FUTURES
mocker.patch(f'{EXMS}.get_fee', fee)
patch_exchange(mocker)
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001)
mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf'))
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
@@ -881,7 +878,6 @@ def test_backtest_one_detail_futures(
default_conf_usdt['timeframe'] = '1h'
if use_detail:
default_conf_usdt['timeframe_detail'] = '5m'
patch_exchange(mocker)
def advise_entry(df, *args, **kwargs):
# Mock function to force several entries
@@ -1237,19 +1233,18 @@ def test_backtest_pricecontours_protections(default_conf, fee, mocker, testdatad
([{"method": "CooldownPeriod", "stop_duration": 3}], 'sine', 9),
([{"method": "CooldownPeriod", "stop_duration": 3}], 'raise', 10),
])
def test_backtest_pricecontours(default_conf, fee, mocker, testdatadir,
def test_backtest_pricecontours(default_conf, mocker, testdatadir,
protections, contour, expected) -> None:
if protections:
default_conf['protections'] = protections
default_conf['enable_protections'] = True
patch_exchange(mocker)
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001)
mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf'))
mocker.patch(f'{EXMS}.get_fee', fee)
# While entry-signals are unrealistic, running backtesting
# over and over again should not cause different results
patch_exchange(mocker)
default_conf['timeframe'] = '1m'
backtesting = Backtesting(default_conf)
backtesting._set_strategy(backtesting.strategylist[0])

View File

@@ -99,11 +99,11 @@ def test_edge_init(mocker, edge_conf) -> None:
def test_edge_init_fee(mocker, edge_conf) -> None:
patch_exchange(mocker)
edge_conf['fee'] = 0.1234
edge_conf['fee'] = 0.01234
edge_conf['stake_amount'] = 20
fee_mock = mocker.patch(f'{EXMS}.get_fee', return_value=0.5)
edge_cli = EdgeCli(edge_conf)
assert edge_cli.edge.fee == 0.1234
assert edge_cli.edge.fee == 0.01234
assert fee_mock.call_count == 0

View File

@@ -343,11 +343,11 @@ def test_initialize_single_lookahead_analysis(lookahead_conf, mocker, caplog):
'no_bias', 'bias1'
])
def test_biased_strategy(lookahead_conf, mocker, caplog, scenario) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.data.history.get_timerange', get_timerange)
mocker.patch(f'{EXMS}.get_fee', return_value=0.0)
mocker.patch(f'{EXMS}.get_min_pair_stake_amount', return_value=0.00001)
mocker.patch(f'{EXMS}.get_max_pair_stake_amount', return_value=float('inf'))
patch_exchange(mocker)
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
PropertyMock(return_value=['UNITTEST/BTC']))
lookahead_conf['pairs'] = ['UNITTEST/USDT']

View File

@@ -10,7 +10,7 @@ from freqtrade.data.history import get_timerange
from freqtrade.exceptions import OperationalException
from freqtrade.optimize.analysis.recursive import RecursiveAnalysis
from freqtrade.optimize.analysis.recursive_helpers import RecursiveAnalysisSubFunctions
from tests.conftest import get_args, log_has_re, patch_exchange
from tests.conftest import EXMS, get_args, log_has_re, patch_exchange
@pytest.fixture
@@ -152,8 +152,9 @@ def test_initialize_single_recursive_analysis(recursive_conf, mocker, caplog):
'no_bias', 'bias1', 'bias2'
])
def test_recursive_biased_strategy(recursive_conf, mocker, caplog, scenario) -> None:
mocker.patch('freqtrade.data.history.get_timerange', get_timerange)
patch_exchange(mocker)
mocker.patch(f'{EXMS}.get_fee', return_value=0.0)
mocker.patch('freqtrade.data.history.get_timerange', get_timerange)
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
PropertyMock(return_value=['UNITTEST/BTC']))
recursive_conf['pairs'] = ['UNITTEST/BTC']

View File

@@ -103,6 +103,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'funding_fee': ANY, 'ft_order_tag': None,
}],
}
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
mocker.patch.multiple(
EXMS,
@@ -111,7 +112,6 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
_dry_is_price_crossed=MagicMock(side_effect=[False, True]),
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
patch_get_signal(freqtradebot)
rpc = RPC(freqtradebot)