Backtrader Community

    • 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/

    multiprocessing.pool.MaybeEncodingError

    Indicators/Strategies/Analyzers
    2
    2
    1601
    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.
    • F
      franklili last edited by

      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()
      
      
      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @franklili last edited by backtrader

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

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