alpacahq / Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
https://alpaca.markets/
143 stars 13 forks source link

Cannot change position side by placing orders #250

Closed Chacoon3 closed 1 week ago

Chacoon3 commented 5 months ago

Describe the bug Assuming that I have a 10-share long position for stock X, I want to close the existing long position first, and then open a 10-share short position for the same stock. I have tried the following two approaches using the paper trading API and they have both failed:

To Reproduce

Approach 1:

Approach 2:

Expected behavior

In either approach, I think the second market sell order should be accepted rather then be rejected. Basically the client program is just trying to close the existing position and build a new position of the opposite side.

I guess in either case the error was thrown because the first order has not been filled yet, but even so the second order has no reason to be rejected.

Client programs can avoid such errors by postponing the submission of the second order after the first order was filled, but this can lead to quite complex client-side logic. So I think the server side should allow orders to be placed in this manner.

Desktop (please complete the following information):