backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

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

Open finithejernigan opened this issue 2 years ago • 2 comments

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,

finithejernigan avatar Jul 30 '23 15:07 finithejernigan

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. 🤔

kernc avatar Aug 25 '23 11:08 kernc

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.

lachyn21 avatar Feb 12 '24 04:02 lachyn21