From 399f144c2754cb3cd6421186b9a749e82da56db7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 29 Aug 2023 21:29:36 +0200 Subject: [PATCH] more calc_profit_combined usage --- freqtrade/rpc/rpc.py | 28 ++++++++++++++++------------ tests/rpc/test_rpc.py | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 497bc1c82..f0b5d2463 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -174,6 +174,8 @@ class RPC: order: Optional[Order] = None current_profit_fiat: Optional[float] = None total_profit_fiat: Optional[float] = None + total_profit_abs = 0.0 + total_profit_ratio: Optional[float] = None if trade.open_order_id: order = trade.select_order_by_order_id(trade.open_order_id) # calculate profit and send message to user @@ -184,23 +186,22 @@ class RPC: except (ExchangeError, PricingError): current_rate = NAN if len(trade.select_filled_orders(trade.entry_side)) > 0: - current_profit = trade.calc_profit_ratio( - current_rate) if not isnan(current_rate) else NAN - current_profit_abs = trade.calc_profit( - current_rate) if not isnan(current_rate) else NAN + + current_profit = current_profit_abs = current_profit_fiat = NAN + if not isnan(current_rate): + prof = trade.calc_profit_combined(current_rate) + current_profit = prof.profit_ratio + current_profit_abs = prof.profit_abs + total_profit_abs = prof.total_profit + total_profit_ratio = prof.total_profit_ratio else: current_profit = current_profit_abs = current_profit_fiat = 0.0 + else: # Closed trade ... current_rate = trade.close_rate current_profit = trade.close_profit or 0.0 current_profit_abs = trade.close_profit_abs or 0.0 - total_profit_abs = trade.realized_profit + current_profit_abs - total_profit_ratio: Optional[float] = None - if trade.max_stake_amount: - total_profit_ratio = ( - (total_profit_abs / trade.max_stake_amount) * trade.leverage - ) # Calculate fiat profit if not isnan(current_profit_abs) and self._fiat_converter: @@ -216,8 +217,11 @@ class RPC: ) # Calculate guaranteed profit (in case of trailing stop) - stoploss_entry_dist = trade.calc_profit(trade.stop_loss) - stoploss_entry_dist_ratio = trade.calc_profit_ratio(trade.stop_loss) + stop_entry = trade.calc_profit_combined(trade.stop_loss) + + stoploss_entry_dist = stop_entry.profit_abs + stoploss_entry_dist_ratio = stop_entry.profit_ratio + # calculate distance to stoploss stoploss_current_dist = trade.stop_loss - current_rate stoploss_current_dist_ratio = stoploss_current_dist / current_rate diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index d97222adc..70341b37e 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -164,7 +164,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None: response = deepcopy(gen_response) response.update({ 'max_stake_amount': 0.001, - 'total_profit_ratio': pytest.approx(-0.00409), + 'total_profit_ratio': pytest.approx(-0.00409153), }) assert results[0] == response