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/

    Cerebro forloop reuse?

    General Code/Help
    2
    3
    187
    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.
    • A
      appypollyloggies last edited by

      I'm trying to reuse cerebro inside a for loop, something like this:

          for item in x:
      
              cerebro = bt.Cerebro()
      
              strategy = SmaCross(item[0],item[1])
      
              cerebro.addstrategy(strategy)
      
              data0 = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2018, 1, 1),
                                                todate=datetime(2020, 12, 31))
              cerebro.adddata(data0)
      
              cerebro.run()
      

      However im arriving at a strange error:

      _obj._id = cerebro._next_stid()
      AttributeError: 'NoneType' object has no attribute '_next_stid'
      

      Which is fine, since only one strategy gets passed (before we're supposedly creating a new obj of cerebro) - it shouldnt have an id for a next strategy. I've read up on the reuse, and im not sure the Strategy Factory approach i read in the other thread will help, as the strategy inputs need to be dynamic and not predefined like in the example. Meaning, the inputs for the SMA need to change after every iteration of the loop.

      Any help with this? Loving the lib so far :)

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @appypollyloggies last edited by

        @appypollyloggies said in Cerebro forloop reuse?:

        strategy = SmaCross(item[0],item[1])

        I think your problem lies here. You are instantiating the strategy class. The strategy class is passed into cerebro as a class and with it the kwargs. Something like this.

        for item in x:
        
                cerebro = bt.Cerebro()
        
                kwargs = dict(
                     param1 = item[0],
                     param2 = item[1], 
                )
        
                cerebro.addstrategy(SmaCross, **kwargs)
        
                data0 = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2018, 1, 1),
                                                  todate=datetime(2020, 12, 31))
                cerebro.adddata(data0)
        
                cerebro.run()
        

        RunBacktest.com

        A 1 Reply Last reply Reply Quote 5
        • A
          appypollyloggies @run-out last edited by

          @run-out Amazing, thank you!

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