What is the alternative for bt.analyzers.BasicTradeStats?
I need some basic stat report for my sample strategy and while searching came across bt.analyzers.BasicTradeStats . It was implemented in 2017 and I think its removed or renamed from future versions.
AttributeError: module 'backtrader.analyzers' has no attribute 'BasicTradeStats'
I went and searched the docs for
BasicTradeStatswhich does not exist there. What is the better alternative for bt.analyzers.BasicTradeStats?
@backtrader14, or anyone else who stumbles across this thread:
you can still get this working if you take the BasicTradeStats analyzer class and import it as a custom analyzer of your own.
To do so, follow the steps here to make modular code for backtrader, substituting "analyzers" for "indicators" where necessary: https://backtest-rookies.com/2017/11/01/backtrader-making-modular-code/
Under your extensions folder, create the analyzers.py file (along with init file), and paste the content from the analyzer found here: https://raw.githubusercontent.com/rich-oregan/backtrader/master/backtrader/analyzers/basictradestats.py
Finally, in your code, make sure you have something like the following lines:
# import the file at the top of your code where "BasicTradeStats" is the name of the class from extensions.analyzers import BasicTradeStats # add the strategy to cerebro cerebro.addanalyzer(BasicTradeStats, _name="BasicTradeStats") opt_runs = cerebro.run(maxcpus=1, runonce=False) for run in opt_runs: for strategy in run: # finally, this will print the analyzer print(strategy.analyzers.BasicTradeStats2.print())