For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Importing data from pandas_datareader
-
Hi all, I am trying to test importing data from pandas_datareader (a spin out of pandas.io) and it throws an error. Anyway I could import those macro economic data into backtrader data stream?
Here's the code where it throws error:
import pandas_datareader.data as web def runstrat(): cerebro = bt.Cerebro() start = datetime.datetime(1949,12,31) end = datetime.datetime(2017,1,3) spx = web.DataReader("^GSPC", 'yahoo', start, end)
And here's the error output:
TypeError Traceback (most recent call last) <ipython-input-55-af84f08c4efa> in <module>() 58 59 if __name__ == '__main__': ---> 60 runstrat() <ipython-input-55-af84f08c4efa> in runstrat() 49 50 ---> 51 data = bt.feeds.PandasData(spx) 52 cerebro.adddata(data) 53 cerebro.broker.setcash(10000.0) C:\Anaconda3\envs\backtrader\lib\site-packages\backtrader\metabase.py in __call__(cls, *args, **kwargs) 85 _obj, args, kwargs = cls.donew(*args, **kwargs) 86 _obj, args, kwargs = cls.dopreinit(_obj, *args, **kwargs) ---> 87 _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs) 88 _obj, args, kwargs = cls.dopostinit(_obj, *args, **kwargs) 89 return _obj C:\Anaconda3\envs\backtrader\lib\site-packages\backtrader\metabase.py in doinit(cls, _obj, *args, **kwargs) 75 76 def doinit(cls, _obj, *args, **kwargs): ---> 77 _obj.__init__(*args, **kwargs) 78 return _obj, args, kwargs 79
pandas-datareader parses the data like this (I've downsample to monthly):
Open High Low Close Volume Adj Close Date 1950-01-31 17.049999 17.049999 17.049999 17.049999 1950-02-28 17.219999 17.219999 17.219999 17.219999 1950-03-31 17.290001 17.290001 17.290001 17.290001 1950-04-30 17.959999 17.959999 17.959999 17.959999 1950-05-31 18.780001 18.780001 18.780001 18.780001
-
Nevermind, fixed the error by adding 'dataname=' in front of the name of the pandas df.
data = bt.feeds.PandasData(dataname=spx)