In the attached code, I execute a buy order and immediately set a stop-limit sell order for the same security (AMZN). However, that order doesn't even get placed until the next day, and even when placed, it doesn't trigger when the limit price is far surpassed.
Day 1: 2014-4-15
We can see from the log output that the buy order was filled within a minute, yet the sell order wasn't even placed (otherwise we would have seen the Bum sell order error in the log. Is this because one can't set a sell order unless those securities are in his portfolio?
Day 2: 2014-4-16
We bought more AMZN stock, giving us 321 shares in our portfolio. This time we see that the sell order is in place with limit of $312. The previous day's stocks were bough at ~$311/share, so i'd expect this limit to have been reached (limit_reached == True), yet the order shows the limit has not reached, and thus the order did not execute. Is this because Day 2's purchase was for ~$317, which is already above the limit price (not sure why this would matter, but if sell-order wasn't placed until after this 2nd buy-order, then maybe the limit-logic is affected by the most recent buy-price?). Note that the portfolio shows the average price paid per share, and that is still below our $312 limit. I'd expect the current price of $317, again, to trigger the sale.
Day 3: 2014-4-17
We can't buy any more shares (broke), but we see that there are now 2 open sale orders to sell all AMZN. Note that even though our code says to sell "all" stock (not a particular qty), both sale orders have quantities (amount=320 and 321). Are sales getting thwarted because of having 2 open sale orders which sum to more shares than are in our portfolio?
The behind-the scenes logic of how limit orders and orders in general are set is a mystery. Can anyone explain why i'm seeing the described/attached behavior?