Shorting and Stop-Loss

Hello, first time poster, long time lurker,

Currently, I am in the process of integrating shorting into my trading algorithm and I can successfully enter and exit a short position at specified times (market sell plus a delayed buy-in-force), but I have not been able to automate a stop-loss for shorting, such that if the price of a security rises above a certain point then I buy the quantity that I am in the short position.

Any guidance on how to automate this process with alpaca’s python API would be appreciated!

@KelE to enter a stop-loss order which will trigger if the price exceeds a certain value, simply submit a buy stop-loss order (ie not a sell) with a StopOrderRequest. That will trigger a buy (to cover) order when the stop prices is exceeded (the opposite of a sell order). Something like this

#Single stop order to ‘buy to cover’ when stop_price is exceeded

buy_stop_order = client.submit_order( StopOrderRequest(
symbol=‘SPY’,
qty=1,
side=OrderSide.BUY,
time_in_force=TimeInForce.DAY,
stop_price=300.00),
))

However, a bit better way to enter this type of order would be to pen the initial short position with a bracket order and include the stop order and also a take-;rofit order at the same time.

#Bracket order with market order as initial ‘parent’ order

market_bracket_order = client.submit_order(MarketOrderRequest(
symbol=‘SPY’,
qty=1,
side=OrderSide.SELL,
time_in_force=TimeInForce.DAY,
order_class = OrderClass.BRACKET,
stop_loss=StopLossRequest(stop_price=300.00),
take_profit=TakeProfitRequest(limit_price=250.00)
))

#Bracket order with limit order as initial ‘parent’ order

market_bracket_order = client.submit_order(LimitOrderRequest(
symbol=‘SPY’,
qty=1,
side=OrderSide.SELL,
limit_price=275.00,
time_in_force=TimeInForce.DAY,
order_class = OrderClass.BRACKET,
stop_loss=StopLossRequest(stop_price=300.00),
take_profit=TakeProfitRequest(limit_price=250.00)
))