Cancel an Open Order

I have. code that will cancel an open limit sell at a certain time and replace it with a market sell to liquidate if it hits conditions. This works normally day in and day out but, today the market sell failed because it didn’t have the inventory to sell. It didn’t have the inventory to sell because it was still reserved for the limit sell.

This normally works but today it failed. I’m wondering if they triggered so fast that the limit sell didn’t cancel before it tried to do a market sell.

I’m using the web service and to cancel the open limit sell, it just does a DELETE:

r=requests.delete(ORDERS_URL + “/” + order_id, headers=HEADERS)
This is immediately followed by a market sell order for the same symbol and quantity.

The response from a DELETE is empty so you cant tell if there is any issue with it. Think it’s a timing issue and I should put a slight pause in?

Should I put a check after the DELETE call to see if the ORDER_ID still exists after I delete it and delete it again?