Going from long to short with OPG market orders

I have two algorithms, one that trades long and another that trades short. On some occasions, my long position closes weak and my long algorithm elects to close the trade with a market → OPG order. This is never an issue; my trading program has successfully done this hundreds of times over the last ~3 months of live trading.

But, I just ran into a situation where the short algorithm wanted to short the same stock that same morning with a market → OPG order.

The first sell order (for the short trade) went in without an issue. But the second sell order (to exit the long trade) blew up with there not being enough shares available for the sell order. Looks like the short trade, instead of opening a trade to short the stock, sold the shares for the long trade. So the long exit trade had nothing left to sell. But why didn’t it just create a short position? This is the net result of what my algorithms were trying to achieve…going from being long to being short that particular stock.

Looking through things, it appears that it really wouldn’t matter which order executed first; the second would barf because there weren’t enough long shares to match the sell order.

This seems like an issue with the API. Seems like it has some kind of placeholder to mark whether the account is long or short a stock at that time. With having a long position, that flag is set to long. And when the first, short order comes in, it puts a hold on those long shares to use with the short order. So when the next order comes in, it sees that the account is long that stock so tries to create it as a sell (not a short) but there aren’t enough long shares remaining (the previous order is selling them all) to fill the sell requirements for the second order.

But it should be able to sell the second order’s shares short rather than selling long shares that aren’t available.

Thanks!
Dave

1 Like