KeyError: ['high'] backtrader + alpaca_trader_api exception when running cerebro

HI Folks,

I recently opened an account with Alpaca, so that I can live trade my backtrader strategies,
I am using Python 3.8 and both my backtrader as well as alpaca_backtrader_api packages are up to date
however , when I am Trying to run even the most basic ‘README’ example from the alpaca-backtrader-api githup page (also available PyPl) , I am getting this exception:

the script is as follows:

import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime
from local_settings import alpaca_paper

ALPACA_API_KEY = alpaca_paper['api_key']
ALPACA_SECRET_KEY = alpaca_paper['api_secret']
ALPACA_PAPER = True


class SmaCross(bt.SignalStrategy):
  def __init__(self):
    sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
    crossover = bt.ind.CrossOver(sma1, sma2)
    self.signal_add(bt.SIGNAL_LONG, crossover)

cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)

store = alpaca_backtrader_api.AlpacaStore(
    key_id=ALPACA_API_KEY,
    secret_key=ALPACA_SECRET_KEY,
    paper=ALPACA_PAPER
)

if not ALPACA_PAPER:
  broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
  cerebro.setbroker(broker)

DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(
    2015, 1, 1), timeframe=bt.TimeFrame.Days)
cerebro.adddata(data0)

print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/alpaca_backtrader_api/alpacastore.py", line 414, in _t_candles
    cdl = cdl.loc[
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 5162, in dropna
    raise KeyError(list(np.compress(check, subset)))
KeyError: ['high']

could it be that Alpaca is returning the wrong data format? could it be that perhaps there is missing date?

FYI I did not fund my account yet. if this is funding related ,what is the minimum amount of money that I have deposit to be able to have this capability?

Thanks!

1 Like

Same here with python 3.9

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

The issue is with the alpaca-backtrader-api library

It’s been broken for over a month and a half and no one is responding to the issues

A new version of the alpaca-backtrader-api library was pushed recently without fixing the issues, only bumped the alpaca-trade-api library version, but didn’t fix the integration.

I believe there’s no version of alpaca-backtrader-api that seems compatible with alpaca-trade-api >= 1.x.x

I’m going to switch to GitHub - kernc/backtesting.py: Backtest trading strategies in Python. for my backtesting since it doesn’t require a data integration, which alpaca seems to not care about maintaining.

I’m experiencing the same problem. How do we get an ETA on when this is going to be fixed?

any luck at all because I am having the same issue.

Same issue here with the example code in the startpost.

When I try the example on Alpaca & Backtrader: Tools of the Trade (Part 1) the data seems to load but I am running into errors while plotting ‘Plot_OldSync’ object has no attribute ‘mpyplot’

I found the problem and a solution.

In the demo, when fetching the data - it’s written:
data0 = DataFactory(dataname=‘AAPL’, historical=True, fromdate=datetime(
2015, 1, 1), timeframe=bt.TimeFrame.Days)

The API then returns:
{‘code’: 42210000, ‘message’: ‘your subscription does not permit querying data from the past 15 minutes’}

Apparently, paper accounts can’t fetch the most recent data.
To walk around it, simply add todate attribute:
data0 = DataFactory(dataname=‘AAPL’, historical=True, fromdate=datetime(2015, 1, 1), todate=datetime(2020, 1, 1), timeframe=bt.TimeFrame.Days)

The question remains - how can you live test a trading strategy if you can’t access the past 15 minutes of data ???

1 Like