(Re-)compute strategy performance metrics. `stats` is the statistics series as returned by `backtesting.backtesting.Backtest.run()`. `data` is OHLC data as passed to the `backtesting.backtesting.Backtest` the `stats` were obtained in. `trades` can be a dataframe subset of `stat
(
*,
stats: pd.Series,
data: pd.DataFrame,
trades: pd.DataFrame = None,
risk_free_rate: float = 0.)
| 172 | |
| 173 | |
| 174 | def compute_stats( |
| 175 | *, |
| 176 | stats: pd.Series, |
| 177 | data: pd.DataFrame, |
| 178 | trades: pd.DataFrame = None, |
| 179 | risk_free_rate: float = 0.) -> pd.Series: |
| 180 | """ |
| 181 | (Re-)compute strategy performance metrics. |
| 182 | |
| 183 | `stats` is the statistics series as returned by `backtesting.backtesting.Backtest.run()`. |
| 184 | `data` is OHLC data as passed to the `backtesting.backtesting.Backtest` |
| 185 | the `stats` were obtained in. |
| 186 | `trades` can be a dataframe subset of `stats._trades` (e.g. only long trades). |
| 187 | You can also tune `risk_free_rate`, used in calculation of Sharpe and Sortino ratios. |
| 188 | |
| 189 | >>> stats = Backtest(GOOG, MyStrategy).run() |
| 190 | >>> only_long_trades = stats._trades[stats._trades.Size > 0] |
| 191 | >>> long_stats = compute_stats(stats=stats, trades=only_long_trades, |
| 192 | ... data=GOOG, risk_free_rate=.02) |
| 193 | """ |
| 194 | equity = stats._equity_curve.Equity |
| 195 | if trades is None: |
| 196 | trades = stats._trades |
| 197 | else: |
| 198 | # XXX: Is this buggy? |
| 199 | equity = equity.copy() |
| 200 | equity[:] = stats._equity_curve.Equity.iloc[0] |
| 201 | for t in trades.itertuples(index=False): |
| 202 | equity.iloc[t.EntryBar:] += t.PnL |
| 203 | return _compute_stats(trades=trades, equity=equity.values, ohlc_data=data, |
| 204 | risk_free_rate=risk_free_rate, strategy_instance=stats._strategy) |
| 205 | |
| 206 | |
| 207 | def resample_apply(rule: str, |
no outgoing calls