Hello Alpaca Community,
I am currently developing an app and need some guidance on integrating my order workflows, specifically regarding protective orders. I have two scenarios where I’m a bit stuck:
Scenario 1: I’m looking to create a buy order followed by protective orders which include:
- A Stop order (sell) to set a fixed stop-loss price.
- A Limit order (sell) to set a take-profit price and secure profits.
Scenario 2: Similarly, I want to create a buy order followed by protective orders that include:
- A Trailing Stop order (sell) to set a trailing percentage value.
- A Limit order (sell) to set a take-profit price and secure profits.
After reviewing the documentation, it appears that bracket orders won’t suffice for scenario 2 due to the absence of a trailing stop feature. My current understanding is that I might have to place each order separately through the API, starting with the buy order and then the protective orders.
However, I want to ensure that this approach won’t trigger the ‘wish trade’ validation logic on Alpaca’s platform.
Could you confirm whether this is a valid integration pattern for Alpaca? If not, could you provide some insights or resources on how to chain these orders correctly for both scenarios?
Draft code for ref:
async def place_order(self, client_order_id, symbol, qty, side, order_type, limit_price=None, stop_price=None, trail_percent=None, trail_price=None):
if order_type == 'market':
order_data = MarketOrderRequest(
client_order_id=client_order_id,
symbol=symbol,
qty=qty,
side=OrderSide.BUY if side.lower() == 'buy' else OrderSide.SELL,
time_in_force=TimeInForce.DAY
)
elif order_type == 'limit':
order_data = LimitOrderRequest(
client_order_id=client_order_id,
symbol=symbol,
qty=qty,
side=OrderSide.BUY if side.lower() == 'buy' else OrderSide.SELL,
limit_price=limit_price,
time_in_force=TimeInForce.GTC
)
elif order_type == 'stop':
order_data = StopOrderRequest(
client_order_id=client_order_id,
symbol=symbol,
qty=qty,
side=OrderSide.BUY if side.lower() == 'buy' else OrderSide.SELL,
stop_price=stop_price,
time_in_force=TimeInForce.GTC
)
elif order_type == 'trailing_stop':
order_data = TrailingStopOrderRequest(
client_order_id=client_order_id,
symbol=symbol,
qty=qty,
side=OrderSide.BUY if side.lower() == 'buy' else OrderSide.SELL,
trail_price=trail_price if trail_price else None,
trail_percent=trail_percent if trail_percent else None,
time_in_force=TimeInForce.GTC
)
else:
raise ValueError(f"Unsupported order type: {order_type}")
try:
return self.trading_client.submit_order(order_data=order_data)
except Exception as e:
logging.error(f"Error placing {order_type} order for {symbol}: {e}")
return None
Thank you in advance for your assistance!