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/

    cerebro.optstrategy() failing with maxcpus > 1 (pickle error)

    General Code/Help
    3
    3
    1237
    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.
    • tomasrollo
      tomasrollo last edited by

      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.

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

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

        1 Reply Last reply Reply Quote 0
        • C
          crunchypickle last edited by

          ah! reminds me jupyter notebook has problems with multiprocessing too!

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