From 1dd56e35d502ebf31a25584eed226b5f040f70a8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 16 Aug 2022 08:21:02 +0200 Subject: [PATCH] Ensure comparisions align when closing a trade --- freqtrade/persistence/trade_model.py | 7 ++++--- tests/test_integration.py | 10 ++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index 436919bb1..25b290bfb 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -875,13 +875,14 @@ class LocalTrade(): self.realized_profit = close_profit_abs self.close_profit_abs = profit - if current_amount > ZERO: + current_amount_tr = amount_to_precision(float(current_amount), + self.amount_precision, self.precision_mode) + if current_amount_tr > 0.0: # Trade is still open # Leverage not updated, as we don't allow changing leverage through DCA at the moment. self.open_rate = price_to_precision(float(current_stake / current_amount), self.price_precision, self.precision_mode) - self.amount = amount_to_precision(float(current_amount), - self.amount_precision, self.precision_mode) + self.amount = current_amount_tr self.stake_amount = float(current_stake) / (self.leverage or 1.0) self.fee_open_cost = self.fee_open * float(current_stake) self.recalc_open_trade_value() diff --git a/tests/test_integration.py b/tests/test_integration.py index 0b2639879..dd3488f81 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -220,8 +220,6 @@ def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None: 'freqtrade.exchange.Exchange', fetch_ticker=ticker_usdt, get_fee=fee, - amount_to_precision=lambda s, x, y: y, - price_to_precision=lambda s, x, y: y, ) patch_get_signal(freqtrade) @@ -249,7 +247,7 @@ def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None: assert len(trade.orders) == 2 for o in trade.orders: assert o.status == "closed" - assert trade.stake_amount == 120 + assert pytest.approx(trade.stake_amount) == 120 # Open-rate averaged between 2.0 and 2.0 * 0.995 assert trade.open_rate < 2.0 @@ -259,9 +257,9 @@ def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None: freqtrade.process() trade = Trade.get_trades().first() assert len(trade.orders) == 2 - assert trade.stake_amount == 120 + assert pytest.approx(trade.stake_amount) == 120 assert trade.orders[0].amount == 30 - assert trade.orders[1].amount == 60 / ticker_usdt_modif['bid'] + assert pytest.approx(trade.orders[1].amount) == 60 / ticker_usdt_modif['bid'] assert pytest.approx(trade.amount) == trade.orders[0].amount + trade.orders[1].amount assert trade.nr_of_successful_buys == 2 @@ -274,7 +272,7 @@ def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None: assert trade.is_open is False assert trade.orders[0].amount == 30 assert trade.orders[0].side == 'buy' - assert trade.orders[1].amount == 60 / ticker_usdt_modif['bid'] + assert pytest.approx(trade.orders[1].amount) == 60 / ticker_usdt_modif['bid'] # Sold everything assert trade.orders[-1].side == 'sell' assert trade.orders[2].amount == trade.amount