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 and PivotPoint indicator pickle error



  • Adding the pivot point indicator to a strategy results in a pickle error:

    File "/opt/emeraldmine/backtest.py", line 61, in run
        runs = self.cerebro.run(stdstats=stdstats)
      File "/usr/local/lib/python3.7/dist-packages/backtrader/cerebro.py", line 1143, in run
        for r in pool.imap(self, iterstrats):
      File "/usr/lib/python3.7/multiprocessing/pool.py", line 748, in next
        raise value
    multiprocessing.pool.MaybeEncodingError: Error sending result: '[<emeraldmine.strategies.pivot.PivotBase object at 0x7f9e3bea7710>]'. Reason: 'PicklingError("Can't pickle <class 'backtrader.metabase.LinesCoupler_0'>: attribute lookup LinesCoupler_0 on backtrader.metabase failed")'
    

    The strategy contains nothing but the initialising of the pivot point indicator with in init():

    class PivotBase(BaseStrategy):
        """Base strategy using built in PivotPoint indicator that uses 2 timeframes."""
        params = dict(
            open=False,
            close=False
        )
    
        def __init__(self):
            self.pivot = btind.PivotPoint(self.data1)
            # # CrossOvers
    
        def next(self):
            pass
    
    if __name__ == "__main__":
        pass
    
    

    This only happens when running optstrategy, obviously because of the pickling, any directions on a fix?



  • Please take a look at the following post: https://community.backtrader.com/topic/1977/multiprocessing-pool-maybeencodingerror

    Could this be relevant?


  • administrators

    The sample code is incomplete but the presence of

    @Benjamin-Kesby said in Optstrategy and PivotPoint indicator pickle error:

    if __name__ == "__main__":
        pass
    

    does clearly point in the direction indicated by @vladisld and the scoping problems that pickle has. The solution would be the same as the one outlined in the referenced post.



  • OK thank you, does this mean I cant keep strategies in separate files and import them? I have also wrapped a backtrader script in a class to allow for some easier configuration/ automation but i guess thats not possible with optstrategy also? Finally why would this error only occur with pivot points. All other indicators i've used so far do not create such an error.


Log in to reply
 

});