Fix fee handling for futures trades

This commit is contained in:
Matthias 2022-05-03 18:03:45 +02:00
parent 2c750fdb09
commit 5b76ae452f
2 changed files with 23 additions and 8 deletions

View File

@ -1613,7 +1613,9 @@ class Exchange:
order['fee']['cost'] / safe_value_fallback2(order, order, 'filled', 'amount'), 8) order['fee']['cost'] / safe_value_fallback2(order, order, 'filled', 'amount'), 8)
elif fee_curr in self.get_pair_quote_currency(order['symbol']): elif fee_curr in self.get_pair_quote_currency(order['symbol']):
# Quote currency - divide by cost # 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: else:
# If Fee currency is a different currency # If Fee currency is a different currency
if not order['cost']: if not order['cost']:
@ -1628,7 +1630,8 @@ class Exchange:
fee_to_quote_rate = self._config['exchange'].get('unknown_fee_rate', None) fee_to_quote_rate = self._config['exchange'].get('unknown_fee_rate', None)
if not fee_to_quote_rate: if not fee_to_quote_rate:
return None 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]]: def extract_cost_curr_rate(self, order: Dict) -> Tuple[float, str, Optional[float]]:
""" """

View File

@ -4151,7 +4151,10 @@ def test__order_contracts_to_amount(
'cost': 60.0, 'cost': 60.0,
'filled': None, 'filled': None,
'remaining': 30.0, 'remaining': 30.0,
'fee': 0.06, 'fee': {
'currency': 'USDT',
'cost': 0.06,
},
'fees': [{ 'fees': [{
'currency': 'USDT', 'currency': 'USDT',
'cost': 0.06, 'cost': 0.06,
@ -4178,7 +4181,10 @@ def test__order_contracts_to_amount(
'cost': 80.0, 'cost': 80.0,
'filled': None, 'filled': None,
'remaining': 40.0, 'remaining': 40.0,
'fee': 0.08, 'fee': {
'currency': 'USDT',
'cost': 0.08,
},
'fees': [{ 'fees': [{
'currency': 'USDT', 'currency': 'USDT',
'cost': 0.08, 'cost': 0.08,
@ -4212,12 +4218,18 @@ def test__order_contracts_to_amount(
'info': {}, '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]) assert order2['amount'] == order2_bef['amount'] * contract_size
order2 = exchange._order_contracts_to_amount(orders[1]) assert order2['cost'] == order2_bef['cost'] * contract_size
# Don't fail
exchange._order_contracts_to_amount(orders[2]) 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', [ @pytest.mark.parametrize('pair,contract_size,trading_mode', [