Improve fee handling

closes #7586
This commit is contained in:
Matthias 2022-10-21 16:30:01 +02:00
parent 410a744ee9
commit b715d9c521
2 changed files with 10 additions and 6 deletions

View File

@ -1471,12 +1471,13 @@ class FreqtradeBot(LoggingMixin):
) )
return cancelled return cancelled
def _safe_exit_amount(self, pair: str, amount: float) -> float: def _safe_exit_amount(self, trade: Trade, pair: str, amount: float) -> float:
""" """
Get sellable amount. Get sellable amount.
Should be trade.amount - but will fall back to the available amount if necessary. Should be trade.amount - but will fall back to the available amount if necessary.
This should cover cases where get_real_amount() was not able to update the amount This should cover cases where get_real_amount() was not able to update the amount
for whatever reason. for whatever reason.
:param trade: Trade we're working with
:param pair: Pair we're trying to sell :param pair: Pair we're trying to sell
:param amount: amount we expect to be available :param amount: amount we expect to be available
:return: amount to sell :return: amount to sell
@ -1495,6 +1496,7 @@ class FreqtradeBot(LoggingMixin):
return amount return amount
elif wallet_amount > amount * 0.98: elif wallet_amount > amount * 0.98:
logger.info(f"{pair} - Falling back to wallet-amount {wallet_amount} -> {amount}.") logger.info(f"{pair} - Falling back to wallet-amount {wallet_amount} -> {amount}.")
trade.amount = wallet_amount
return wallet_amount return wallet_amount
else: else:
raise DependencyException( raise DependencyException(
@ -1553,7 +1555,7 @@ class FreqtradeBot(LoggingMixin):
# Emergency sells (default to market!) # Emergency sells (default to market!)
order_type = self.strategy.order_types.get("emergency_exit", "market") order_type = self.strategy.order_types.get("emergency_exit", "market")
amount = self._safe_exit_amount(trade.pair, sub_trade_amt or trade.amount) amount = self._safe_exit_amount(trade, trade.pair, sub_trade_amt or trade.amount)
time_in_force = self.strategy.order_time_in_force['exit'] time_in_force = self.strategy.order_time_in_force['exit']
if (exit_check.exit_type != ExitType.LIQUIDATION if (exit_check.exit_type != ExitType.LIQUIDATION
@ -1828,7 +1830,7 @@ class FreqtradeBot(LoggingMixin):
never in base currency. never in base currency.
""" """
self.wallets.update() self.wallets.update()
amount_ = amount amount_ = trade.amount
if order_obj.ft_order_side == trade.exit_side or order_obj.ft_order_side == 'stoploss': if order_obj.ft_order_side == trade.exit_side or order_obj.ft_order_side == 'stoploss':
# check against remaining amount! # check against remaining amount!
amount_ = trade.amount - amount amount_ = trade.amount - amount

View File

@ -3969,15 +3969,17 @@ def test__safe_exit_amount(default_conf_usdt, fee, caplog, mocker, amount_wallet
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
if has_err: if has_err:
with pytest.raises(DependencyException, match=r"Not enough amount to exit trade."): with pytest.raises(DependencyException, match=r"Not enough amount to exit trade."):
assert freqtrade._safe_exit_amount(trade.pair, trade.amount) assert freqtrade._safe_exit_amount(trade, trade.pair, trade.amount)
else: else:
wallet_update.reset_mock() wallet_update.reset_mock()
assert freqtrade._safe_exit_amount(trade.pair, trade.amount) == amount_wallet assert trade.amount != amount_wallet
assert freqtrade._safe_exit_amount(trade, trade.pair, trade.amount) == amount_wallet
assert log_has_re(r'.*Falling back to wallet-amount.', caplog) assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
assert trade.amount == amount_wallet
assert wallet_update.call_count == 1 assert wallet_update.call_count == 1
caplog.clear() caplog.clear()
wallet_update.reset_mock() wallet_update.reset_mock()
assert freqtrade._safe_exit_amount(trade.pair, amount_wallet) == amount_wallet assert freqtrade._safe_exit_amount(trade, trade.pair, amount_wallet) == amount_wallet
assert not log_has_re(r'.*Falling back to wallet-amount.', caplog) assert not log_has_re(r'.*Falling back to wallet-amount.', caplog)
assert wallet_update.call_count == 1 assert wallet_update.call_count == 1