alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
505 stars 125 forks source link

[Bug]: Order stuck in pending_cancel #400

Closed dsun615 closed 3 weeks ago

dsun615 commented 6 months ago

Is there an existing issue for this?

Current Behavior

When I do a TradingClient.cancel_orders(), my order get stuck in pending_cancel status. It may take a few minutes for it to go away itself. Sometime this issue doesn't happen at all. I do notice sometime it can happen on the orders page https://app.alpaca.markets/orders where you check the box to select all and cancel. I have one now currently stuck in pending_status the whole day. Order id c6ba2784-f809-4946-a483-42def039d1e2 in my paper account. However, it may have successfully cancelled by the time you look at it.

Expected Behavior

I expect when you cancel, an order, or all orders that it will cancel and not get stuck.

SDK Version I encountered this issue in

alpaca-py: v0.13.6

Steps To Reproduce

In the paper account.
Make sure to have multiple open positions. 

Prior to having open positions. I enter bracket orders for multiple stocks. When the orders get filled, there are the two order legs lefts for each of the bracket orders -the target and the stop loss.

The automation tries to exist out the position within 15 to 30 minutes by cancelling all orders and creating a new order OCO orders against the open positions. 

However, before I can get to creating the OCO orders, the existing orders get stuck in the 'pending_cancel' status when trying to cancel all orders. I didn't note the time of how long it takes for it to actually complete cancelling. I just notice when I go back to look at my logs, that my program failed due to pending_cancel. When I rerun at that time - about 30 minutes later or so after the cancel, my oco orders go through because the pending_cancel orders finally completed. I normally run this logic between 9:45AM - 10:15 EST. Not sure if the issue happens because its during peak trading time and a lot of orders are taking some time to complete?

--------------------------------
Sample Bracket order prior to having an open position.

symbol = 'T'
share_size = 100
limit_price = 16.20
take_profit = 17
stop_loss = 15.50

ALPACA_API_KEY = ''
ALPACA_SECRET_KEY = ''

stock_trading_client = TradingClient(ALPACA_API_KEY, ALPACA_SECRET_KEY)

order_req = LimitOrderRequest(symbol=symbol
                               , qty =share_size
                               , side=OrderSide.SELL
                               , type=OrderType.LIMIT
                               , time_in_force=TimeInForce.GTC
                               , limit_price=limit_price
                               , client_order_id = f'{datetime.utcnow().timestamp()}'
                               )

order_req.order_class = OrderClass.BRACKET
order_req.take_profit = TakeProfitRequest(limit_price=take_profit)                    
order_req.stop_loss = StopLossRequest(stop_price=stop_loss)

order = stock_trading_client.submit_order(order_data=order_req)

----------------------------------
After 15 or 30 minutes. I cancel the order to create a new updated order.

stock_trading_client.cancel_orders()

Filled out the Steps to Reproduce section?

Anything else?

pending_cancel_issue

hiohiohio commented 3 weeks ago

Seems this is not a bug. There are some limitations of timing when we change status of orders. We need to communicate with external exchanges for status change of orders. Therefore, depending on time especially outside of market hours, this situation could happen.