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/

    optstrategy skips values of parameters

    General Code/Help
    2
    5
    984
    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.
    • T
      Trade Prophet last edited by

      Hello,
      I'm trying to use optstrategy with a range of values for one strategy parameter but when I check the value for the first time strategy init gets called, I see that the value I get is NOT the first value of the range

      cerebro.optstrategy(_Strategy, r_p=range(2, 91, 4))
      

      first legal range value should be 2 but the first time inside init shows that r_p value is 14
      Why did backtrader skip values?

      1 Reply Last reply Reply Quote 0
      • P
        Paska Houso last edited by

        May that be due to multiprocessing? Execution order is not guaranteed.

        But it's only an isolated line of code. How does the rest look like?

        P 1 Reply Last reply Reply Quote 0
        • T
          Trade Prophet last edited by

          Here is the rest of the code, this time with list comprehension which I thought might work but didn't.
          The strategy itself has nothing fancy, just one ATR (20) indicator.
          The data is an hourly bars .csv file (each bar is one hour)

              # Create a cerebro entity
              cerebro = bt.Cerebro(stdstats=False)
              cerebro.addobserver(bt.observers.Broker)
          
              # Add a strategy
              x = [item for item in range(2, 91, 4)]
              strats = cerebro.optstrategy(_Strategy,
          								 r_p=x,
          								 rebalance_frequency=10)
          
              spy_data = bt.feeds.bt.feeds.GenericCSVData(dataname='.\\hourly\\spy.us.txt',separator=',',nullvalue=float('NaN'),
                                              dtformat=('%Y-%m-%d'),tmformat=('%H:%M:%S'),datetime=0,time=1,open=2,high=3,low=4,
                                              close=5,volume=6,openinterest=7,timeframe=bt.TimeFrame.Minutes,compression=1,adjclose=False, reverse=False, plot=False)
              cerebro.adddata(data=spy_data,name='SPY')
          
              # Set our desired cash start
              cerebro.broker.setcash(100000.0)
              cerebro.broker.setcommission(commission=0.0015)
          	# run backtest
          	thestrats = cerebro.run()
          
          1 Reply Last reply Reply Quote 0
          • P
            Paska Houso @Paska Houso last edited by

            @paska-houso said in optstrategy skips values of parameters:

            May that be due to multiprocessing? Execution order is not guaranteed.

            @trade-prophet said in optstrategy skips values of parameters:

            thestrats = cerebro.run()
            

            You are running with multiprocessing enabled (using all cores of the machine). Let me recommend that you execute the sample quickstart11.py from the backtrader sources to see how things are executed out of order and yet no value is skipped.

            1 Reply Last reply Reply Quote 0
            • T
              Trade Prophet last edited by

              Thank you for your reply,
              If this is the case then all is OK, as long as I get full value coverage, I don't mind the out-of-order issue.

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