Market With Stop Order or Bracket Orders V2

Hi, I’m hoping someone can help me; I need help understanding how to submit ONE order that’s a Market Order WITH Stop Loss, or a Bracket Order in the updated alpaca-py library. I.e., can I specify a stop in MarketOrderRequest, and if so, how? I can only find documentation on the ‘old’ API for this.

I can create and submit market orders without a stop just fine using the MarketOrderRequest Class.

Stage a BUY market order

market_order_data = market_order("AAPL", 1, OrderSide.BUY, TimeInForce.GTC)

Market order function

def market_order(ticker, size, order_side, tif):
    market_order_data = MarketOrderRequest(
        symbol=ticker, qty=size, side=order_side, time_in_force=tif
    )
    return market_order_data

I can also use the StopOrderRequest Class to create and submit a stop order if I don’t have an open market order.

Stage a SELL stop order

stop_order_data = stop_order("AAPL", 1, OrderSide.SELL, TimeInForce.GTC, 100)

Stop order function

def stop_order(ticker, size, order_side, tif, stop):
    stop_order_data = StopOrderRequest(
        symbol=ticker, qty=size, side=order_side, time_in_force=tif, stop_price=stop
    )
    return stop_order_data

Either order will be accepted after hours, but only one or the other; I get why the system doesn’t like having long and short orders open simultaneously, and this is where I’m struggling. How can I submit ONE Buy Market Order WITH a Sell Stop Loss? I receive this error when submitting both orders: alpaca.common.exceptions.APIError: {“code”:40310000,“message”:“cannot open a short sell while a long buy order is open”}

Submitting either of the order works fine, as mentioned, but not both separately.

Submit market order

`submit_order(market_order_data)`

Submit an order and print the returned object function

def submit_order(order_data):
    order = trading_client.submit_order(order_data)
    for property_name, value in order:
        print(f'"{property_name}": {value}')

Thank you,
Patrik

I think you just want to send an OTO order. I’m not sure how to do it in Python SDK but I think it should be possible.