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

multiprocessing.pool.MaybeEncodingError



  • In Linux, when I added optstrategy, there was a error:
    Traceback (most recent call last):
    File "ema_mean_reversion.py", line 355, in <module>
    results = cerebro.run()
    File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/backtrader/cerebro.py", line 1143, in run
    for r in pool.imap(self, iterstrats):
    File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/pool.py", line 748, in next
    raise value
    multiprocessing.pool.MaybeEncodingError: Error sending result: '[<main.EmaMeanReversionStrategy object at 0x7f4ab7de6b38>]'. Reason: 'PicklingError("Can't pickle <class'main.EmaMeanReversionStrategy'>: it's not the same object as__main__.EmaMeanReversionStrategy")'
    this is my codes:

    class EmaMeanReversionStrategy(bt.Strategy):
        params = (
            ('emaperiod', 750),
            ('printlog', False),
            ('upper', 3.0),
            ('lower', - 2.7)
        )
        def __init__(self):
            self.dataclose = self.datas[0].close
            self.ema = bt.indicators.ExponentialMovingAverage(
                    self.datas[0], period=self.params.emaperiod)
            self.dpce = (self.dataclose / self.ema - 1) * 100
            
            .....
    if __name__ == '__main__':
        cerebro = bt.Cerebro(optreturn=False)
        cerebro.optstrategy(EmaMeanReversionStrategy, upper = np.arange(2.0, 4.0, 0.2))
        cerebro.addobserver(bt.observers.DrawDown)
        cerebro.addsizer(bt.sizers.PercentSizer, percents = 95)
        dataframe = pd.read_csv('bitfinex_BTCUSD_min_20180601_20190531.csv', index_col=0, parse_dates=True)
        dataframe['openinterest'] = 0
        fromdate = datetime.datetime(2018, 12, 1, 0, 1)
        fromdate_str = fromdate.strftime('%Y/%m/%d')
        todate = datetime.datetime(2019, 5, 31, 23, 59)
        todate_str = todate.strftime('%Y/%m/%d')
        data = bt.feeds.PandasData(dataname=dataframe,
            fromdate = fromdate,
            todate = todate,
            timeframe = bt.TimeFrame.Minutes
            )
        # Add the Data Feed to Cerebro
        cerebro.adddata(data, name = 'BTCUSD')
        # Set our desired cash start
        cerebro.broker.setcash(100000.0)
        # Set the commission
        cerebro.broker.setcommission(commission=0.0035)
        results = cerebro.run()
    
    

  • administrators

    @franklili said in multiprocessing.pool.MaybeEncodingError:

    multiprocessing.pool.MaybeEncodingError: Error sending result: '[<main.EmaMeanReversionStrategy object at 0x7f4ab7de6b38>]'. Reason: 'PicklingError("Can't pickle <class'main.EmaMeanReversionStrategy'>: it's not the same object as__main__.EmaMeanReversionStrategy")'

    This multiprocessing error has to do with scoping in Python, hence the ("it's not the same ...") It is a well-known pickling problem.

    My guess is that you have to take the core out of if __name ... and put it into a function of its own.


Log in to reply
 

});