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__'
-
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!
-
@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.