mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-10 10:21:59 +00:00
Improve backtest tests
This commit is contained in:
parent
ad28543d4d
commit
bf0b95b3d8
|
@ -3684,9 +3684,13 @@ def test__fetch_and_calculate_funding_fees(
|
||||||
type(api_mock).has = PropertyMock(return_value={'fetchFundingRateHistory': True})
|
type(api_mock).has = PropertyMock(return_value={'fetchFundingRateHistory': True})
|
||||||
|
|
||||||
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange)
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange)
|
||||||
# TODO-lev: test this for longs
|
funding_fees = exchange._fetch_and_calculate_funding_fees(
|
||||||
funding_fees = exchange._fetch_and_calculate_funding_fees('ADA/USDT', amount, True, d1, d2)
|
pair='ADA/USDT', amount=amount, is_short=True, open_date=d1, close_date=d2)
|
||||||
assert pytest.approx(funding_fees) == expected_fees
|
assert pytest.approx(funding_fees) == expected_fees
|
||||||
|
# Fees for Longs are inverted
|
||||||
|
funding_fees = exchange._fetch_and_calculate_funding_fees(
|
||||||
|
pair='ADA/USDT', amount=amount, is_short=False, open_date=d1, close_date=d2)
|
||||||
|
assert pytest.approx(funding_fees) == -expected_fees
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('exchange,expected_fees', [
|
@pytest.mark.parametrize('exchange,expected_fees', [
|
||||||
|
|
|
@ -1169,6 +1169,107 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat
|
||||||
assert 'STRATEGY SUMMARY' in captured.out
|
assert 'STRATEGY SUMMARY' in captured.out
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.filterwarnings("ignore:deprecated")
|
||||||
|
def test_backtest_start_nomock_futures(default_conf_usdt, mocker,
|
||||||
|
caplog, testdatadir, capsys):
|
||||||
|
# Tests detail-data loading
|
||||||
|
default_conf_usdt.update({
|
||||||
|
"trading_mode": "futures",
|
||||||
|
"collateral": "isolated",
|
||||||
|
"use_sell_signal": True,
|
||||||
|
"sell_profit_only": False,
|
||||||
|
"sell_profit_offset": 0.0,
|
||||||
|
"ignore_roi_if_buy_signal": False,
|
||||||
|
"strategy": CURRENT_TEST_STRATEGY,
|
||||||
|
})
|
||||||
|
patch_exchange(mocker)
|
||||||
|
result1 = pd.DataFrame({'pair': ['XRP/USDT', 'XRP/USDT'],
|
||||||
|
'profit_ratio': [0.0, 0.0],
|
||||||
|
'profit_abs': [0.0, 0.0],
|
||||||
|
'open_date': pd.to_datetime(['2021-11-18 18:00:00',
|
||||||
|
'2021-11-18 03:00:00', ], utc=True
|
||||||
|
),
|
||||||
|
'close_date': pd.to_datetime(['2021-11-18 20:00:00',
|
||||||
|
'2021-11-18 05:00:00', ], utc=True),
|
||||||
|
'trade_duration': [235, 40],
|
||||||
|
'is_open': [False, False],
|
||||||
|
'is_short': [False, False],
|
||||||
|
'stake_amount': [0.01, 0.01],
|
||||||
|
'open_rate': [0.104445, 0.10302485],
|
||||||
|
'close_rate': [0.104969, 0.103541],
|
||||||
|
'sell_reason': [SellType.ROI, SellType.ROI]
|
||||||
|
})
|
||||||
|
result2 = pd.DataFrame({'pair': ['XRP/USDT', 'XRP/USDT', 'XRP/USDT'],
|
||||||
|
'profit_ratio': [0.03, 0.01, 0.1],
|
||||||
|
'profit_abs': [0.01, 0.02, 0.2],
|
||||||
|
'open_date': pd.to_datetime(['2021-11-19 18:00:00',
|
||||||
|
'2021-11-19 03:00:00',
|
||||||
|
'2021-11-19 05:00:00'], utc=True
|
||||||
|
),
|
||||||
|
'close_date': pd.to_datetime(['2021-11-19 20:00:00',
|
||||||
|
'2021-11-19 05:00:00',
|
||||||
|
'2021-11-19 08:00:00'], utc=True),
|
||||||
|
'trade_duration': [47, 40, 20],
|
||||||
|
'is_open': [False, False, False],
|
||||||
|
'is_short': [False, False, False],
|
||||||
|
'stake_amount': [0.01, 0.01, 0.01],
|
||||||
|
'open_rate': [0.104445, 0.10302485, 0.122541],
|
||||||
|
'close_rate': [0.104969, 0.103541, 0.123541],
|
||||||
|
'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
|
||||||
|
})
|
||||||
|
backtestmock = MagicMock(side_effect=[
|
||||||
|
{
|
||||||
|
'results': result1,
|
||||||
|
'config': default_conf_usdt,
|
||||||
|
'locks': [],
|
||||||
|
'rejected_signals': 20,
|
||||||
|
'final_balance': 1000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'results': result2,
|
||||||
|
'config': default_conf_usdt,
|
||||||
|
'locks': [],
|
||||||
|
'rejected_signals': 20,
|
||||||
|
'final_balance': 1000,
|
||||||
|
}
|
||||||
|
])
|
||||||
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
||||||
|
PropertyMock(return_value=['XRP/USDT']))
|
||||||
|
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest', backtestmock)
|
||||||
|
|
||||||
|
patched_configuration_load_config_file(mocker, default_conf_usdt)
|
||||||
|
|
||||||
|
args = [
|
||||||
|
'backtesting',
|
||||||
|
'--config', 'config.json',
|
||||||
|
'--datadir', str(testdatadir),
|
||||||
|
'--strategy-path', str(Path(__file__).parents[1] / 'strategy/strats'),
|
||||||
|
'--timeframe', '1h',
|
||||||
|
]
|
||||||
|
args = get_args(args)
|
||||||
|
start_backtesting(args)
|
||||||
|
|
||||||
|
# check the logs, that will contain the backtest result
|
||||||
|
exists = [
|
||||||
|
'Parameter -i/--timeframe detected ... Using timeframe: 1h ...',
|
||||||
|
f'Using data directory: {testdatadir} ...',
|
||||||
|
'Loading data from 2021-11-17 01:00:00 '
|
||||||
|
'up to 2021-11-21 03:00:00 (4 days).',
|
||||||
|
'Backtesting with data from 2021-11-17 21:00:00 '
|
||||||
|
'up to 2021-11-21 03:00:00 (3 days).',
|
||||||
|
'XRP/USDT, funding_rate, 8h, data starts at 2021-11-18 00:00:00',
|
||||||
|
'XRP/USDT, mark, 8h, data starts at 2021-11-18 00:00:00',
|
||||||
|
f'Running backtesting for Strategy {CURRENT_TEST_STRATEGY}',
|
||||||
|
]
|
||||||
|
|
||||||
|
for line in exists:
|
||||||
|
assert log_has(line, caplog)
|
||||||
|
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert 'BACKTESTING REPORT' in captured.out
|
||||||
|
assert 'SELL REASON STATS' in captured.out
|
||||||
|
assert 'LEFT OPEN TRADES REPORT' in captured.out
|
||||||
|
|
||||||
@pytest.mark.filterwarnings("ignore:deprecated")
|
@pytest.mark.filterwarnings("ignore:deprecated")
|
||||||
def test_backtest_start_multi_strat_nomock_detail(default_conf, mocker,
|
def test_backtest_start_multi_strat_nomock_detail(default_conf, mocker,
|
||||||
caplog, testdatadir, capsys):
|
caplog, testdatadir, capsys):
|
||||||
|
|
1
tests/testdata/futures/XRP_USDT-8h-funding_rate.json
vendored
Normal file
1
tests/testdata/futures/XRP_USDT-8h-funding_rate.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[[1637193600017,0.0001,0.0,0.0,0.0,0.0],[1637222400007,0.0001,0.0,0.0,0.0,0.0],[1637251200011,0.0001,0.0,0.0,0.0,0.0],[1637280000000,0.0001,0.0,0.0,0.0,0.0],[1637308800000,0.0001,0.0,0.0,0.0,0.0],[1637337600005,0.0001,0.0,0.0,0.0,0.0],[1637366400012,0.00013046,0.0,0.0,0.0,0.0],[1637395200000,0.0001,0.0,0.0,0.0,0.0],[1637424000007,0.0001,0.0,0.0,0.0,0.0],[1637452800000,0.00013862,0.0,0.0,0.0,0.0],[1637481600006,0.0001,0.0,0.0,0.0,0.0],[1637510400000,0.00019881,0.0,0.0,0.0,0.0],[1637539200004,0.00013991,0.0,0.0,0.0,0.0],[1637568000000,0.0001,0.0,0.0,0.0,0.0],[1637596800000,0.0001,0.0,0.0,0.0,0.0],[1637625600004,0.0001,0.0,0.0,0.0,0.0],[1637654400010,0.0001,0.0,0.0,0.0,0.0],[1637683200005,0.00017402,0.0,0.0,0.0,0.0],[1637712000001,0.00016775,0.0,0.0,0.0,0.0],[1637740800003,0.00033523,0.0,0.0,0.0,0.0],[1637769600010,0.0001,0.0,0.0,0.0,0.0],[1637798400000,0.00020066,0.0,0.0,0.0,0.0],[1637827200010,0.00034381,0.0,0.0,0.0,0.0],[1637856000000,0.00032096,0.0,0.0,0.0,0.0],[1637884800000,0.00058316,0.0,0.0,0.0,0.0],[1637913600000,0.0001646,0.0,0.0,0.0,0.0],[1637942400016,0.0001,0.0,0.0,0.0,0.0],[1637971200005,0.0001,0.0,0.0,0.0,0.0],[1638000000008,0.0001,0.0,0.0,0.0,0.0],[1638028800007,0.0001,0.0,0.0,0.0,0.0],[1638057600018,0.0001,0.0,0.0,0.0,0.0],[1638086400000,0.0001,0.0,0.0,0.0,0.0],[1638115200004,0.0001,0.0,0.0,0.0,0.0],[1638144000002,0.0001,0.0,0.0,0.0,0.0],[1638172800004,0.0001,0.0,0.0,0.0,0.0],[1638201600000,0.0001,0.0,0.0,0.0,0.0],[1638230400000,0.0001,0.0,0.0,0.0,0.0],[1638259200006,0.0001,0.0,0.0,0.0,0.0],[1638288000000,0.0001,0.0,0.0,0.0,0.0],[1638316800000,0.0001,0.0,0.0,0.0,0.0],[1638345600000,0.0001,0.0,0.0,0.0,0.0],[1638374400001,0.0001,0.0,0.0,0.0,0.0],[1638403200000,0.0001,0.0,0.0,0.0,0.0],[1638432000007,0.0001,0.0,0.0,0.0,0.0],[1638460800008,0.0001,0.0,0.0,0.0,0.0],[1638489600004,0.0001,0.0,0.0,0.0,0.0],[1638518400002,0.0001,0.0,0.0,0.0,0.0],[1638547200006,0.0001,0.0,0.0,0.0,0.0],[1638576000006,0.0001,0.0,0.0,0.0,0.0],[1638604800004,-0.00219334,0.0,0.0,0.0,0.0],[1638633600000,0.0001,0.0,0.0,0.0,0.0],[1638662400003,0.00006147,0.0,0.0,0.0,0.0],[1638691200008,0.0001,0.0,0.0,0.0,0.0],[1638720000007,0.0001,0.0,0.0,0.0,0.0],[1638748800009,0.0001,0.0,0.0,0.0,0.0],[1638777600001,0.0001,0.0,0.0,0.0,0.0],[1638806400000,0.0001,0.0,0.0,0.0,0.0],[1638835200018,0.0001,0.0,0.0,0.0,0.0],[1638864000000,0.0001,0.0,0.0,0.0,0.0],[1638892800000,0.0001,0.0,0.0,0.0,0.0],[1638921600000,0.0001,0.0,0.0,0.0,0.0],[1638950400018,0.0001,0.0,0.0,0.0,0.0],[1638979200010,0.0001,0.0,0.0,0.0,0.0],[1639008000010,0.0001,0.0,0.0,0.0,0.0],[1639036800000,0.0001,0.0,0.0,0.0,0.0],[1639065600000,0.0001,0.0,0.0,0.0,0.0],[1639094400000,0.0001,0.0,0.0,0.0,0.0],[1639123200008,0.0001,0.0,0.0,0.0,0.0],[1639152000012,0.00008995,0.0,0.0,0.0,0.0],[1639180800009,0.0001,0.0,0.0,0.0,0.0],[1639209600008,-0.00002574,0.0,0.0,0.0,0.0],[1639238400000,-0.00002024,0.0,0.0,0.0,0.0],[1639267200001,-0.00008282,0.0,0.0,0.0,0.0],[1639296000015,0.0001,0.0,0.0,0.0,0.0],[1639324800011,0.00008752,0.0,0.0,0.0,0.0],[1639353600006,0.0001,0.0,0.0,0.0,0.0],[1639382400019,0.0001,0.0,0.0,0.0,0.0],[1639411200000,0.0001,0.0,0.0,0.0,0.0],[1639440000004,0.00007825,0.0,0.0,0.0,0.0],[1639468800000,0.00007108,0.0,0.0,0.0,0.0],[1639497600015,0.0001,0.0,0.0,0.0,0.0],[1639526400000,0.0001,0.0,0.0,0.0,0.0],[1639555200008,0.0001,0.0,0.0,0.0,0.0],[1639584000005,0.0001,0.0,0.0,0.0,0.0],[1639612800006,0.0001,0.0,0.0,0.0,0.0],[1639641600009,0.0001,0.0,0.0,0.0,0.0],[1639670400000,0.0001,0.0,0.0,0.0,0.0],[1639699200000,0.0001,0.0,0.0,0.0,0.0],[1639728000005,0.0001,0.0,0.0,0.0,0.0],[1639756800006,0.0001,0.0,0.0,0.0,0.0],[1639785600014,0.0001,0.0,0.0,0.0,0.0]]
|
1
tests/testdata/futures/XRP_USDT-8h-mark.json
vendored
Normal file
1
tests/testdata/futures/XRP_USDT-8h-mark.json
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user