Position not closing after market sell in paper account

Hello all,

I have pretty strange issue I’m facing with my paper account. I’m using my paper account to test out my algorithm. Part of my code monitors the time of day so that it can liquidate all my positions before the market closes. What happens is, when it’s time to liquidate, it goes through my positions (using the .list_positions method of the Alpaca api, and sells them one by one (market order). After each sell, the code will get the new position list using the list_positions method again. Simple enough right?

This is the issue, for some reason, very randomly, after the order is marked as “filled” (see attachment), it stays in my Alpaca paper account position! So my code keeps trying to sell it (market). This goes on and on forever until the market closes. Each time my code sells, the position on alpaca goes more negative. At the time of righting this post, this stock still hasnt cleared from my account.

Is anyone else facing a similar issue??

There seems to be two issues. First, the list_positions method may have a small delay in reporting current positions after a trade. Second, your logic seems to be flawed and not doing exactly what you intended.

Let’s look at the first issue and see if we can discern when the list_positions method shows updated values. It appears you went long CNHI at 15:51:09 UTC when an order for 102 shares was filed. At that point you had 102 shares in your account. Then, at 20:51:10 UTC, these shares were sold. You then had 0 shares in your account. All good so far.

Then, at 20:51:18 UTC the algo placed another order to sell 102 shares. Presumably it checked list_positions and still found 102 shares in your account (ie the positions hadn’t been updated). Not sure when it checked for updated positions but it was between 0-8 seconds after the first trade was placed. Then at 20:51:29 UTC you again placed an order to sell 102 shares. Again, presumably because list_positions thought there were 102 shares there (however it may have thought there were -102 shares, more on that below). That was 19 seconds after the first ‘correct’ order. Now it gets interesting. At 20:51:34 an order was placed to sell 204 shares. This implies the list_positions method showed an updated quantity. This was within 24 seconds of the first ‘correct’ order. So, it seems reasonable there may be some delay in updating the portfolio stats after a fill, and from the above time, but that delay in this case is less than 24 seconds.

Moving to the second issue. It appears your logic isn’t looking at the ‘sign’ or the ‘side’ (ie long or short) when trying to close the position. In looking at the 20:51:34 order to sell 204 shares, that probably should have been an order to ‘buy’ 204 shares to close a short position. I seems at that point you were short that many shares in your account.

Maybe put in a 1 minute or so delay before re-checking your list_position logic, and also verify you are handling the ‘side’ properly when closing positions.

1 Like

Thanks so much for your response. you’re absolutely right. My code is checking my positions too fast after executing an order. list_positions is not updating in time. But additionally, I my logic is not accounting for the possibility of shorts. To be honest, I didnt even know that was possible (that one could sell securities he didnt own). I have revised my code to check the sign of “qty” of an owned stock. I have also disabled the shorting option from my account settings. This lack of understanding was leading me to believe that there was something wrong with the paper account platform. I’m happy I didnt move forward with that assumption and implement in my real account. Again, thanks so much for you help.