Ok I get it. The short answer is that since first order is not filled until the end of the current candlestick, the second order is not placed since there is no need for action to get to 0 shares.
I understand that limitation (could be better documented though), but the real problem is that Quantopian does not provide profit targets (or at least I have been unable to find them at the documentation), and provided stop losses are not reliable in my recent experience. Therefore, I had to make up my own solution, which is more error-prone and much less elegant than providing internal stops and targets.
In the attached code, I was trying to backtest a simple strategy:
1. detect open gaps
2. classify them in 3 groups depending on the openning price and the pivot points in order to trade/not to trade them
3. trade them with risk/reward 1:1 or 1.5:1 depending on the type of gap. This means using a profit target for yesterday's close price (to close the gap) and a stop loss
4. 5 minutes after market closes, close open positions to market (if profit target nor stop loss have been reached)
Nevertheless, when backtest was completed, I saw that many days the step 4 was triggered to close positions which had passes the StopLoss, so they are not reliable. This is a big issue!
So I coded both profit target and stop loss like the code attacked.
Other problem I detected is the scheduled function has limitations, so I can go for the minute 1 as soonest, and to minute 5 before closure at most. The problem with minute 1 as soonest, is that the trade actually takes place at minute 2, which can be too late for my system. I don't unserstand this limitation either.
So, the important question is: isn't there an easy way of using simple, elegant stop losses and profit targets?