From f03f586eeb829314d3ba491be706d43408f50747 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 21 Mar 2022 05:01:18 -0600 Subject: [PATCH 1/3] funding_fee tests --- tests/exchange/test_exchange.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 9bb9db58f..aed58a368 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -3877,6 +3877,7 @@ def test_get_or_calculate_liquidation_price(mocker, default_conf): @pytest.mark.parametrize('exchange,rate_start,rate_end,d1,d2,amount,expected_fees', [ + ('binance', 0, 2, "2021-09-01 01:00:00", "2021-09-01 04:00:00", 30.0, 0.0), ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), ('binance', 0, 2, "2021-09-01 00:00:15", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), ('binance', 1, 2, "2021-09-01 01:00:14", "2021-09-01 08:00:00", 30.0, -0.0002493), @@ -3891,9 +3892,11 @@ def test_get_or_calculate_liquidation_price(mocker, default_conf): # ('kraken', "2021-09-01 00:00:00", "2021-09-01 07:59:59", 30.0, -0.0012443999999999999), # ('kraken', "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, 0.0045759), # ('kraken', "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0008289), + ('ftx', 0, 2, "2021-09-01 00:10:00", "2021-09-01 00:30:00", 30.0, 0.0), ('ftx', 0, 9, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, 0.0010008000000000003), ('ftx', 0, 13, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, 0.0146691), ('ftx', 1, 9, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, 0.0016656000000000002), + ('gateio', 0, 2, "2021-09-01 00:10:00", "2021-09-01 04:00:00", 30.0, 0.0), ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.0009140999999999999), ('gateio', 1, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0002493), @@ -3966,7 +3969,7 @@ def test__fetch_and_calculate_funding_fees( exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange) mocker.patch('freqtrade.exchange.Exchange.timeframes', PropertyMock( - return_value=['1h', '4h', '8h'])) + return_value=['1h', '4h', '8h'])) 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 From 2c89da6bf7b5c07c1a93a9ec76610857c931681f Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 21 Mar 2022 19:30:24 +0100 Subject: [PATCH 2/3] Update code to properly behave when rounding open_date for funding fees --- freqtrade/exchange/exchange.py | 3 +-- tests/exchange/test_exchange.py | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index fe47ca4d1..93190fb2a 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -2302,11 +2302,10 @@ class Exchange: timeframe = self._ft_has['mark_ohlcv_timeframe'] timeframe_ff = self._ft_has.get('funding_fee_timeframe', self._ft_has['mark_ohlcv_timeframe']) - open_date = timeframe_to_prev_date(timeframe, open_date) if not close_date: close_date = datetime.now(timezone.utc) - open_timestamp = int(open_date.timestamp()) * 1000 + open_timestamp = int(timeframe_to_prev_date(timeframe, open_date).timestamp()) * 1000 # close_timestamp = int(close_date.timestamp()) * 1000 mark_comb: PairWithTimeframe = ( diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index aed58a368..265d24569 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -3878,13 +3878,13 @@ def test_get_or_calculate_liquidation_price(mocker, default_conf): @pytest.mark.parametrize('exchange,rate_start,rate_end,d1,d2,amount,expected_fees', [ ('binance', 0, 2, "2021-09-01 01:00:00", "2021-09-01 04:00:00", 30.0, 0.0), - ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), - ('binance', 0, 2, "2021-09-01 00:00:15", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), + ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.00091409999), + ('binance', 0, 2, "2021-09-01 00:00:15", "2021-09-01 08:00:00", 30.0, -0.0002493), ('binance', 1, 2, "2021-09-01 01:00:14", "2021-09-01 08:00:00", 30.0, -0.0002493), ('binance', 1, 2, "2021-09-01 00:00:16", "2021-09-01 08:00:00", 30.0, -0.0002493), - ('binance', 0, 1, "2021-09-01 00:00:00", "2021-09-01 07:59:59", 30.0, -0.0006647999999999999), - ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.0009140999999999999), - ('binance', 0, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), + ('binance', 0, 1, "2021-09-01 00:00:00", "2021-09-01 07:59:59", 30.0, -0.00066479999), + ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.00091409999), + ('binance', 0, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0002493), # TODO: Uncoment once _calculate_funding_fees can pas time_in_ratio to exchange._get_funding_fee # ('kraken', "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0014937), # ('kraken', "2021-09-01 00:00:15", "2021-09-01 08:00:00", 30.0, -0.0008289), @@ -3893,17 +3893,17 @@ def test_get_or_calculate_liquidation_price(mocker, default_conf): # ('kraken', "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, 0.0045759), # ('kraken', "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0008289), ('ftx', 0, 2, "2021-09-01 00:10:00", "2021-09-01 00:30:00", 30.0, 0.0), - ('ftx', 0, 9, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, 0.0010008000000000003), + ('ftx', 0, 9, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, 0.0010008), ('ftx', 0, 13, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, 0.0146691), - ('ftx', 1, 9, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, 0.0016656000000000002), + ('ftx', 0, 9, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, 0.001668), + ('ftx', 1, 9, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, 0.0019932), ('gateio', 0, 2, "2021-09-01 00:10:00", "2021-09-01 04:00:00", 30.0, 0.0), - ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999999999999), - ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.0009140999999999999), + ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 30.0, -0.0009140999), + ('gateio', 0, 2, "2021-09-01 00:00:00", "2021-09-01 12:00:00", 30.0, -0.0009140999), ('gateio', 1, 2, "2021-09-01 00:00:01", "2021-09-01 08:00:00", 30.0, -0.0002493), - ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, -0.0015235000000000001), + ('binance', 0, 2, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, -0.0015235), # TODO: Uncoment once _calculate_funding_fees can pas time_in_ratio to exchange._get_funding_fee # ('kraken', "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, -0.0024895), - ('ftx', 0, 9, "2021-09-01 00:00:00", "2021-09-01 08:00:00", 50.0, 0.0016680000000000002), ]) def test__fetch_and_calculate_funding_fees( mocker, From 247635db79896e07f64241b20a9b47cec28fe577 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 22 Mar 2022 19:28:13 +0100 Subject: [PATCH 3/3] Fix tests --- freqtrade/freqtradebot.py | 4 ++-- tests/test_freqtradebot.py | 20 ++++++++------------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 2d6b46745..2d63cc77f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -276,7 +276,7 @@ class FreqtradeBot(LoggingMixin): pair=trade.pair, amount=trade.amount, is_short=trade.is_short, - open_date=trade.open_date + open_date=trade.open_date_utc ) trade.funding_fees = funding_fees else: @@ -1358,7 +1358,7 @@ class FreqtradeBot(LoggingMixin): pair=trade.pair, amount=trade.amount, is_short=trade.is_short, - open_date=trade.open_date, + open_date=trade.open_date_utc, ) exit_type = 'exit' if sell_reason.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS): diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 6e8b1afbf..dc6c0b838 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -5034,9 +5034,9 @@ def test_update_funding_fees( default_conf['trading_mode'] = 'futures' default_conf['margin_mode'] = 'isolated' - date_midnight = arrow.get('2021-09-01 00:00:00') - date_eight = arrow.get('2021-09-01 08:00:00') - date_sixteen = arrow.get('2021-09-01 16:00:00') + date_midnight = arrow.get('2021-09-01 00:00:00').datetime + date_eight = arrow.get('2021-09-01 08:00:00').datetime + date_sixteen = arrow.get('2021-09-01 16:00:00').datetime columns = ['date', 'open', 'high', 'low', 'close', 'volume'] # 16:00 entry is actually never used # But should be kept in the test to ensure we're filtering correctly. @@ -5119,11 +5119,7 @@ def test_update_funding_fees( trades = Trade.get_open_trades() assert len(trades) == 3 for trade in trades: - assert pytest.approx(trade.funding_fees) == ( - trade.amount * - mark_prices[trade.pair].iloc[0]['open'] * - funding_rates[trade.pair].iloc[0]['open'] * multipl - ) + assert pytest.approx(trade.funding_fees) == 0 mocker.patch('freqtrade.exchange.Exchange.create_order', return_value=open_exit_order) time_machine.move_to("2021-09-01 08:00:00 +00:00") if schedule_off: @@ -5136,8 +5132,8 @@ def test_update_funding_fees( ) assert trade.funding_fees == pytest.approx(sum( trade.amount * - mark_prices[trade.pair].iloc[0:2]['open'] * - funding_rates[trade.pair].iloc[0:2]['open'] * multipl + mark_prices[trade.pair].iloc[1:2]['open'] * + funding_rates[trade.pair].iloc[1:2]['open'] * multipl )) else: @@ -5147,8 +5143,8 @@ def test_update_funding_fees( for trade in trades: assert trade.funding_fees == pytest.approx(sum( trade.amount * - mark_prices[trade.pair].iloc[0:2]['open'] * - funding_rates[trade.pair].iloc[0:2]['open'] * + mark_prices[trade.pair].iloc[1:2]['open'] * + funding_rates[trade.pair].iloc[1:2]['open'] * multipl ))