diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 382847b21..72c6d4c72 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -3684,9 +3684,13 @@ def test__fetch_and_calculate_funding_fees( type(api_mock).has = PropertyMock(return_value={'fetchFundingRateHistory': True}) 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('ADA/USDT', amount, True, d1, d2) + funding_fees = exchange._fetch_and_calculate_funding_fees( + pair='ADA/USDT', amount=amount, is_short=True, open_date=d1, close_date=d2) 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', [ diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index cfeb4a23a..75bcdffcc 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -1169,6 +1169,107 @@ def test_backtest_start_multi_strat_nomock(default_conf, mocker, caplog, testdat 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") def test_backtest_start_multi_strat_nomock_detail(default_conf, mocker, caplog, testdatadir, capsys): diff --git a/tests/testdata/futures/XRP_USDT-8h-funding_rate.json b/tests/testdata/futures/XRP_USDT-8h-funding_rate.json new file mode 100644 index 000000000..494da4efc --- /dev/null +++ b/tests/testdata/futures/XRP_USDT-8h-funding_rate.json @@ -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]] \ No newline at end of file diff --git a/tests/testdata/futures/XRP_USDT-8h-mark.json b/tests/testdata/futures/XRP_USDT-8h-mark.json new file mode 100644 index 000000000..63dad259b --- /dev/null +++ b/tests/testdata/futures/XRP_USDT-8h-mark.json @@ -0,0 +1 @@ +[[1637193600000,1.0959,1.162,1.0907,1.1074,523374743.8000000119],[1637222400000,1.1075,1.1104,1.045,1.0563,429699821.3999999762],[1637251200000,1.0564,1.0635,1.0145,1.041,417701240.6000000238],[1637280000000,1.0411,1.0572,1.0179,1.0421,262751968.599999994],[1637308800000,1.042,1.1034,1.0418,1.0891,322658150.1999999881],[1637337600000,1.0891,1.099,1.0748,1.0903,176970752.400000006],[1637366400000,1.0903,1.1005,1.0821,1.0856,125726657.400000006],[1637395200000,1.0857,1.1024,1.06,1.0657,193947922.5],[1637424000000,1.0656,1.0987,1.0619,1.0976,165812883.599999994],[1637452800000,1.0975,1.0988,1.0732,1.0803,103157439.799999997],[1637481600000,1.0804,1.0818,1.0638,1.0788,139946704.400000006],[1637510400000,1.0787,1.0867,1.055,1.0581,155236087.3000000119],[1637539200000,1.0582,1.0604,1.026,1.0433,245459370.400000006],[1637568000000,1.0434,1.072,1.0373,1.0577,214156908.400000006],[1637596800000,1.0577,1.0598,1.0284,1.0366,171637007.0],[1637625600000,1.0365,1.0569,1.0311,1.0368,133990133.0],[1637654400000,1.0367,1.0623,1.02,1.0474,300886007.5],[1637683200000,1.0472,1.0725,1.043,1.067,164993866.900000006],[1637712000000,1.0671,1.0741,1.0328,1.0398,162787182.099999994],[1637740800000,1.0397,1.0496,1.005,1.0287,263357085.900000006],[1637769600000,1.0287,1.0343,1.0142,1.0329,142076018.1999999881],[1637798400000,1.0329,1.0525,1.0266,1.0332,151346926.0],[1637827200000,1.0333,1.0597,1.023,1.0529,210738649.0],[1637856000000,1.053,1.0663,1.041,1.0447,169577266.5],[1637884800000,1.0448,1.0479,1.0,1.0145,243945720.900000006],[1637913600000,1.0144,1.0146,0.8836,0.9465,1033033518.6000000238],[1637942400000,0.9467,0.9608,0.9333,0.9392,185904492.1999999881],[1637971200000,0.9392,0.9614,0.9354,0.947,133557450.400000006],[1638000000000,0.947,0.9659,0.9466,0.9563,130188025.599999994],[1638028800000,0.9562,0.9615,0.9338,0.9455,143028245.599999994],[1638057600000,0.9455,0.947,0.8779,0.93,306498284.8999999762],[1638086400000,0.93,0.9415,0.9177,0.9257,126269337.799999997],[1638115200000,0.9256,0.9693,0.8855,0.9686,298275834.3000000119],[1638144000000,0.9686,0.9954,0.9661,0.99,178517855.1999999881],[1638172800000,0.99,0.9926,0.9632,0.9772,199170626.8000000119],[1638201600000,0.9772,1.0024,0.973,0.9901,226187446.0],[1638230400000,0.9901,1.0154,0.9718,0.9833,239524176.3000000119],[1638259200000,0.9834,1.0301,0.97,1.0065,296499649.0],[1638288000000,1.0064,1.0138,0.9845,0.9989,232078115.8000000119],[1638316800000,0.9989,1.0182,0.9934,1.0143,118435865.599999994],[1638345600000,1.0143,1.017,0.9966,1.0119,169147098.3000000119],[1638374400000,1.0118,1.0182,0.98,0.9906,181653125.0],[1638403200000,0.9906,0.9909,0.9545,0.9746,168864095.6999999881],[1638432000000,0.9745,0.9844,0.9629,0.9748,153223080.3000000119],[1638460800000,0.9748,0.9843,0.9645,0.9722,110476722.5],[1638489600000,0.9722,0.981,0.9583,0.9779,132304244.599999994],[1638518400000,0.978,0.984,0.9569,0.9615,147970618.1999999881],[1638547200000,0.9614,0.9614,0.8854,0.9213,403564589.8000000119],[1638576000000,0.9212,0.9246,0.5764,0.7497,1544746782.7000000477],[1638604800000,0.7497,0.8066,0.7405,0.792,741292824.2999999523],[1638633600000,0.792,0.8574,0.7855,0.8449,360411800.1999999881],[1638662400000,0.8449,0.8623,0.8131,0.8382,270770494.1000000238],[1638691200000,0.8381,0.8437,0.7674,0.7897,459358701.5],[1638720000000,0.7897,0.8099,0.7816,0.8041,355021022.1999999881],[1638748800000,0.8041,0.8068,0.7619,0.7747,268906281.8000000119],[1638777600000,0.7748,0.7939,0.7475,0.7934,511486538.1000000238],[1638806400000,0.7934,0.8299,0.786,0.8251,246683461.0],[1638835200000,0.8252,0.8459,0.8209,0.8303,176572777.6999999881],[1638864000000,0.8303,0.844,0.8133,0.8368,248783345.1999999881],[1638892800000,0.8368,0.8386,0.8037,0.8154,188883508.6999999881],[1638921600000,0.8155,0.8437,0.8037,0.8404,168332179.400000006],[1638950400000,0.8404,0.8796,0.8022,0.8669,452844121.1999999881],[1638979200000,0.8668,0.8842,0.85,0.862,291431732.3000000119],[1639008000000,0.8619,0.8718,0.842,0.8602,203577851.900000006],[1639036800000,0.8603,0.934,0.8315,0.8911,1062305914.5],[1639065600000,0.8913,0.9045,0.8433,0.8582,451002103.3999999762],[1639094400000,0.8582,0.8829,0.8252,0.8333,426994850.3999999762],[1639123200000,0.8333,0.8677,0.8155,0.8234,472180388.3000000119],[1639152000000,0.8234,0.8365,0.791,0.7985,293320072.3999999762],[1639180800000,0.7984,0.8369,0.7837,0.826,287918666.5],[1639209600000,0.8261,0.8433,0.8228,0.8376,194875683.0],[1639238400000,0.8376,0.8442,0.8201,0.8388,171763908.1999999881],[1639267200000,0.8388,0.842,0.8151,0.8244,165764217.599999994],[1639296000000,0.8243,0.83,0.8081,0.822,154383486.900000006],[1639324800000,0.8221,0.857,0.8201,0.8393,188392105.5],[1639353600000,0.8393,0.8444,0.8165,0.8272,174739478.8000000119],[1639382400000,0.8271,0.8381,0.7923,0.7991,303947463.0],[1639411200000,0.7992,0.807,0.76,0.7815,426182302.3000000119],[1639440000000,0.7815,0.7966,0.7716,0.7891,237419158.400000006],[1639468800000,0.7891,0.8178,0.7787,0.7927,307108359.8999999762],[1639497600000,0.7926,0.8212,0.7883,0.8101,235803972.599999994],[1639526400000,0.8101,0.8151,0.8009,0.8102,155175275.900000006],[1639555200000,0.8102,0.8176,0.7767,0.7781,222914476.5],[1639584000000,0.778,0.8396,0.7768,0.8259,439618329.1000000238],[1639612800000,0.826,0.8361,0.8181,0.8238,133573251.099999994],[1639641600000,0.8238,0.8335,0.8191,0.8263,140555140.3000000119],[1639670400000,0.8263,0.8274,0.8025,0.8045,155672857.0],[1639699200000,0.8047,0.8323,0.8013,0.8124,173377367.599999994],[1639728000000,0.8124,0.8151,0.7749,0.7953,243494249.400000006],[1639756800000,0.7953,0.8109,0.7871,0.7963,186657940.1999999881],[1639785600000,0.7963,0.8159,0.7904,0.8124,144712394.5]] \ No newline at end of file