# KeyError: \['high'\] backtrader + alpaca\_trader\_api exception when running cerebro

**URL:** <https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016>\
**Category:** Getting Started with Alpaca\
**Created:** [June 22, 2021, 1:09am UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016 "2021-06-22T01:09:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuvzib](https://avatars.discourse-cdn.com/v4/letter/y/c57346/32.png) [@yuvzib](https://forum.alpaca.markets/u/yuvzib)\
**Post date:** [June 22, 2021, 1:09am UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/1 "2021-06-22T01:09:45Z")

</div>

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:

```auto
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()

```

```auto
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!

---

<div class="post-metadata">

**Author:** ![bo198214](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bo198214](https://forum.alpaca.markets/u/bo198214)\
**Post date:** [July 30, 2021, 7:36pm UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/2 "2021-07-30T19:36:06Z")

</div>

Same here with python 3.9

---

<div class="post-metadata">

**Author:** ![lovelmark](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@lovelmark](https://forum.alpaca.markets/u/lovelmark)\
**Post date:** [July 30, 2021, 7:36pm UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/3 "2021-07-30T19:36:18Z")

</div>

> [@yuvzib](#):
>
> `KeyError: ['high']`

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](http://net-informations.com/ds/pd/exists.htm) 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.

---

<div class="post-metadata">

**Author:** ![jrhone](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/jrhone/32/2779_2.png) [@jrhone](https://forum.alpaca.markets/u/jrhone)\
**Post date:** [August 2, 2021, 9:04pm UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/4 "2021-08-02T21:04:52Z")

</div>

The issue is with the alpaca-backtrader-api library

---

<div class="post-metadata">

**Author:** ![jrhone](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/jrhone/32/2779_2.png) [@jrhone](https://forum.alpaca.markets/u/jrhone)\
**Post date:** [August 6, 2021, 9:55am UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/5 "2021-08-06T09:55:11Z")

</div>

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

- [Multiple indicators example throws an error · Issue #173 · alpacahq/alpaca-backtrader-api · GitHub](https://github.com/alpacahq/alpaca-backtrader-api/issues/173)
- [the readme example throws an Exception · Issue #172 · alpacahq/alpaca-backtrader-api · GitHub](https://github.com/alpacahq/alpaca-backtrader-api/issues/172)

A new version of the [alpaca-backtrader-api](https://github.com/alpacahq/alpaca-backtrader-api) library was pushed recently without fixing the issues, only bumped the [alpaca-trade-api](https://github.com/alpacahq/alpaca-trade-api-python) library version, but didn’t fix the integration.

I believe there’s no version of [alpaca-backtrader-api](https://github.com/alpacahq/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.](https://github.com/kernc/backtesting.py) for my backtesting since it doesn’t require a data integration, which alpaca seems to not care about maintaining.

---

<div class="post-metadata">

**Author:** ![jdubs](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@jdubs](https://forum.alpaca.markets/u/jdubs)\
**Post date:** [August 13, 2021, 7:01pm UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/6 "2021-08-13T19:01:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![muhe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/muhe/32/1493_2.png) [@muhe](https://forum.alpaca.markets/u/muhe)\
**Post date:** [September 10, 2021, 2:12pm UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/7 "2021-09-10T14:12:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StevieV](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/steviev/32/1832_2.png) [@StevieV](https://forum.alpaca.markets/u/StevieV)\
**Post date:** [September 26, 2021, 8:07am UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/8 "2021-09-26T08:07:17Z")

</div>

Same issue here with the example code in the startpost.

When I try the example on [Alpaca & Backtrader: Tools of the Trade (Part 1)](https://alpaca.markets/learn/backtrader-01/) the data seems to load but I am running into errors while plotting ‘Plot\_OldSync’ object has no attribute ‘mpyplot’

---

<div class="post-metadata">

**Author:** ![haggai\_s](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/haggai_s/32/3725_2.png) [@haggai\_s](https://forum.alpaca.markets/u/haggai_s)\
**Post date:** [February 12, 2022, 10:17pm UTC](https://forum.alpaca.markets/t/keyerror-high-backtrader-alpaca-trader-api-exception-when-running-cerebro/6016/9 "2022-02-12T22:17:42Z")

</div>

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