Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    What is the alternative for bt.analyzers.BasicTradeStats?

    General Code/Help
    3
    3
    516
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      backtrader14 last edited by backtrader14

      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 BasicTradeStats which does not exist there. What is the better alternative for bt.analyzers.BasicTradeStats?

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        If you are looking for trade stats, than you may want to use TradeAnalyzer or Closed trade list (including MFE/MAE) analyzer.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 3
        • C
          csph_alpaca last edited by

          @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())
          

          Works great!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors