Correction: I've updated the post with an edit to clarify 'end of day' behavior does differ between live trading and backtest. The post is accurate now.
Hi Eric,
The way minute-level simulations (and live trading) works is based on aggregated minute bars of trade prices sourced from Nanex (so in the OHLCV bar, O = open trade for that minute, H = highest traded price for that minute, and so on). Each minute we get a new bar of trade data and the handle_data() method is called.
The other key piece of information is that orders placed in one call to handle_data, say on pricing bar 9:31am, are 'filled' on the next bar, 9.32am.
In simulation this means that we use the price and volume in the 9:32 am bar to simulate the order fill, depending on the slippage model selected the algo is 'at best' filled at the close price for the 9.32am bar. [N.B. this s a design decision in how we have built our backtester, it is a conservative choice with the intent of erring on the side of caution w.r.t. assumed execution speed.)
In live trading of course our simulator is not setting the fill price, we are submitting your order to the broker when it is placed and it is filled (via IB paper trading or in 'real life') as determined by the broker, typically within 1 second, and the broker transmits that information along with the fill price back to us. The number of shares filled and the fill price are available to the algorithm along with the next 1 minute bar of pricing data. And the beat goes on.
So to answer your questions above in order of market open, then market close:
trading at the open: if you are most concerned with the pricing you are getting in simulation then no, there is no way to force your algo get the simulated 9:31am 'first traded' price, you will at earliest get the 9:32am last traded price. If you are most concerned with the actual price you'll get from IB, I can tell you (and you can verify with an IB demo account) that if your algo places a trade in the first minute of trading you'll end up getting a broker fill at something close to the 9:31 am price.
trading at the close: In practice, to trade as close to the close as possible you should place orders at 3:59pm (or 1 minute prior to close). In live trading we cancel any open orders at 4pm/market close each day. N.B. In the backtester, orders placed at 4pm exactly will actually be executed on the next bar, at 9:31am the next morning. This is likely not the intended outcome in some cases and we're looking at the best way to resolve this difference between the backtester and live trading.
One additional note, we have heard loud and clear that for lower frequency strategies it would be really nice to let the algorithm writer more seamlessly execute daily order types (VWAP over the day, MOC, etc) and save the user the code writing (or cutting/pasting) associated with minute granularity when it is not really needed. I think that makes a lot of sense and that its something we'd support eventually, but no ETA on that at this point.
Best regards,
Jess
p.s. you can also @ me via: [email protected], [email protected] or @jstauth on twitter