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/

    I am struggling to save my backtrader optimizer results(params and analyzers) into a CSV file

    General Discussion
    2
    4
    156
    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.
    • N
      NikolaTesla last edited by

      Hello, I am trying to run a back trader strategy and right now working on optimizing it. Even after referring to https://community.backtrader.com/topic/174/create-a-csv-of-the-result-of-optstrategy/8 I find it difficult to print(and save to csv) the params and analyzers The problem I face is that after using the following code,

      def stop(self):
          self.cash_start = self.broker.get_value()
          self.cash_final = self.broker.get_cash()
          self.draw_down = self.analyzers.drawdown.get_analysis()
      
          self.log(f"Period: {self.p.period}, Abs Margin is {self.p.abs_margin}, trailstop is {self.p.trail_stop}, Vol_Max is {self.p.Vol_Max} and the final value is {self.broker.getvalue()}", doprint=True)
      
      def main():
         ..
         ..
         ..
          strats = cerebro.optstrategy(
              strategy_name, period = [10, 100, 100], ........ 
              )
      
          workers = cerebro.run(maxcpus=48)  
      
          all_strategies = [r[0] for r in workers]
      
          all_results = [str(s.cash_start) for s in all_strategies]
          print(all_results)
      
      

      But the issue is that I get an Attribute error that OptRetur object has no attribute 'cash_start'. The error is as follows and can someone please help me figure out where am I going wrong??

      AttributeError: 'OptReturn' object has no attribute 'cash_start'
      
      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        please go over the following docs:

        https://www.backtrader.com/docu/cerebro/#returning-the-results
        https://www.backtrader.com/docu/optimization-improvements/#results-management

        By default the optreturn Cerebro parameter is set to True - meaning that run method will not return the list of Strategy objects but a stripped down OptReturn objects containing only the original strategy parameters and analyzers.

        Quoting the description of the optreturn parameter:

        ``optreturn`` (default: ``True``)
        
                If ``True`` the optimization results will not be full ``Strategy``
                objects (and all *datas*, *indicators*, *observers* ...) but and object
                with the following attributes (same as in ``Strategy``):
        
                  - ``params`` (or ``p``) the strategy had for the execution
                  - ``analyzers`` the strategy has executed
        
                In most occassions, only the *analyzers* and with which *params* are
                the things needed to evaluate a the performance of a strategy. If
                detailed analysis of the generated values for (for example)
                *indicators* is needed, turn this off
        

        The best way of getting the optimization statistics is to use the analyzer objects that will collect and report such statistics. There are two benefits using the analyzers :

        • Modular and easy to understand design
        • Some performance benefits (up to 15% according to the docs ) in execution time.
        N 1 Reply Last reply Reply Quote 1
        • N
          NikolaTesla last edited by

          @vladisld said in I am struggling to save my backtrader optimizer results(params and analyzers) into a CSV file:

          By default the optreturn Cerebro parameter is set to True - meaning that run method will not return the list of Strategy objects but a stripped down OptReturn objects containing only the original strategy parameters and analyzers.

          Hey, Thank you so much for your response. This totally solved the errors I was facing

          1 Reply Last reply Reply Quote 0
          • N
            NikolaTesla @vladisld last edited by

            @vladisld said in I am struggling to save my backtrader optimizer results(params and analyzers) into a CSV file:

            The best way of getting the optimization statistics is to use the analyzer objects that will collect and report such statistics. There are two benefits using the analyzers :

            Modular and easy to understand design
            Some performance benefits (up to 15% according to the docs ) in execution time.

            What about this? Can you refer me to an example regarding this? I mean the analyzer objects which were used in some code or something? I am sorry but looking at an example helps me understand things better. Thank you for your time :)

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }