Ensure comparisions align when closing a trade

This commit is contained in:
Matthias 2022-08-16 08:21:02 +02:00
parent e4b7bcaeab
commit 1dd56e35d5
2 changed files with 8 additions and 9 deletions

View File

@ -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()

View File

@ -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