Navigation

    Backtrader Community

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

    KeyError: '__restricted__'

    General Code/Help
    data feed pandasdata
    2
    2
    108
    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.
    • M
      MeMine last edited by

      I am trying to load data into cerebro, and the data is in the form of a pandas dataframe.

      import pandas as pd
      import backtrader as bt 
      import backtrader.feeds as btfeeds
      
      df = pd.read_csv('csvfile.csv', index_col=0)
      df['datetime'] = pd.to_datetime(df['datetime'])
      data = PandasData(dataname=df)
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy)
      cerebro.adddata(data)
      cerebro.run()
      
      class PandasData(btfeeds.PandasData):
      	params = (
      		('datetime','datetime'),
      		('open','open'),
      		('high','high'),
      		('low','low'),
      		('close','close'),
      		('volume','volume'),
      		('openinterest',None),
      	)
      	datafields = [
              'datetime', 'open', 'high', 'low', 'close', 'volume'
          ]
      
      class TestStrategy(bt.Strategy):
      	def log(self, txt, dt=None):
      		dt = dt or self.datas[0].datetime.date(0)
      		print('%s, %s' % (dt.isoformat(), txt))		
      
      	def __init__(self):
      		self.ma10 = bt.talib.SMA(self.data.close, 10)
      	
      	def next(self):
      		if self.data.close > self.ma10:
      			self.log('Close, %.2f' % self.dataclose[0])
      		elif self.data.close < self.ma10:
      			self.log('Close, %.2f' % self.dataclose[0])
      

      I have looked up the community for answers but I did not manage to find a solution to this error:

      File "/controllers/backtest.py", line 10, in index
          data = PandasData(dataname=df)
      File "/modules/backtrader/metabase.py", line 86, in __call__
          _obj, args, kwargs = cls.donew(*args, **kwargs)
      File "/modules/backtrader/lineseries.py", line 419, in donew
          _obj, args, kwargs = super(MetaLineSeries, cls).donew(*args, **kwargs)
      File "/modules/backtrader/lineroot.py", line 48, in donew
          _obj, args, kwargs = super(MetaLineRoot, cls).donew(*args, **kwargs)
      File "/modules/backtrader/metabase.py", line 244, in donew
          clsmod = sys.modules[cls.__module__]
      KeyError: '__restricted__'
      

      Kindly advise. Thank you!

      D 1 Reply Last reply Reply Quote 0
      • D
        doniervask @MeMine last edited by

        @memine said in KeyError: '__restricted__':

        KeyError: 'restricted'

        This annoying error means that Pandas can not find your column name in your dataframe. Before doing anything with the data frame, use print(df.columns) to see dataframe column exist or not.

        print(df.columns)
        

        I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.

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