How to record performance and figures from cerebro?
-
I have around a 100 different strategies I am cycling over a years worth of trade weights using the following code,
returns = [] if __name__ == "__main__": for strategy in strategies: allocations = [] print(strategy) dump = pd.read_csv(f'{strategy}.csv') cerebro = bt.Cerebro() ticker_names = list(set([alls[0] for alls in allocations])) targets = {ticker: {} for ticker in ticker_names} for all in allocations: targets[all[0]].update({all[1]: all[2]}) for ticker, target in targets.items(): data = bt.feeds.PandasData( dataname=yahoodict[ticker], timeframe=bt.TimeFrame.Days, fromdate=datelist[0], todate=datelist[-1], ) data.target = target cerebro.adddata(data, name=ticker) cerebro.addstrategy(Strategy) cerebro.broker.setcash(100000) cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='mysharpe') thestrats = cerebro.run() returns.append(cerebro.broker.getvalue())
I am trying to save the results generated from cerebro, but I can only seem to save the returns at the end of the period using getvalue which returns the final value.
Is there a way to check the daily returns or daily portfolio value instead of just the final broker value or some other details so I can understand the performance of my strategies?
I have tried this guide here http://actuarialdatascience.com/backtrader_performance_report.html and used the
cerebro.report('output/dir/for/your/report')
command, but I get an error that there is no such thing as a report function.What can I do?
-
@anarchy89 You want analyzers. Try this general search as well as the docs.
-
@run-out from what i understand, these only show overall performance of the strategies correct, they dont show a daily breakdown?
-
@run-out I tried the analysers from the blog post here, https://www.backtrader.com/blog/posts/2016-07-22-benchmarking/benchmarking/, i dont understand the difference between these 2 lines,
cerebro.addanalyzer(bt.analyzers.TimeReturn, timeframe=bt.TimeFrame.Years, data=data, _name='datareturns') cerebro.addanalyzer(bt.analyzers.TimeReturn, timeframe=bt.TimeFrame.Years) _name='timereturns')
Could you explain it?
-
The standard TimeReturn analyzer has been extended to support tracking a data feed. The 2 major parameters invoved: timeframe (default: None) If None then the complete return over the entire backtested period will be reported Pass TimeFrame.NoTimeFrame to consider the entire dataset with no time constraints data (default: None) Reference asset to track instead of the portfolio value.
So the top analyzer tracks the return for the specific
data
and gives it the namedatareturns
while the next line tracks the return for the whole portfolio and gives it the nametimereturns
, at least that's how i'm reading it.