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



  • 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)
        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()
    


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



  • 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
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.