Search code examples
pythonpython-3.xtypeerrorbt

How to fix TypeError in bt backtesting framework for Python


I am trying to load data from the bt backtesting framework for Python.

  1. I was trying to load the data for the ticker "tlt" using the bt backtesting framework.
  2. I am getting a TypeError: while retrieve the data
  3. I did tried fixing even by ensuring I have installed all the dependencies
  4. I also ensured the ticker and dates are correct
  5. The data download works if I use yfinance
  6. Not sure what is missing and it is not working in the bt backtesting framework

Getting an Error below error when trying to load data.

TypeError: string indices must be integers

How do I solve it?

Python Query:

%matplotlib inline
import bt
import matplotlib.pyplot as plt

data1 = bt.get("tlt", start="2010-01-01", end="2022-06-30")

Already tried the below ensured that I have

  1. pip install bt
  2. pip install matplotlib
  3. Tried loading data from yfinance and it works
  4. Does not works with bt backtesting framework

Entire Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[44], line 1
----> 1 data1 = bt.get("tlt", start="2010-01-01", end="2022-06-30")

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/decorator.py:232, in decorate.<locals>.fun(*args, **kw)
    230 if not kwsyntax:
    231     args, kw = fix(args, kw, sig)
--> 232 return caller(func, *(extras + args), **kw)

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/ffn/utils.py:34, in _memoize(func, *args, **kw)
     32     return cache[key]
     33 else:
---> 34     cache[key] = result = func(*args, **kw)
     35     return result

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/ffn/data.py:76, in get(tickers, provider, common_dates, forward_fill, clean_tickers, column_names, ticker_field_sep, mrefresh, existing, **kwargs)
     74 # call provider - check if supports memoization
     75 if hasattr(provider, "mcache"):
---> 76     data[ticker] = provider(ticker=t, field=f, mrefresh=mrefresh, **kwargs)
     77 else:
     78     data[ticker] = provider(ticker=t, field=f, **kwargs)

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/decorator.py:232, in decorate.<locals>.fun(*args, **kw)
    230 if not kwsyntax:
    231     args, kw = fix(args, kw, sig)
--> 232 return caller(func, *(extras + args), **kw)

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/ffn/utils.py:34, in _memoize(func, *args, **kw)
     32     return cache[key]
     33 else:
---> 34     cache[key] = result = func(*args, **kw)
     35     return result

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/ffn/data.py:140, in yf(ticker, field, start, end, mrefresh)
    137 if field is None:
    138     field = "Adj Close"
--> 140 tmp = pdata.get_data_yahoo(ticker, start=start, end=end)
    142 if tmp is None:
    143     raise ValueError("failed to retrieve data for %s:%s" % (ticker, field))

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/pandas_datareader/data.py:80, in get_data_yahoo(*args, **kwargs)
     79 def get_data_yahoo(*args, **kwargs):
---> 80     return YahooDailyReader(*args, **kwargs).read()

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/pandas_datareader/base.py:253, in _DailyBaseReader.read(self)
    251 # If a single symbol, (e.g., 'GOOG')
    252 if isinstance(self.symbols, (string_types, int)):
--> 253     df = self._read_one_data(self.url, params=self._get_params(self.symbols))
    254 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
    255 elif isinstance(self.symbols, DataFrame):

File /opt/anaconda3/envs/My_Python_Lab_One/lib/python3.9/site-packages/pandas_datareader/yahoo/daily.py:153, in YahooDailyReader._read_one_data(self, url, params)
    151 try:
    152     j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))
--> 153     data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154 except KeyError:
    155     msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers

Solution

  • This is the issue with pandas-datareader. How to use data is here pdr. Datareader - TypeError: string indices must be integers

    I would assume bt [bt] project is dead as there are no responses to issues in GitHub since 2021