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/

    Dynamic parameters passed to cerebro.optstrategy()

    General Discussion
    2
    4
    465
    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.
    • L
      lampalork last edited by

      Dear All,
      I'm familiar with the following syntax:

      cerebro.optstrategy(MyHolyGrailStrategy, period=range(1, 20), )
      

      But how can I call optstrategy when the parameters to be optimized and their respective ranges are only known at runtime?
      For addstrategy(), I'm using something like

      cerebro.addstrategy(MyHolyGrailStrategy, parameters)
      

      where parameters is a dictionnary that is received by the _init method of my strategy

      def __init__(self, params):
      

      But the same approach does not seem to be working here.

      Many thanks in advance
      Kind regards,
      Lamp'

      1 Reply Last reply Reply Quote 0
      • L
        lampalork last edited by

        ok, found what I was looking for. For the benefits of others...

        d = {'period': [14, 16, 20 , 22, 25, 30], 'period2': [33, 39]}
        cerebro.addstrategy(MyHolyGrailStrategy, *[], **d)
        

        cheers

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          Just to be clear, what have you exactly found?

          The signature for addstrategy and optstrategy is clear an documented. It taks *args and *kwargs and where possible some of the keyword arguments are incorporated to the declarative built-in params (see below). The rest is given verbatim to the strategy.

          • Docs - Cerebro - https://www.backtrader.com/docu/cerebro/

          Note: you probably didn't want to use addstrategy in your last post, given that you obviously pass a combination of parameters meant for optimization.

          @lampalork said in Dynamic parameters passed to cerebro.optstrategy():

          For addstrategy(), I'm using something like

          cerebro.addstrategy(MyHolyGrailStrategy, parameters)
          

          where parameters is a dictionary that is received by the __init__ method of my strategy

          def __init__(self, params):
          

          That pattern for example goes against the declarative params provided as a built-in by backtrader and brings absolutely no benefit.

          • Docs - Concepts - Parameters - https://www.backtrader.com/docu/concepts/#parameters
          1 Reply Last reply Reply Quote 1
          • L
            lampalork last edited by

            yes, my bad. I meant this:

            d = {'period': [14, 16, 20 , 22, 25, 30], 'period2': [33, 39]}
            cerebro.optstrategy(MyHolyGrailStrategy, *[], **d)
            

            @lampalork said in Dynamic parameters passed to cerebro.optstrategy():

            d = {'period': [14, 16, 20 , 22, 25, 30], 'period2': [33, 39]}
            cerebro.addstrategy(MyHolyGrailStrategy, *[], **d)

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