Petersoj / alpaca-java

A Java API for Alpaca, the commission free, algo friendly, stock trading broker. https://alpaca.markets
https://petersoj.github.io/alpaca-java/
MIT License
197 stars 82 forks source link

Getting Status Code: 422, Status Message: "Unprocessable Entity error message today. #145

Closed Andys52621 closed 6 months ago

Andys52621 commented 6 months ago

Hi Jacob

I am getting this error today, but same line of code was working fine yesterday and I did not change anything.. Do you know what could be the problem ? Its a simple sell short order like this..

alpacaAPI.orders().requestMarketOrder("RIVN", 100, OrderSide.SELL, OrderTimeInForce.GOOD_UNTIL_CANCELLED);

net.jacobpeterson.alpaca.rest.AlpacaClientException: Status Code: 422, Status Message: "Unprocessable Entity", API Response Code: 42210000, API Response Message: "potential wash trade detected. use complex orders" at net.jacobpeterson.alpaca.rest.AlpacaClient.request(AlpacaClient.java:207) at net.jacobpeterson.alpaca.rest.AlpacaClient.requestJSON(AlpacaClient.java:162) at net.jacobpeterson.alpaca.rest.AlpacaClient.requestObject(AlpacaClient.java:140) at net.jacobpeterson.alpaca.rest.AlpacaClient.requestObject(AlpacaClient.java:122)

Thank you, Andy

Petersoj commented 6 months ago

The API response message in your exception message states: potential wash trade detected. use complex orders. Here is more info on wash trading. I would review your algo's logic and confirm that multiple market orders with opposing sides aren't submitted close in together in time. My guess is that the suggestion to "use complex orders" might mean that you can submit multiple orders with opposing sides close together in time as long as, for example, one order is a market buy order and the other order is something like a bracket or OTO order, not a market sell order.

Andys52621 commented 6 months ago

alpaca error trade wash sales

Hi Jacob, Thanks for the reply. So I think Alpaca did change some rules between yesterday and today. I am using Alpaca paper account to test a logic. Right now the way its working, buy 10 rivn market price (assume mkt price is 20), then after order filled , place a limit sell at 20.10 ) , so far it works ... then if price dropped to 19.50 then by mkt 10 share, buy itself at this time generating this error "potential wash sales trade detected". But yesterday I ran same logic, it was working and bought and put sell limit orders below below the price as share was going down many many times, i had more than 200 transactions yesterday. Today same logic is generating this wash sales error. Just to check at alpaca site, Then I logged to Alpaca paper account using UI, and replicated the same scenario and got same error message on their UI also. Seems like alpaca has enforced some rule which doesnt make any sense cuz people should be allowed to buy at falling mkt rate despite they have a higher limit sell order existing.

Do you think it makes sense ? I attached a screenshot also, Its just a PAPER account, nothing to worry about.

Andy

Petersoj commented 6 months ago

Thanks for the detailed info. I'm not sure what changes Alpaca made on their end that would flag your trading activity as potential wash trading. I would reach out to Alpaca support (Slack or email) to get more clarity on this. Closing since your issue isn't strictly related to this library. Let me know if you have any other questions.