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.04k stars 987 forks source link

Get Balance and Equity evolution over time #1145

Closed ironhak closed 1 month ago

ironhak commented 1 month ago
bt = Backtest(initialiseData('ES=F'),DonchainBreakout,cash=10_000)
stats = bt.run()
print(stats_trades)

This only return a dataframe with the list of trades taken, I would like to have a dataframe with equity and balance for every point in time of my original time series data.

How can I do this???

ironhak commented 1 month ago

Solved like this:

class Test(Strategy):

    equity_ = pd.DataFrame()

    def init(self):

    def next(self):

        self.equity_.loc[len(self.data), 'Date'] = self.data.index[-1]
        self.equity_.loc[len(self.data), 'Equity'] = self.equity 

bt = Backtest(df,DonchainBreakout,cash=10_000)
stats = bt.run()
equity_df = bt._strategy.equity_