Replace order creates a new order successfully but the old order is filled later thus creating duplicate order

Environment

Language
Python 3.6

Alpaca SDK Version
latest

Other Environment Details
This is on paper environment

Problem

Summary
Describe the problem you’re running into here.
While I am trying to exit a stock(i.e. sell an already bought stock)
I had placed a replace order (as initial order did not fill and price had changed to my favor), to which alpaca returns a success and provides new order ID, However the previous order then fills due to which there exist duplicate order. As I am unaware of the previous order being filled, I still try to sell the stock(which is already sold) and thus creating duplicate sell order and thus ended up unintentionally shorting the stock.

Paper or Live Trading?
Paper trading simulator

Example Code
It cannot be reproduced generally, as it has not happened before, but such single instance issues in handling financial orders can cause great losses.
Example from logs:
Order({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘3b86c9e5-335d-450d-bb2b-ee4a1a658aa3’,
‘canceled_at’: None,
‘client_order_id’: ‘45cf316e-46c1-47c6-a7d3-4e83ac49c1b3’,
‘created_at’: ‘2021-05-24T13:32:01.234595Z’,
‘expired_at’: None,
‘extended_hours’: False,
‘failed_at’: None,
‘filled_at’: None,
‘filled_avg_price’: None,
‘filled_qty’: ‘0’,
‘hwm’: None,
‘id’: ‘38c616d4-99a3-4e9a-9804-ae6dcb5e7655’,
‘legs’: None,
‘limit_price’: ‘37.64’,
‘notional’: None,
‘order_class’: ‘’,
‘order_type’: ‘limit’,
‘qty’: ‘312’,
‘replaced_at’: None,
‘replaced_by’: None,
‘replaces’: ‘44388bdf-450c-4835-9223-4724b4ebfb5c’,
‘side’: ‘sell’,
‘status’: ‘new’,
‘stop_price’: None,
‘submitted_at’: ‘2021-05-24T13:34:25.558401Z’,
‘symbol’: ‘XLF’,
‘time_in_force’: ‘day’,
‘trail_percent’: None,
‘trail_price’: None,
‘type’: ‘limit’,
‘updated_at’: ‘2021-05-24T13:34:25.558449Z’})

previous orderID:38c616d4-99a3-4e9a-9804-ae6dcb5e7655 and now new orderid 28bad7c4-ac5c-4cce-b2bf-6901a921ad96

Order({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘3b86c9e5-335d-450d-bb2b-ee4a1a658aa3’,
‘canceled_at’: None,
‘client_order_id’: ‘af0662f7-fc6b-4ef7-8f58-59ae750fd20e’,
‘created_at’: ‘2021-05-24T13:32:01.234595Z’,
‘expired_at’: None,
‘extended_hours’: False,
‘failed_at’: None,
‘filled_at’: None,
‘filled_avg_price’: None,
‘filled_qty’: ‘0’,
‘hwm’: None,
‘id’: ‘28bad7c4-ac5c-4cce-b2bf-6901a921ad96’,
‘legs’: None,
‘limit_price’: ‘37.7’,
‘notional’: None,
‘order_class’: ‘’,
‘order_type’: ‘limit’,
‘qty’: ‘312’,
‘replaced_at’: None,
‘replaced_by’: None,
‘replaces’: ‘38c616d4-99a3-4e9a-9804-ae6dcb5e7655’,
‘side’: ‘sell’,
‘status’: ‘new’,
‘stop_price’: None,
‘submitted_at’: ‘2021-05-24T13:35:35.066217Z’,
‘symbol’: ‘XLF’,
‘time_in_force’: ‘day’,
‘trail_percent’: None,
‘trail_price’: None,
‘type’: ‘limit’,
‘updated_at’: ‘2021-05-24T13:35:35.066282Z’})

Later on checking checking both orders:
initial order 38c616d4-99a3-4e9a-9804-ae6dcb5e7655
Order({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘3b86c9e5-335d-450d-bb2b-ee4a1a658aa3’,
‘canceled_at’: None,
‘client_order_id’: ‘45cf316e-46c1-47c6-a7d3-4e83ac49c1b3’,
‘created_at’: ‘2021-05-24T13:32:01.234595Z’,
‘expired_at’: None,
‘extended_hours’: False,
‘failed_at’: None,
‘filled_at’: ‘2021-05-24T13:34:50.730449Z’,
‘filled_avg_price’: ‘37.68’,
‘filled_qty’: ‘312’,
‘hwm’: None,
‘id’: ‘38c616d4-99a3-4e9a-9804-ae6dcb5e7655’,
‘legs’: None,
‘limit_price’: ‘37.64’,
‘notional’: None,
‘order_class’: ‘’,
‘order_type’: ‘limit’,
‘qty’: ‘312’,
‘replaced_at’: None,
‘replaced_by’: None,
‘replaces’: ‘44388bdf-450c-4835-9223-4724b4ebfb5c’,
‘side’: ‘sell’,
‘status’: ‘filled’,
‘stop_price’: None,
‘submitted_at’: ‘2021-05-24T13:34:25.558401Z’,
‘symbol’: ‘XLF’,
‘time_in_force’: ‘day’,
‘trail_percent’: None,
‘trail_price’: None,
‘type’: ‘limit’,
‘updated_at’: ‘2021-05-24T13:36:58.941928Z’})

wile the other order 28bad7c4-ac5c-4cce-b2bf-6901a921ad96 was replaced again for a min till the order was fulfilled:
Order({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘3b86c9e5-335d-450d-bb2b-ee4a1a658aa3’,
‘canceled_at’: None,
‘client_order_id’: ‘af0662f7-fc6b-4ef7-8f58-59ae750fd20e’,
‘created_at’: ‘2021-05-24T13:32:01.234595Z’,
‘expired_at’: None,
‘extended_hours’: False,
‘failed_at’: ‘2021-05-24T13:35:40.723278Z’,
‘filled_at’: None,
‘filled_avg_price’: None,
‘filled_qty’: ‘0’,
‘hwm’: None,
‘id’: ‘28bad7c4-ac5c-4cce-b2bf-6901a921ad96’,
‘legs’: None,
‘limit_price’: ‘37.7’,
‘notional’: None,
‘order_class’: ‘’,
‘order_type’: ‘limit’,
‘qty’: ‘312’,
‘replaced_at’: None,
‘replaced_by’: None,
‘replaces’: ‘38c616d4-99a3-4e9a-9804-ae6dcb5e7655’,
‘side’: ‘sell’,
‘status’: ‘rejected’,
‘stop_price’: None,
‘submitted_at’: ‘2021-05-24T13:35:35.066217Z’,
‘symbol’: ‘XLF’,
‘time_in_force’: ‘day’,
‘trail_percent’: None,
‘trail_price’: None,
‘type’: ‘limit’,
‘updated_at’: ‘2021-05-24T13:35:59.550658Z’})

This appears to be related to a current issue with slow updates on fills in the paper trading environment. It’s being addressed and should be fixed soon.