-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
ENH: Extract strategy performance method compute_stats #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add long/short trade statistics
- Need info about which metrics we want (pips, duration, wins, losses...etc)
I'm not in favor of supplying extra new keys in the stats series. It's long as it is.
Instead the idea is, the curious user will be able to use the newly exposed backtesting.lib.compute_stats()
to compute stats on any subset of trades
they are interested in:
stats = bt.run()
long_trades = stats._trades.query('Size > 0')
long_stats = compute_stats(long_trades, stats._equity_curve.Equity, data)
Maybe even better to expose compute_stats(trades: pd.DataFrame, # trades subset
stats: pd.Series, # previous stats
risk_free_rate: float = 0) -> pd.Series and obtain equity and OHLC data simply from stats passed in. |
Particularly OHLC data is potentially too heavy (e.g. pickling for multiprocessing) to bake into returned stats. Better pass that separately. |
I agree, it would be better to provide the information and let users calculate metrics they need. I wasn't sure if we had to add the long/short stats based on the original issue conversation. |
backtesting/_stats.py
Outdated
def compute_stats( | ||
trades: List[pd.DataFrame], | ||
equity: np.ndarray, | ||
ohlc_data: pd.DataFrame, | ||
risk_free_rate: float = 0) -> pd.Series: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good. Now let's expose+document this function publicly in backtesting.lib
. The user will be able to pass trades: pd.DataFrame
as obtained by this exact method (stats._trades
), so you have to figure out a way for that.
Co-authored-by: kernc <kerncece@gmail.com>
Co-authored-by: kernc <kerncece@gmail.com>
backtesting/lib.py
Outdated
>>> broker = stats._strategy._broker | ||
>>> equity = pd.Series(broker._equity).bfill().fillna(broker._cash).values | ||
>>> perf = compute_stats(trades=broker.closed_trades, equity=equity, ohlc_data=GOOG) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kernc passing stats._strategy._broker.closed_trades
makes it compatible as is. If we want to pass stats._trades
, we'd need to convert the DataFrame
to a list of Trade
.
Oops 😛 |
@crazy25000 Apologies for the late review. 🌞 Many thanks for your work on this! 🥮 |
Resolves #194
Resolves #71
Extracted methods
compute_drawdown_duration_peaks
,compute_stats
, and class_Stats
frombacktesting.py
to a new file so that they're available to users.This will give us the ability to explicitly get performance metrics and have direct access to the data.
TODO:
flake8
passesmypy
passesAdd long/short trade statisticsNeed info about which metrics we want (pips, duration, wins, losses...etc)