Trailing Stop Loss - Canceling Orders

What is the most effective way to get an open order ID for a trailing stop loss order?

For example, I open a long position based on an indicator event and simultaneously open a trailing stop loss sell order. One of two things can happen; The trailing stop loss is triggered and I exit the position or the indicators trigger another event that requires you to cancel the open trailing stop loss order and close the open long position.

I can run the list orders API call and query through the list of open orders to find it by symbol then match it to order ID but wasn’t sure if there is an easier method out there. It would be nice to have an API call that cancels open orders by symbol

1 Like

For anyone curious, This was my solution:

##Get Open Orders List
orders = api.list_orders()

##Loop Through Orders List to find the symbol to be canceled (Ticker_Sym is my ticker variable)
for order in orders:
if order.symbol == Ticker_Sym:
print(‘Order Found…Canceling Open Order’)
api.cancel_order(order.id)

1 Like

Good solution. You pretty much need to cancel each order separately. One can also get the orders for just a subset of symbols. So something similar could also be done:

# Get list of open orders for the stock or stocks you want
my_orders = api.list_orders(params={"symbols":"SPY"})

# Loop through the orders list and cancel the orders
for order in my_orders:
  api.cancel_order(order.id)

In future versions of the python SDK the symbols parameter will be built in. However, for now it can be added using the params keyword.

1 Like