From 5b76ae452f5aa2599d0e9ab53348f25155319780 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 3 May 2022 18:03:45 +0200 Subject: [PATCH] Fix fee handling for futures trades --- freqtrade/exchange/exchange.py | 7 +++++-- tests/exchange/test_exchange.py | 24 ++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 82dcacb51..b300156b5 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1613,7 +1613,9 @@ class Exchange: order['fee']['cost'] / safe_value_fallback2(order, order, 'filled', 'amount'), 8) elif fee_curr in self.get_pair_quote_currency(order['symbol']): # Quote currency - divide by cost - return round(order['fee']['cost'] / order['cost'], 8) if order['cost'] else None + return round(self._contracts_to_amount( + order['symbol'], order['fee']['cost']) / order['cost'], + 8) if order['cost'] else None else: # If Fee currency is a different currency if not order['cost']: @@ -1628,7 +1630,8 @@ class Exchange: fee_to_quote_rate = self._config['exchange'].get('unknown_fee_rate', None) if not fee_to_quote_rate: return None - return round((order['fee']['cost'] * fee_to_quote_rate) / order['cost'], 8) + return round((self._contracts_to_amount( + order['symbol'], order['fee']['cost']) * fee_to_quote_rate) / order['cost'], 8) def extract_cost_curr_rate(self, order: Dict) -> Tuple[float, str, Optional[float]]: """ diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 689ffa4ce..9faf404cd 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -4151,7 +4151,10 @@ def test__order_contracts_to_amount( 'cost': 60.0, 'filled': None, 'remaining': 30.0, - 'fee': 0.06, + 'fee': { + 'currency': 'USDT', + 'cost': 0.06, + }, 'fees': [{ 'currency': 'USDT', 'cost': 0.06, @@ -4178,7 +4181,10 @@ def test__order_contracts_to_amount( 'cost': 80.0, 'filled': None, 'remaining': 40.0, - 'fee': 0.08, + 'fee': { + 'currency': 'USDT', + 'cost': 0.08, + }, 'fees': [{ 'currency': 'USDT', 'cost': 0.08, @@ -4212,12 +4218,18 @@ def test__order_contracts_to_amount( 'info': {}, }, ] + order1_bef = orders[0] + order2_bef = orders[1] + order1 = exchange._order_contracts_to_amount(deepcopy(order1_bef)) + order2 = exchange._order_contracts_to_amount(deepcopy(order2_bef)) + assert order1['amount'] == order1_bef['amount'] * contract_size + assert order1['cost'] == order1_bef['cost'] * contract_size - order1 = exchange._order_contracts_to_amount(orders[0]) - order2 = exchange._order_contracts_to_amount(orders[1]) + assert order2['amount'] == order2_bef['amount'] * contract_size + assert order2['cost'] == order2_bef['cost'] * contract_size + + # Don't fail exchange._order_contracts_to_amount(orders[2]) - assert order1['amount'] == 30.0 * contract_size - assert order2['amount'] == 40.0 * contract_size @pytest.mark.parametrize('pair,contract_size,trading_mode', [