alpacahq / alpaca-py

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

[Bug]: Account still using v1 endpoint #239

Closed srepollock closed 1 year ago

srepollock commented 1 year ago

Is there an existing issue for this?

Current Behavior

Calling TradingClient(api_key, api_secret, paper=True).get_account() (where the client is set up with paper-trading) gives a 403 error:

Traceback (most recent call last):
  File ".venv/lib/python3.10/site-packages/alpaca/common/rest.py", line 187, in _one_request
    response.raise_for_status()
  File ".venv/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://paper-api.alpaca.markets/v2/account

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".py", line 321, in core
    account = self._broker_apis[broker].account()
  File "alpaca.py", line 237, in account
    return self._trading_client.get_account()
  File ".venv/lib/python3.10/site-packages/alpaca/trading/client.py", line 417, in get_account
    response = self.get("/account")
  File ".venv/lib/python3.10/site-packages/alpaca/common/rest.py", line 212, in get
    return self._request("GET", path, data, **kwargs)
  File ".venv/lib/python3.10/site-packages/alpaca/common/rest.py", line 126, in _request
    return self._one_request(method, url, opts, retry)
  File ".venv/lib/python3.10/site-packages/alpaca/common/rest.py", line 196, in _one_request
    raise APIError(error, http_error)
alpaca.common.exceptions.APIError: {"message": "forbidden."}

Expected Behavior

It should return the paper account information:

{
  "account_blocked": false,
  "account_number": "010203ABCD",
  "crypto_status": "PAPER_ONLY",
  "buying_power": "262113.632",
  "cash": "-23140.2",
  "created_at": "2019-06-12T22:47:07.99658Z",
  "currency": "USD",
  "crypto_status": "ACTIVE",
  "non_marginable_buying_power": "7386.56",
  "accrued_fees": "0",
  "pending_transfer_in": "0",
  "pending_transfer_out": "0",
  "daytrade_count": "0",
  "daytrading_buying_power": "262113.632",
  "equity": "103820.56",
  "id": "e6fe16f3-64a4-4921-8928-cadf02f92f98",
  "initial_margin": "63480.38",
  "last_equity": "103529.24",
  "last_maintenance_margin": "38000.832",
  "long_market_value": "126960.76",
  "maintenance_margin": "38088.228",
  "multiplier": "4",
  "pattern_day_trader": false,
  "portfolio_value": "103820.56",
  "regt_buying_power": "80680.36",
  "short_market_value": "0",
  "shorting_enabled": true,
  "sma": "0",
  "status": "ACTIVE",
  "trade_suspended_by_user": false,
  "trading_blocked": false,
  "transfers_blocked": false
}

SDK Version I encountered this issue in

alpaca-py version: v0.7.0 (latest)

Steps To Reproduce

TradingClient(api_key, api_secret, paper=True).get_account()

Filled out the Steps to Reproduce section?

Anything else?

No response

haxdds commented 1 year ago

The authentication issue is not related to the version of the API. The SDK is using the v2 endpoint for get_account. Instead make sure you are using the correct keys combo for the API you are submitting the request to.