alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
537 stars 134 forks source link

[Bug]: trading_client.get_account only works on Paper=False, if using Paper=True -> unauthorized (even with Paper keys) #347

Closed phoenix-ds closed 10 months ago

phoenix-ds commented 11 months ago

Is there an existing issue for this?

Current Behavior

Initialize trading client:

broker_api_key = my live key broker_api_secret = my live secret

trading_client = TradingClient(broker_api_key, broker_api_secret, paper=False) account = trading_client.get_account()

This returns an object "account" as expected. Everything works fine

Now we simply switch the broker_api_key and broker_api_secret to the Paper ones provided by Alpaca in the portal.

And we repeat this but now:

trading_client = TradingClient(broker_api_key, broker_api_secret, paper=True) account = trading_client.get_account()

Result is that we get an error

[edited to simplify showing the error]

0app | File "/var/app/python-trader/backend/app.py", line 919, in fetch_quotes_and_calculate_ema 0|app | account = trading_client.get_account() alpaca.common.exceptions.APIError: {"message": "forbidden."}

If calling the endpoint https://paper-api.alpaca.markets/v2/account directly with those keys (curl or postman), the response is the account info as expected

Expected Behavior

The function

trading_client = TradingClient(broker_api_key, broker_api_secret, paper=True) account = trading_client.get_account()

Should return the paper account info (assuming key and secret are correctly set for the paper account)

SDK Version I encountered this issue in

alpaca-py 0.10.0

Steps To Reproduce

from alpaca.trading.client import TradingClient

trading_client = TradingClient('api-key', 'secret-key', Paper=True)

account = trading_client.get_account()

Filled out the Steps to Reproduce section?

Anything else?

I think that perhaps this function is not calling the correct API endpoint when Paper = True? So it's trying to call the normal alpaca-markets endpoint with a Paper Key/Secret pair which of course would result in an error

Please check whether the function is calling the correct API endpoint

hiohiohio commented 11 months ago

@phoenix-ds I could get account using paper. Can you please try again with api key and secret for paper?

The below is what I have checked in my env with alpaca-py v0.10.0 (and also v0.11.0)

from alpaca.trading.client import TradingClient

api_key=<PAPER API KEY>
secret_key=<PAPER API SECRET>
paper = True

trader = TradingClient(api_key, secret_key, paper=paper)
trader.get_account()
alessiocastrica commented 10 months ago

I wasn't able to replicate this issue as well, I tried with both paper and production keys. @phoenix-ds please check that you're using the right API keys.