diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index c68bd7d92..626cbd208 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -530,8 +530,8 @@ class RPC: loserate = (100 - winrate) expectancy = 1.0 - if mean_winning_profit > 0 and mean_losing_profit > 0: - expectancy = (1 + (mean_winning_profit / mean_losing_profit)) * (winrate / 100) - 1 + if mean_winning_profit > 0 and abs(mean_losing_profit) > 0: + expectancy = (1 + (mean_winning_profit / abs(mean_losing_profit))) * (winrate / 100) - 1 else: if mean_winning_profit == 0: expectancy = 0.0 diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 1c59748ed..47a5b424a 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -416,13 +416,13 @@ def test_rpc_trade_statistics(default_conf_usdt, ticker, fee, mocker) -> None: assert pytest.approx(stats['profit_all_percent_mean']) == -57.86 assert pytest.approx(stats['profit_all_fiat']) == -85.205614098 assert pytest.approx(stats['winrate']) == 66.666666667 + assert pytest.approx(stats['expectancy']) == 0.223308883 assert stats['trade_count'] == 7 assert stats['first_trade_humanized'] == '2 days ago' assert stats['latest_trade_humanized'] == '17 minutes ago' assert stats['avg_duration'] in ('0:17:40') assert stats['best_pair'] == 'XRP/USDT' assert stats['best_rate'] == 10.0 - assert stats['expectancy'] == 1.0 assert stats['expectancy_rate'] == 3.64 # Test non-available pair diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index f793b1f9c..9de97159e 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -829,7 +829,8 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): 'profit_closed_percent_mean': -0.75, 'profit_closed_ratio_sum': -0.015, 'profit_closed_percent_sum': -1.5, 'profit_closed_ratio': -6.739057628404269e-06, 'profit_closed_percent': -0.0, 'winning_trades': 0, 'losing_trades': 2, - 'profit_factor': 0.0, 'trading_volume': 91.074, + 'profit_factor': 0.0, 'winrate':0.0, 'expectancy': 0.0, 'expectancy_rate': 0.0033695635, + 'trading_volume': 91.074, } ), ( @@ -844,7 +845,8 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): 'profit_closed_percent_mean': 0.75, 'profit_closed_ratio_sum': 0.015, 'profit_closed_percent_sum': 1.5, 'profit_closed_ratio': 7.391275897987988e-07, 'profit_closed_percent': 0.0, 'winning_trades': 2, 'losing_trades': 0, - 'profit_factor': None, 'trading_volume': 91.074, + 'profit_factor': None, 'winrate':100.0, 'expectancy': 1.0, 'expectancy_rate': 0.0003695635, + 'trading_volume': 91.074, } ), ( @@ -859,7 +861,8 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): 'profit_closed_percent_mean': 0.25, 'profit_closed_ratio_sum': 0.005, 'profit_closed_percent_sum': 0.5, 'profit_closed_ratio': -5.429078808526421e-06, 'profit_closed_percent': -0.0, 'winning_trades': 1, 'losing_trades': 1, - 'profit_factor': 0.02775724835771106, 'trading_volume': 91.074, + 'profit_factor': 0.02775724835771106, 'winrate':50.0, 'expectancy': -0.48612137582114445, + 'expectancy_rate': 0.0028695635, 'trading_volume': 91.074, } ) ]) @@ -916,6 +919,9 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, is_short, expected) 'winning_trades': expected['winning_trades'], 'losing_trades': expected['losing_trades'], 'profit_factor': expected['profit_factor'], + 'winrate': expected['winrate'], + 'expectancy': expected['expectancy'], + 'expectancy_rate': expected['expectancy_rate'], 'max_drawdown': ANY, 'max_drawdown_abs': ANY, 'trading_volume': expected['trading_volume'],