From 39b2a096abc29280a2f6242a6f39549dbf413052 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 22 Dec 2023 07:06:03 +0100 Subject: [PATCH] Ignore adjust_trade values that would invert position --- freqtrade/freqtradebot.py | 7 ------- tests/test_integration.py | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index c8eafc022..b33554d1c 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -673,13 +673,6 @@ class FreqtradeBot(LoggingMixin): amount = self.exchange.amount_to_contract_precision( trade.pair, abs(float(FtPrecise(stake_amount * trade.leverage) / FtPrecise(current_exit_rate)))) - if amount > trade.amount: - # This is currently ineffective as remaining would become < min tradable - # Fixing this would require checking for 0.0 there - - # if we decide that this callback is allowed to "fully exit" - logger.info( - f"Adjusting amount to trade.amount as it is higher. {amount} > {trade.amount}") - amount = trade.amount if amount == 0.0: logger.info("Amount to exit is 0.0 due to exchange limits - not exiting.") diff --git a/tests/test_integration.py b/tests/test_integration.py index 45c424170..3dcd91af0 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -650,12 +650,27 @@ def test_dca_exiting(default_conf_usdt, ticker_usdt, fee, mocker, caplog, levera caplog.clear() # Sell more than what we got (we got ~20 coins left) - # First adjusts the amount to 20 - then rejects. + # Doesn't exit, as the amount is too high. freqtrade.strategy.adjust_trade_position = MagicMock(return_value=-50) freqtrade.process() - assert log_has_re("Adjusting amount to trade.amount as it is higher.*", caplog) + trade = Trade.get_trades().first() + assert len(trade.orders) == 2 + + # Amount too low... + freqtrade.strategy.adjust_trade_position = MagicMock(return_value=-(trade.stake_amount * 0.99)) + freqtrade.process() + + trade = Trade.get_trades().first() + assert len(trade.orders) == 2 + + # Amount exactly comes out as exactly 0 + freqtrade.strategy.adjust_trade_position = MagicMock( + return_value=-(trade.amount / trade.leverage * 2.02)) + freqtrade.process() + trade = Trade.get_trades().first() assert len(trade.orders) == 3 + assert trade.orders[-1].ft_order_side == 'sell' assert pytest.approx(trade.stake_amount) == 40.198 assert trade.is_open is False