alpacahq / alpaca-backtrader-api

Alpaca Trading API integrated with backtrader
https://pypi.org/project/alpaca-backtrader-api/
Apache License 2.0
599 stars 143 forks source link

Multiple indicators example throws an error #173

Open jrhone opened 3 years ago

jrhone commented 3 years ago

The multiple indicators example throws an error with the following config and real API keys

IS_BACKTEST = False
IS_LIVE = True
symbol = "AAPL"
$ python live_v3.py 
2021-06-28 03:04:49,918 started trading stream
Traceback (most recent call last):
  File "/Users/jonathanrhone1/Src/quant/live_v3.py", line 127, in <module>
    cerebro.run()
  File "/Users/jonathanrhone1/miniconda3/envs/quant39/lib/python3.9/site-packages/backtrader/cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "/Users/jonathanrhone1/miniconda3/envs/quant39/lib/python3.9/site-packages/backtrader/cerebro.py", line 1187, in runstrategies
    self._broker.start()
  File "/Users/jonathanrhone1/miniconda3/envs/quant39/lib/python3.9/site-packages/alpaca_backtrader_api/alpacabroker.py", line 103, in start
    self.positions = self.update_positions()
  File "/Users/jonathanrhone1/miniconda3/envs/quant39/lib/python3.9/site-packages/alpaca_backtrader_api/alpacabroker.py", line 83, in update_positions
    broker_positions_symbols = [p.symbol for p in broker_positions]
  File "/Users/jonathanrhone1/miniconda3/envs/quant39/lib/python3.9/site-packages/alpaca_backtrader_api/alpacabroker.py", line 83, in <listcomp>
    broker_positions_symbols = [p.symbol for p in broker_positions]
  File "/Users/jonathanrhone1/miniconda3/envs/quant39/lib/python3.9/site-packages/alpaca_trade_api/entity.py", line 29, in __getattr__
    return super().__getattribute__(key)
AttributeError: 'Position' object has no attribute 'symbol'
$ pip freeze 
alpaca-backtrader-api==0.14.0
alpaca-trade-api==1.2.2
backtrader==1.9.76.123
pandas==1.1.5

I also had to comment the Starting Portfolio Value line.

brianhumphreys commented 2 years ago

Any workaround on this?

brianhumphreys commented 2 years ago

Make sure you are properly authenticated @jrhone. After some investigation, it seems that I was getting a 404 and the backtrader was trying to wrap the 404 response in a Position object. Obviously, this was user error but perhaps there could be better error reporting around this @camelpac.

dependencies:

alpaca-backtrader-api==0.14.1
alpaca-trade-api==1.2.3