alpacahq / alpaca-backtrader-api

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

Alpaca-trade-api 1.x #171

Open jrhone opened 3 years ago

jrhone commented 3 years ago

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.

camelpac commented 3 years ago

Hi, an updated version was released last week: 0.14.0

ohheytherewow commented 3 years ago

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

camelpac commented 3 years ago

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

ohheytherewow commented 3 years ago

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

@camelpac great, thanks for the reply!

ohheytherewow commented 3 years ago

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

camelpac commented 3 years ago

not yet. it's in the backlog

jrhone commented 3 years ago

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?

camelpac commented 3 years ago

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.

mkaicher commented 3 years ago

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.

jrhone commented 3 years ago

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)
ohheytherewow commented 3 years ago

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?

brianhumphreys commented 2 years ago

@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

mitchweaver commented 2 years ago

can confirm the above is still an issue

brianhumphreys commented 2 years ago

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.