alpacahq / Alpaca-API

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

Why is having open buy & sell orders on the same stock forbidden? #82

Closed biyimaks closed 4 years ago

biyimaks commented 4 years ago

I tried to place two simultaneous orders on the same stock, one to buy, and the other to sell. Both orders are limit orders, and have different prices. One of the orders was accepted, and the other order was rejected with the error below:

Forbidden ---> Alpaca.Markets.RestClientErrorException: cannot open a long buy while a short sell order is open

Why is this forbidden?

bdowling commented 4 years ago

US Equities do not allow you to be both long and short the same ticker in the same account simultaneously, so we block these as conflicting orders.

chirashidon commented 4 years ago

It seems like it should be permissible to have open buy and sell orders on the same stock as long as (a) both are limit orders and (b) the limit buy price is less than the limit sell price.

Especially for the case of time_in_force=opg, this makes perfect sense.

Example:

submit_order symbol=XYZ side=buy type=limit price=99.0 time_in_force=opg submit_order symbol=XYZ side=sell type=limit price=101.0 time_in_force=opg

Then the opening auction occurs, and: If XYZ stock opens <= $99, only the buy order gets executed. If XYZ stock opens between $99 and $101, no order gets executed. If XYZ stock opens >= $101, only the sell order gets executed.

This is a legitimate use case. Is there a reason not to support it? Thanks.

adrianbegi commented 4 years ago

US Equities do not allow you to be both long and short the same ticker in the same account simultaneously, so we block these as conflicting orders.

Correct, however a limit buy and sell order placed simultaneously with appropriate limit price would never encounter this issue as stated by @chirashidon. This is the first I am coming across a broker that does not allow for an Order in both directions. Can we reopen this issue @bdowling ?

astu9880 commented 2 years ago

Hi, can we reopen this? Seems like a legitimate and popular use case.

asxzy commented 2 years ago

can we reopen this?

biyimaks commented 2 years ago

https://alpaca.markets/blog/bracket-orders/ can probably help (depending on the trading scenario being targeted)

asxzy commented 2 years ago

https://alpaca.markets/blog/bracket-orders/ can probably help (depending on the trading scenario being targeted)

Unless I missed something. the document said it supports OCO on one side. The bracket order is an OTOCO order, which I tested and works well. Yet I haven't figured out how to build an OCO order for both sides -- one limit buy and one limit sell.

And honestly, the support for brackets orders is kinda weak in alpaca. One can not build a nested bracket order. e.g. TD Ameritrade https://developer.tdameritrade.com/content/place-order-samples

for example, if I want an OCO order, which long at 110, take profit at 115, stop loss at 100; OR short at 90, take profit at 85, stop loss at 100. a nested order would look like.

{
    "order_class": "oco",
    "legs": [
        {
            "order_class": "otoco",
            "triger": {
                "side": "buy",
                "qty": 1,
                "limit_price": 110,
                "type": "limit",
            },
            "legs": [
                {
                    "side": "sell",
                    "qty": 1,
                    "limit_price": 115,
                    "type": "limit",
                },
                {
                    "side": "sell",
                    "qty": 1,
                    "stop_price": 90,
                    "type": "stop",
                },
            ],
        },
        {
            "order_class": "otoco",
            "triger": {
                "side": "sell",
                "qty": 1,
                "limit_price": 90,
                "type": "limit",
            },
            "legs": [
                {
                    "side": "buy",
                    "qty": 1,
                    "limit_price": 85,
                    "type": "limit",
                },
                {
                    "side": "buy",
                    "qty": 1,
                    "stop_price": 100,
                    "type": "stop",
                },
            ],
        },
    ],
}