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



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



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


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


Log in to reply
 

});