alpaca-backtrader-api icon indicating copy to clipboard operation
alpaca-backtrader-api copied to clipboard

Alpaca-trade-api 1.x

Open jrhone opened this issue 3 years ago • 14 comments

Is upgrading to alpaca-trade-api >= 1.0 planned in the near future?

Lots of recent updates there around websockets and V2 data that would be great to use.

jrhone avatar Jun 21 '21 22:06 jrhone

Hi, an updated version was released last week: 0.14.0

camelpac avatar Jun 22 '21 04:06 camelpac

@camelpac unfortunately the update doesn't replace v1.0, as it doesn't permit subscription to multiple symbols. is there a workaround for that?

ohheytherewow avatar Jun 22 '21 14:06 ohheytherewow

not yet. follow the alpaca-proxy-agent project for updates.

camelpac avatar Jun 23 '21 18:06 camelpac

not yet. follow the alpaca-proxy-agent project for updates.

@camelpac great, thanks for the reply!

ohheytherewow avatar Jun 23 '21 18:06 ohheytherewow

@camelpac any chance this will be updated soon? I'd happily subscribe to the data API if this were supported, but it doesn't make sense if i can only subscribe to one ticker symbol.

ohheytherewow avatar Jul 28 '21 15:07 ohheytherewow

not yet. it's in the backlog

camelpac avatar Jul 29 '21 05:07 camelpac

This update was not done properly by the way, alpaca-trade-api 1.x is not integrated well and even the basic examples provided in the repo are not working.

Given all these issues it seems you decided to make another release with only the alpaca-trade-api version bumped but none of the issues fixed!

Can we get an ETA on when this will be resolved? Shouldn't it be high priority when your examples are broken for almost two months?

jrhone avatar Aug 06 '21 10:08 jrhone

Hi @jrhone The alpaca-proxy-agent was just updated to the data v2 stream. You can now use it to subscribe to multiple symbols.

What other issue to you face? please point me to specific examples.

camelpac avatar Aug 10 '21 04:08 camelpac

I am having issues testing minute granularity on >= 0.14.0. For example, when testing the README.md example modified for minute granularity:

data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime( 2021, 7, 21, 13, 30), todate=datetime(2021, 7, 21, 20, 0), timeframe=bt.TimeFrame.Minutes)

I'm only getting results between UTC 13:30 and 16:00. This occurs when testing one or multiple sessions. I've also tried substituting NYC and my local timezones without success. There's clearly something amiss with timezones in >=0.14.0. The above works perfectly on <=0.13.1.

NOTE: Regardless of version, I've had to apply the trading_calendars fix mentioned in issue #176. Modifying calendar_helpers in trading-calendars works but throws a FutureWarning. Substituting exchange_calendars works with no warning.

mkaicher avatar Aug 11 '21 06:08 mkaicher

Hi @camelpac , there are errors when trying to run the examples in the sample folder and the README.

Please see the two issues below which specify the sample code, error stacktraces and library versions used.

https://github.com/alpacahq/alpaca-backtrader-api/issues/172 https://github.com/alpacahq/alpaca-backtrader-api/issues/173

I think it would make sense to go through all the examples in the samples folder and verify they work. They should be tested with all the variations of the config args because some combos work while others do not.

You have 3 options:
 - backtest (IS_BACKTEST=True, IS_LIVE=False)
 - paper trade (IS_BACKTEST=False, IS_LIVE=False)
 - live trade (IS_BACKTEST=False, IS_LIVE=True)

jrhone avatar Aug 12 '21 10:08 jrhone

Hi @camelpac,

i am still unable to run my backtrader code with the updated proxy / backtrader-api code using the latest trading api. i see log of attempted connections in the proxy, but all data subscriptions fail.

these are strategies that worked fine using the earlier backtrader-api and v1 data. any hope this code might be updated sometime soon?

ohheytherewow avatar Aug 30 '21 14:08 ohheytherewow

@camelpac thanks for this work you've done thus far. I am excited to see where it will go in the future.

To add to the other commenters' requests, I am having issues with the sample code when running strategy_sma_crossover.py. I get the following error:

Traceback (most recent call last):
  File "strategy_sma_crossover.py", line 118, in <module>
    cerebro.run()
  File "/Users/brianhumphreys/.pyenv/versions/3.7.3/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "/Users/brianhumphreys/.pyenv/versions/3.7.3/lib/python3.7/site-packages/backtrader/cerebro.py", line 1187, in runstrategies
    self._broker.start()
  File "/Users/brianhumphreys/.pyenv/versions/3.7.3/lib/python3.7/site-packages/alpaca_backtrader_api/alpacabroker.py", line 103, in start
    self.positions = self.update_positions()
  File "/Users/brianhumphreys/.pyenv/versions/3.7.3/lib/python3.7/site-packages/alpaca_backtrader_api/alpacabroker.py", line 83, in update_positions
    broker_positions_symbols = [p.symbol for p in broker_positions]
  File "/Users/brianhumphreys/.pyenv/versions/3.7.3/lib/python3.7/site-packages/alpaca_backtrader_api/alpacabroker.py", line 83, in <listcomp>
    broker_positions_symbols = [p.symbol for p in broker_positions]
  File "/Users/brianhumphreys/.pyenv/versions/3.7.3/lib/python3.7/site-packages/alpaca_trade_api/entity.py", line 29, in __getattr__
    return super().__getattribute__(key)
AttributeError: 'Position' object has no attribute 'symbol'

With the following dependencies:

alpaca-backtrader-api==0.14.1
alpaca-trade-api==1.2.3
backtrader==1.9.76.123
trading-calendars==2.1.1

I'll see if I can do some investigative work on this to help out

brianhumphreys avatar Nov 07 '21 00:11 brianhumphreys

can confirm the above is still an issue

mitchweaver avatar Dec 22 '21 18:12 mitchweaver

Quick update: I solved this issue by realizing that my key and secret set in my env file were not registering because the sample code was explicitly passing in the key and secret into the AlpacaStore constructor. The issue was that I had not set the variables with my key and secret.

# Your credentials here
ALPACA_API_KEY = "<key_id>"
ALPACA_SECRET_KEY = "<secret_key>"

Once setting these it worked. I believe this was happening because the endpoint was sending back a 401 status and the backtrader was not catching properly and trying to cast the 40 response into a Position object.... hints AttributeError: 'Position' object has no attribute 'symbol'

I think there might have been another issue that I worked through but it's been too long.

A potential improvement here would be handling 401s from the authentication endpoint better. The error message is not clear.

brianhumphreys avatar Jan 14 '22 22:01 brianhumphreys