alpacahq / alpaca-backtrader-api

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

Support for Oauth #52

Open ronnyli opened 4 years ago

ronnyli commented 4 years ago

Hi, does alpaca-backtrader-api support authenticating with Oauth access tokens? A quick look through the source code seems to imply that we can only authenticate with API keys.

Would adding support for Oauth be as simple as adding the oauth param to AlpacaStore and passing that parameter to API(tradeapi.REST)? If so, I can make a PR to add this functionality

shlomiku commented 4 years ago

Hi, it should work. inside AlpacaStore (line 234) we do this

self.oapi = API(self.p.key_id,
                self.p.secret_key,
                self.p.base_url,
                self.p.api_version)

check if your Oauth works by adding that

ronnyli commented 4 years ago
Exception in thread Thread-7:
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\hocke\leverhead\alpaca_backtrader_api\alpacastore.py", line 336, in _t_streaming_events
    streamer = Streamer(q,
  File "C:\Users\hocke\leverhead\alpaca_backtrader_api\alpacastore.py", line 107, in __init__
    self.conn = tradeapi.StreamConn(api_key,
  File "c:\users\hocke\venv\leverheads\lib\site-packages\alpaca_trade_api\stream2.py", line 184, in __init__
    _key_id, _secret_key, _ = get_credentials(key_id, secret_key)
  File "c:\users\hocke\venv\leverheads\lib\site-packages\alpaca_trade_api\common.py", line 24, in get_credentials
    raise ValueError('Secret key must be given to access Alpaca trade API'
ValueError: Secret key must be given to access Alpaca trade API (env: APCA_API_SECRET_KEY

Looks like the issue is with Streamer but unfortunately the StreamConn in alpaca-trade-api doesn't accept oauth parameter

ronnyli commented 4 years ago

If I don't use Polygon or the Alpaca Data API do I need to connect to the Streamer?

shlomiku commented 4 years ago

not sure I understand. you want to use a different source but connect it to the alpaca broker? alpacabroker (the module) is dependent on the alpacastore module. so you cannot create one without the other about support in streamconn, you should open a feature request in the https://github.com/alpacahq/alpaca-trade-api-python repo

ronnyli commented 4 years ago

Sorry about not being clear. Basically, I'm not sure what the purpose of the Streamer is and I'm wondering if I can safely ignore it when authenticating with Oauth. I plan to use AlpacaBroker and AlpacaStore for executing trades but I don't need AlpacaData since I have my own data feed.

Given that I don't intend to use AlpacaData, can I add an if statement here to prevent instantiating Streamer if I don't provide an api_key and api_secret?

EDIT: Also, what happens if I provide my actual API key and secret key but use someone else's OAuth access token?

shlomiku commented 4 years ago

adding that if statement is not possible in the main repo. you could have a fork and do it there though the reason is that it is a patch for something it was not designed for, doing something like that could cause bugs in the future (just an example - someone forgets to add credentials and the streamer doesn't start and no exception happens.. he will not know why nothing works)

adding your credentials is never a good practice... so don't do that (you may get unexpected results.. if not now, maybe in a future commit) currently what happens is that the AlpacaBroker gets the REST interface from the AlpacaStore so if you initialize it there correctly it should probably work (but again, it's not a good practice so operate with caution)

now... we have 2 streamers. one for prices, one for events. prices stream is not relevant for you since you use a different feed. the events are broker events.. so I'm guessing you probably need that

in any case - do these actions only with paper trading and see if you get your desired results. also, please update it here so others may get that information