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/

    Plotting with Optimizer not working

    General Code/Help
    3
    6
    175
    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.
    • dracount
      dracount last edited by

      Hi,

      I am having trouble with getting plotting to work:

      I have tried:

          print('run begin')
          optimized_runs = cerebro.run(maxcpus=1)
          print('runs completed: ' + str(len(optimized_runs)))
          optimized_runs[0][0].plot()
      

      with error
      return getattr(self.lines, name) AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'plot'

      also:

          print('run begin')
          optimized_runs = cerebro.run(maxcpus=1)
          print('runs completed: ' + str(len(optimized_runs)))
          #optimized_runs[0][0].plot()
      
          for optimized_run in optimized_runs:
              optimized_run.plot()
      

      with error: AttributeError: 'list' object has no attribute 'plot'

      and

          print('run begin')
          optimized_runs = cerebro.run(maxcpus=1)
          print('runs completed: ' + str(len(optimized_runs)))
          
          for optimized_run in optimized_runs:
              for strategy in optimized_run:
                  strategy.plot()
      

      with error
      AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'plot'

      Any help would be greatly appreciated!

      Thank you

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        Not sure plotting was designed to work with optstrategy: https://community.backtrader.com/topic/409/optstrategy-and-plotting

        1 Reply Last reply Reply Quote 1
        • Kevin Parker
          Kevin Parker last edited by

          I've just taken a look at backtrader yesterday, I believe you need to take the strategy again into a new cerbro instance and plot that.

          For example:

          ...

          results = cerebro.optstrategy(SomeStrategy, ...)

          results_ordered = []
          
          for run in results:
              for strategy in run:
                  print('--> Params', strategy.params)
                  results_ordered.append(strategy)
          
          results_ordered = sorted(results_ordered, key=lambda x: x.broker.get_value(), reverse=True)
          
          for strategy in results_ordered:
              print('--> Strategy {} {}', strategy.broker.get_value(), strategy.params.__dict__)
          
          cerebro = bt.Cerebro()
          
          cerebro.addstrategy(SomeStrategy, **results_ordered[0].params.__dict__)
          cerebro.adddata(data)
          cerebro.run()
          cerebro.plot()
          
          Kevin Parker 1 Reply Last reply Reply Quote 1
          • Kevin Parker
            Kevin Parker @Kevin Parker last edited by

            @Kevin-Parker

            So this is wrong, not sure why the first strategy result in run is a new instance of MyStrategy, with the defaults, but it is.

                results_ordered = []
                        
                        for run in results:
                            results_ordered.append(run[1])
                         
                        results_ordered = sorted(results_ordered, key=lambda x: x.broker.get_value(), reverse=True)
                        
                        for result in results_ordered[:10]:
                            print('--> Result', result.broker.get_value(), result.params.__dict__)
                        
                        print('--> Winning strategy %s %s' % (results_ordered[0].broker.get_value(), results_ordered[0].params.__dict__))
            
            dracount 1 Reply Last reply Reply Quote 0
            • dracount
              dracount @Kevin Parker last edited by

              @Kevin-Parker Thanks for the tip - please let me know if you work it out

              Kevin Parker 1 Reply Last reply Reply Quote 0
              • Kevin Parker
                Kevin Parker @dracount last edited by

                @dracount I did, see the code example.

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