Skip to content
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

Way to access equity values, individual trade returns, ... #4

Closed
fabio11c opened this issue May 22, 2019 · 5 comments · Fixed by #47
Closed

Way to access equity values, individual trade returns, ... #4

fabio11c opened this issue May 22, 2019 · 5 comments · Fixed by #47
Labels
API Needs API-related discussion enhancement New feature or request

Comments

@fabio11c
Copy link

Is there a way to extract the returns of the strategy and not only have a plot?

@kernc kernc added the question Not a bug, but a FAQ entry label May 23, 2019
@kernc
Copy link
Owner

kernc commented May 23, 2019

bt = Backtest(...)
result = bt.run()

result._trade_data  # type: pd.DataFrame

In _trade_data, you can find columns such as changing Equity values, P/L of individual trades, ...

result._trade_data['Equity']
result._trade_data['P/L']

df = pd.DataFrame()
df['Equity'] = pd.Series(broker.log.equity).bfill().fillna(broker._cash)
equity = df.Equity.values
df['Exit Entry'] = broker.log.exit_entry
exits = df['Exit Entry']
df['Exit Position'] = broker.log.exit_position
df['Entry Price'] = broker.log.entry_price
df['Exit Price'] = broker.log.exit_price
df['P/L'] = broker.log.pl
pl = df['P/L']
df['Returns'] = returns = pl.dropna() / equity[exits.dropna().values.astype(int)]
df['Drawdown'] = dd = 1 - equity / np.maximum.accumulate(equity)
dd_dur, dd_peaks = _drawdown_duration_peaks(dd, data.index)
df['Drawdown Duration'] = dd_dur
dd_dur = df['Drawdown Duration']
df.index = data.index

Note, this is currently private API, so it may change.

@kernc kernc changed the title Question about equity. Way to access equity values, individual trade returns, ... May 23, 2019
@fabio11c
Copy link
Author

Ok, thanks...what do you mean with a "private API"?

@fabio11c
Copy link
Author

result._trade_data is not an atribute of bt.run(). Im still having the issue.

@kernc
Copy link
Owner

kernc commented May 23, 2019

bt.run()._trade_data

It should be. It's the way plotting functions access raw trading data:

s._trade_data = df # Private API

orig_trade_data = trade_data = results._trade_data.copy(False)

@kernc
Copy link
Owner

kernc commented May 23, 2019

what do you mean with a "private API"?

It's undocumented API, without reliability (change) guarantees, not meant for public consumption. It's why the attribute is prefixed with an underscore, following a common Python convention.

You are welcome to use it in testing, though. Maybe even let me know how you are using it and whether you need/expect something else from/like it too. 👍

Repository owner deleted a comment from algomaschine Jul 4, 2019
@kernc kernc added the API Needs API-related discussion label Jul 5, 2019
@kernc kernc closed this as completed in #47 Jul 15, 2020
@kernc kernc added enhancement New feature or request and removed question Not a bug, but a FAQ entry labels Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Needs API-related discussion enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants