alpaca-java icon indicating copy to clipboard operation
alpaca-java copied to clipboard

Getting 401 trying to place paper trade buy order

Open s1rk1t opened this issue 1 year ago • 1 comments

Hi,

So I am able to query the market data API without issue using the example code you provided, but when I try to place a paper trade buy order I get the following error:

net.jacobpeterson.alpaca.openapi.trader.ApiException: Message: Unauthorized HTTP response code: 401 HTTP response body: {"code":40110000,"message":"request is not authorized"} HTTP response headers: {access-control-allow-credentials=[true], access-control-allow-headers=[Apca-Api-Key-Id, Apca-Api-Secret-Key, Authorization, Content-Type], access-control-allow-methods=[GET, OPTIONS, PUT, DELETE, POST, PATCH], access-control-allow-origin=[*], access-control-max-age=[1728000], connection=[keep-alive], content-length=[55], content-type=[application/json; charset=UTF-8], date=[Thu, 01 Aug 2024 00:11:49 GMT], strict-transport-security=[max-age=31536000; includeSubDomains], vary=[Origin], x-request-id=[8fe0e1ccf75ec4f0c42a613868e1ab5a]}

at net.jacobpeterson.alpaca.openapi.trader.ApiClient.handleResponse(ApiClient.java:1117)
at net.jacobpeterson.alpaca.openapi.trader.ApiClient.execute(ApiClient.java:1030)
at net.jacobpeterson.alpaca.openapi.trader.api.OrdersApi.postOrderWithHttpInfo(OrdersApi.java:842)
at net.jacobpeterson.alpaca.openapi.trader.api.OrdersApi.postOrder(OrdersApi.java:821)
at tests.Tests.testAlpacaBuyOrder(Tests.java:82)

I have tried it in different classes, but nothing seems to work. I'm using the format you gave in the README as follows:

final Order openingOrder = a.getAlpacaAPI().trader().orders() .postOrder(new PostOrderRequest() .symbol(symbol) .qty("1") .side(OrderSide.BUY) .type(OrderType.MARKET) .timeInForce(TimeInForce.GTC))

And since I'm able to query the market data API successfully, I don't believe there is any auth issue on my end since the AlpacaAPI object uses the same API key and secret for both.

Any idea what might be the issue?

s1rk1t avatar Aug 01 '24 00:08 s1rk1t

A few ideas:

  • Make sure you're using TraderAPIEndpointType.PAPER in your AlpacaAPI constructor.
  • Make sure your API keys are correct and correspond to your paper account in Alpaca.
  • Make sure your account configuration has trading enabled (not disabled/suspended).
  • Reach out to Dan on the Alpaca Slack if this is some sort of issue with your account.

Petersoj avatar Aug 03 '24 17:08 Petersoj