nautilus_trader icon indicating copy to clipboard operation
nautilus_trader copied to clipboard

win_rate in backtest reports

Open graceyangfan opened this issue 2 years ago • 3 comments

Bug Report

When doing backtest,nautilus_trader provide wrong win_rate.

Expected Behavior

Actual Behavior

The win rate is up to 0.98 during my personal strategy test,but by checking positions it about 0.5.It seems that nautilus_trader compute win_rate with current total pnl instead of realized pnl of every position.

Specifications

  • OS platform:Windows 10 WSL
  • Python version:python 3.9
  • nautilus_trader version:latest develop version.

graceyangfan avatar Aug 26 '22 12:08 graceyangfan

Hi @graceyangfan

I see the current implementation definitely has the intent of using all realized PnLs for the calculation:

class WinRate(PortfolioStatistic):
    """
    Calculates the win rate from a realized PnLs series.
    """

    def calculate_from_realized_pnls(self, realized_pnls: pd.Series) -> Optional[Any]:
        # Preconditions
        if realized_pnls is None or realized_pnls.empty:
            return 0.0

        # Calculate statistic
        winners = [x for x in realized_pnls if x > 0.0]
        losers = [x for x in realized_pnls if x <= 0.0]

        return len(winners) / float(max(1, (len(winners) + len(losers))))

Confirming you calculated a different win rate based on the realized PnLs of your positions?

cjdsellers avatar Aug 26 '22 22:08 cjdsellers

@cjdsellers I just check the the order_fills_report and positions_report ,order_fills_report will provide realized pnl for every position,which compute 0.5 win_rate,but positions_report realized_pnl compute 0.98,since I have confirmed positions_report realized_pnl is the current total pnls,I guess the win_rate computing is wrong If you pass the positions_report realized pnl list to WinRate

graceyangfan avatar Aug 27 '22 00:08 graceyangfan

Well this is interesting, it depends if you want the win rate of positions as an end-to-end trade (opened-to-closed), which is how I would think of it, and whats currently implemented.

Or, the win rate of all the incremental trades which were involved in a positions life. Possibly this calls for a separate statistic so both are calculated :thinking:

cjdsellers avatar Aug 27 '22 01:08 cjdsellers

@cjdsellers it seems that this error has been fixed since #933

graceyangfan avatar Jan 25 '23 02:01 graceyangfan