Skip to content

Commit

Permalink
Convert winrate to ratio instead of % in calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
froggleston committed Jul 18, 2023
1 parent 6ccc12f commit f95f954
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions freqtrade/rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ def _rpc_trade_statistics(
mean_winning_profit = (winning_profit / winning_trades) if winning_trades > 0 else 0
mean_losing_profit = (abs(losing_profit) / losing_trades) if losing_trades > 0 else 0

winrate = (winning_trades / closed_trade_count) * 100 if closed_trade_count > 0 else 0
loserate = (100 - winrate)
winrate = (winning_trades / closed_trade_count) if closed_trade_count > 0 else 0
loserate = (1 - winrate)

expectancy, expectancy_ratio = self.__calc_expectancy(mean_winning_profit,
mean_losing_profit,
Expand Down Expand Up @@ -630,14 +630,14 @@ def __calc_expectancy(
winrate: float, loserate: float) -> Tuple[float, float]:

expectancy = (
((winrate / 100) * mean_winning_profit) -
((loserate / 100) * mean_losing_profit)
(winrate * mean_winning_profit) -
(loserate * mean_losing_profit)
)

expectancy_ratio = float('inf')
if mean_losing_profit > 0:
expectancy_ratio = (
((1 + (mean_winning_profit / mean_losing_profit)) * (winrate / 100)) - 1
((1 + (mean_winning_profit / mean_losing_profit)) * winrate) - 1
)

return expectancy, expectancy_ratio
Expand Down
2 changes: 1 addition & 1 deletion freqtrade/rpc/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ async def _profit(self, update: Update, context: CallbackContext) -> None:
f"`{first_trade_date}`\n"
f"*Latest Trade opened:* `{latest_trade_date}`\n"
f"*Win / Loss:* `{stats['winning_trades']} / {stats['losing_trades']}`\n"
f"*Winrate:* `{winrate:.2f}%`\n"
f"*Winrate:* `{winrate:.2%}%`\n"
f"*Expectancy (Ratio):* `{expectancy:.2f} ({expectancy_ratio:.2f})`"
)
if stats['closed_trade_count'] > 0:
Expand Down
6 changes: 3 additions & 3 deletions tests/rpc/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,15 @@ def test_rpc_trade_statistics(default_conf_usdt, ticker, fee, mocker) -> None:
assert pytest.approx(stats['profit_all_coin']) == -77.45964918
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.9133333333333327
assert pytest.approx(stats['winrate']) == 0.666666667
assert pytest.approx(stats['expectancy']) == 0.913333333
assert pytest.approx(stats['expectancy_ratio']) == 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_ratio'] == 0.22330888345558253

# Test non-available pair
mocker.patch(f'{EXMS}.get_rate',
Expand Down
4 changes: 2 additions & 2 deletions tests/rpc/test_rpc_apiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ 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, 'winrate': 100.0, 'expectancy': 0.0003695635,
'profit_factor': None, 'winrate': 1.0, 'expectancy': 0.0003695635,
'expectancy_ratio': None, 'trading_volume': 91.074,
}
),
Expand All @@ -861,7 +861,7 @@ 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, 'winrate': 50.0,
'profit_factor': 0.02775724835771106, 'winrate': 0.5,
'expectancy': -0.0027145635000000003, 'expectancy_ratio': -0.48612137582114445,
'trading_volume': 91.074,
}
Expand Down

0 comments on commit f95f954

Please sign in to comment.