diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 9f2c8cf37..ede8a9d3c 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -239,12 +239,15 @@ class RPC: trade.pair, side='exit', is_short=trade.is_short, refresh=False) except (PricingError, ExchangeError): current_rate = NAN - if len(trade.select_filled_orders(trade.entry_side)) > 0: - trade_profit = trade.calc_profit(current_rate) - profit_str = f'{trade.calc_profit_ratio(current_rate):.2%}' + trade_profit = NAN + profit_str = f'{NAN:.2%}' else: - trade_profit = 0.0 - profit_str = f'{0.0:.2f}' + if trade.nr_of_successful_entries > 0: + trade_profit = trade.calc_profit(current_rate) + profit_str = f'{trade.calc_profit_ratio(current_rate):.2%}' + else: + trade_profit = 0.0 + profit_str = f'{0.0:.2f}' direction_str = ('S' if trade.is_short else 'L') if nonspot else '' if self._fiat_converter: fiat_profit = self._fiat_converter.convert_amount( @@ -424,8 +427,6 @@ class RPC: for trade in trades: current_rate: float = 0.0 - if not trade.open_rate: - continue if trade.close_date: durations.append((trade.close_date - trade.open_date).total_seconds()) @@ -447,9 +448,13 @@ class RPC: trade.pair, side='exit', is_short=trade.is_short, refresh=False) except (PricingError, ExchangeError): current_rate = NAN - profit_ratio = trade.calc_profit_ratio(rate=current_rate) - profit_abs = trade.calc_profit( - rate=trade.close_rate or current_rate) + trade.realized_profit + if isnan(current_rate): + profit_ratio = NAN + profit_abs = NAN + else: + profit_ratio = trade.calc_profit_ratio(rate=current_rate) + profit_abs = trade.calc_profit( + rate=trade.close_rate or current_rate) + trade.realized_profit profit_all_coin.append(profit_abs) profit_all_ratio.append(profit_ratio) diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 4c580c3c2..1a2428fe7 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -461,46 +461,6 @@ def test_rpc_trade_statistics(default_conf_usdt, ticker, fee, mocker) -> None: assert isnan(stats['profit_all_coin']) -# Test that rpc_trade_statistics can handle trades that lacks -# trade.open_rate (it is set to None) -def test_rpc_trade_statistics_closed(mocker, default_conf_usdt, ticker, fee): - mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price', - return_value=1.1) - mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) - mocker.patch.multiple( - 'freqtrade.exchange.Exchange', - fetch_ticker=ticker, - get_fee=fee, - ) - - freqtradebot = get_patched_freqtradebot(mocker, default_conf_usdt) - patch_get_signal(freqtradebot) - stake_currency = default_conf_usdt['stake_currency'] - fiat_display_currency = default_conf_usdt['fiat_display_currency'] - - rpc = RPC(freqtradebot) - - # Create some test data - create_mock_trades_usdt(fee) - - for trade in Trade.query.order_by(Trade.id).all(): - trade.open_rate = None - - stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency) - assert stats['profit_closed_coin'] == 0 - assert stats['profit_closed_percent_mean'] == 0 - assert stats['profit_closed_fiat'] == 0 - assert stats['profit_all_coin'] == 0 - assert stats['profit_all_percent_mean'] == 0 - assert stats['profit_all_fiat'] == 0 - assert stats['trade_count'] == 7 - assert stats['first_trade_date'] == '2 days ago' - assert stats['latest_trade_date'] == '17 minutes ago' - assert stats['avg_duration'] == '0:00:00' - assert stats['best_pair'] == 'XRP/USDT' - assert stats['best_rate'] == 10.0 - - def test_rpc_balance_handle_error(default_conf, mocker): mock_balance = { 'BTC': {