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.optstrategy() failing with maxcpus > 1 (pickle error)



  • Hi all,

    I'm getting a pickle error when trying to use cerebro.optstrategy() with maxcpus > 1 (or entirely without the parameter):

    Traceback (most recent call last):
      File "backtrader/mybacktrader.py", line 239, in <module>
        results = cerebro.run(stdstats=False)
      File "/home/rollo/anaconda2/envs/crypto_27/lib/python2.7/site-packages/backtrader/cerebro.py", line 1143, in run
        for r in pool.imap(self, iterstrats):
      File "/home/rollo/anaconda2/envs/crypto_27/lib/python2.7/multiprocessing/pool.py", line 673, in next
        raise value
    cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
    

    Here's how I instantiate cerebro and add my strategy to optimize:

    cerebro = bt.Cerebro(tradehistory=True, maxcpus=1, optreturn=True, optdatas=True, preload=True, runonce=True)
    cerebro.optstrategy(strategy_class, **strategy_parameters)
    

    It only works with maxcpus=1

    Using the latest version of backtrader (1.9.64.122).

    Has anybody met with such error? I've searched the forums but haven't found anything similar.

    Thanks
    T.


  • administrators

    @tomasrollo said in cerebro.optstrategy() failing with maxcpus > 1 (pickle error):

      File "/home/rollo/anaconda2/envs/crypto_27/lib/python2.7/multiprocessing/pool.py", line 673, in next
        raise value
    cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
    

    This part of the trace seems to indicate it is related to specific code present in one of your next methods (probably in the strategy, although the trace isn't detailed enough to be able to assert that)

    The raise value would seem to be in your code, but the guess is: it is actually inside the function which cannot be pickled.