kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.06k stars 990 forks source link

Question - add columns to output of bt.run() #1033

Open finithejernigan opened 11 months ago

finithejernigan commented 11 months ago

Hi,

Great package for backtesting! One question - is it possible to add additional columns to the output of the backtest (such as the indicator values) at the time of the trades? Alternatively, getting the indicator values output for each bar would also be helpful. I could hack it to do it but wondered if there was an easy way.

For example, adding additional columns for each trade with the value of each indicator?

image

Thanks so much for the great package,

kernc commented 10 months ago

This is partly private API, but so far stable ... I think you can use (untested):

indicators = {ind.name: ind
              for ind in stats['_strategy']._indicators}

entry_bars = stats['_trades']['EntryBar']

indicators_df = pd.DataFrame(indicators).iloc[entry_bars]

We might want to include these values in the stats['_trades'] df by default ...

But what about exit_bars ... those values might be relevant for some too. 🤔

lachyn21 commented 4 months ago

Hey I would like to work on this issue as well. I have written this feature on my forked repo for 1D array indicators to be included in the '_trades' dataframe.