For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

optstrategy skips values of parameters

  • 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?

  • 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?

  • 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)
        # Add a strategy
        x = [item for item in range(2, 91, 4)]
        strats = cerebro.optstrategy(_Strategy,
        spy_data ='.\\hourly\\',separator=',',nullvalue=float('NaN'),
                                        close=5,volume=6,openinterest=7,timeframe=bt.TimeFrame.Minutes,compression=1,adjclose=False, reverse=False, plot=False)
        # Set our desired cash start
    	# run backtest
    	thestrats =

  • @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 =

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

  • 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.

Log in to reply