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/

    Can't pickle: it's not the same object as

    General Code/Help
    2
    3
    3252
    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.
    • André
      André last edited by

      Hi,

      I can't use optstrategy due to the following error:

      _pickle.PicklingError: Can't pickle <class 'backtrader.lineseries.Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_AbstractDataBase_DataBase_PandasData'>: it's not the same object as backtrader.lineseries.Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_AbstractDataBase_DataBase_PandasData
      
      class PandasData(bt.feed.DataBase):
          params = (
              # Possible values for datetime (must always be present)
              #  None : datetime is the "index" in the Pandas Dataframe
              #  -1 : autodetect position or case-wise equal name
              #  >= 0 : numeric index to the colum in the pandas dataframe
              #  string : column name (as index) in the pandas dataframe
              ('datetime', None),
      
              # Possible values below:
              #  None : column not present
              #  -1 : autodetect position or case-wise equal name
              #  >= 0 : numeric index to the colum in the pandas dataframe
              #  string : column name (as index) in the pandas dataframe
              ('open', -1),
              ('high', -1),
              ('low', -1),
              ('close', -1),
              ('volume', -1),
              ('openinterest', None),
          )
      
      def runstrat(data=None):
      
          cerebro = bt.Cerebro()
          cerebro.broker.set_cash(1000)
          cerebro.optstrategy(Strategy_MA, period=range(0, 5))
          cerebro.adddata(data)
      
          results = cerebro.run()
      
      
      if __name__ == '__main__':
          dataframe = pd.read_json('data.json')
          data = bt.feeds.PandasData(dataname=dataframe)
          runstrat(data)
      

      Can anyone tell me what's wrong with this code? If I replace optstrategy with addstrategy it works.

      1 Reply Last reply Reply Quote 1
      • André
        André last edited by

        I found the problem.

        1. Instead of bt.feeds.PandasData, I had to use my PandasClass class
        2. The name PandasData had to be renamed to PandasClass2. There was a conflict with the internal one.
        T 1 Reply Last reply Reply Quote 0
        • T
          Takashioffer @André last edited by

          @André Hi, could you clarify on how to rename the PandasClass?

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