Inaccurate Filled_qty returned on Limit Sell - using REST call

All, I’m loving the trading bot using python / rest services. I’ve recently come across an issue where it seems like the web service is responding with the wrong filled quantity.

Here is the example:
I created a limit purchase for 137 shares. That purchase was filled.
I created a limit sell for 137 shares. That purchase was filled at a reported back 136 shares.
I tried to create a limit sell for the remaining 1 share but the service responded I had insufficient quantity.

When I went to the Alpaca trading history on alpaca, it showed that the 137 limit sell had gone through for 137 shares.

Has anybody seen this before? If I get a response that not all requested shares were filled, is there a way to do an inventory or portfolio call to see what I have on hand for a symbol? So, if it reports back 136 were filled out of 137, should I do another call to see if I really have the 1 on hand?

This is a relatively new problem and it seems to be sporadic. I wonder if it’s because I’m checking the status of a sell by order_id as the sell is in progress. I made a change so that if the filled_qty is greater than zero but less than the requested sell qty, it will sleep for 2 seconds and check again. I’ll let you know if this fixes it.

@tbarnette Glad you are loving Alpaca! I have a hunch or two what may be happening. Could you provide the order number where you saw only 136 shares being filled? I can check the logs and see what’s up.

Hi Dan, I think I have these correct… On Order ID: 93f41876-2478-4825-92d9-76e76f781b2a (I think)… I placed a limit sell order for 137 shares.

I kept checking it status and when the order went through, I received the response: “qty”:“137”,“filled_qty”:“136”,“filled_avg_price”:“74.24022”.

So, then I took the qty, less the filled qty (1) and tried to place a sell order for it. That was when I got a:
available":“0”,“code”:40310000,“existing_qty”:“1”,“held_for_orders”:“1”,“message”:"insufficient qty available for order (requested: 1, available: 0)

The weird thing is, when I checked the Alpaca dashboard, all 137 seemed to have been sold with the initial request. Looking forward to hearing about this! Thanks in advance.

@tbarnette It appears the order was filled by a succession of partial fills. Here’s the order history.

It may have just been the timing of when you checked order status. What order attribute and value(s) do you look at to determine status?

That is interesting. I thought it was one and done. Whatever it filled, it filled. So, I could have got a filled_qty of 0, 111, 126, 133, etc… all the way up to the full fill. So, if it ever comes back with a quantity filled of less than what I was looking for, it may still be looking.

After I place a sell order, I keep checking the filled_qty using the response from a get query against the open order - order id. As soon as it comes back > 0 I assume it’s done. So if it’s > 0 but < than requested amount, I create a new sell order for the difference. In the mean time, it seems to complete.

Is there a way to know it’s done trying? Should I be checking something else? This is super helpful, thank you!

@tbarnette The order.status attribute will tell you if an order is filled or not. A status of ‘filled’ is a terminal state and means it is complete. A status of ‘partially_filled’ or ‘new’ indicates it is 1) an active order and 2) it is in the process of being filled. There are a few rare status states such as ‘suspended’ and ‘calculated’ that are unfortunately sort of indeterminate. You can check out the other status states here. Probably check the state maybe in conjunction with the qty.

Awesome! I will tuck this in my code and let you know how it goes! Thank you!

1 Like

Hi @Dan_Whitnable_Alpaca I just wanted to get back to you and let you know. My code has run through two day long cycles without issue, this fixed it - THANK YOU!!! Now on to fix some of my other bugs… lol…

@tbarnette Always glad that I can help. Start another post if you have other issues. Good luck!