Hi Peter et al,
I'd be happy to further elaborate on my response.
Can you elaborate on the mechanics of a stop order as an example, please? Assume I go long with a market order that gets filled. I then place a sell stop at a price 5% below the purchase price. The stop order show as a pending order and is cancelled overnight. Is that correct? Although in paper trading with Quantopian (not IB) or in the backtester the order is not cancelled.
When the market price hits your 5% loss threshold, this will convert the stop-order into a market-order. The order will then be executed as a regular order and attempt to sell your designated shares. You mention "stop order.. cancelled overnight"... that's almost correct. Let's imagine this edge-case scenario:
4:00PM - stop-order price is hit and converts order into a market-order. However the market is closed and no more trades can be placed. All open orders are expired at the end of the day, as explained in this thread.
9:30AM: The algo is running the next day and looks at the market price. We don't know what happened overnight. The price could have wildly risen, in this case, the stop-order is no longer valid and it will not attempt to exit the position. Alternatively the price stayed below the threshold, in which case we execute the stop-order and convert it to a market order. We place the order to sell, which can be filled within a few seconds depending on the liquidity.
Either way, your algo is checking your stop-order price against the market price. When the threshold is met, it will place an order to exit the position.
I see two possible scenarios:
(i) the stop order resides on the Quantopian server. When the stop is reached a market order is sent to IB.
or
ii) the stop order is sent to IB but is still recorded on Quantopian as pending. If the stop is reached a market order is executed. Some communication would be required from IB to alert Quantopian.
The first scenario is accurate. Our market feed comes from Nanex, which provides the pricing for all positions. The stop order is triggered by the Nanex price, alerts us, and sends the market order to IB for execution.
@Grant:
In my example to Peter I was using a stop-order, not a stop-limit order and I think Anony gave a great description.
if both the market and limit orders are submitted simultaneously, then one could end up with a limit order covering a yet-to-be filled market order.
The onus is on the code writer to prevent that scenario from happening - and this would likely get caught during backtesting before deploying to paper or live trading. Your example shows a great way of checking if we've filled our position and then setting the stop-order.
I too am a fan of "rejiggered", I'll see if I can throw it in sometime :)