In this backtest, where I submit a market order at 1am on a trading day (12 Dec 2022), the order isn't executed that trading day, but is instead executed at the open of the next trading day (13 Dec 2022).
Is this expected behavior? If so, then how should I be placing an order to buy as soon as possible?
Mike Lin
On Day 1, the order ticket is returned with status Invalid (7), possibly because we haven't gotten the first data bar yet. This post suggests that we need the first data bar before we can place a trade.
It appears that universe selection occurs around 4am-6am when live trading. What if a new security was added to our universe and we want to buy it on market open or as close to that as possible? Would we have to subscribe to tick data and then place the order after the first tick?
Louis Szeto
Hi Mike
You may place a Market On Open order within the OnSecuritiesChanged handler. Refer to the attached backtest for reference.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Mike Lin
Thanks for looking at this. If you change the resolution to daily and rerun the backtest, then SPY is purchased two days in a row instead of just once, which doesn't seem right.
The first purchase shows as filled at 2022-10-05 00:00:00, which means it must have filled on 2022-10-04 during the trading day. Universe selection, when you check whether we are invested in SPY, is also done at 2022-10-05 00:00:00, but immediately before the order, which is probably why it looks like we aren't yet invested, even though we are. I think the problem is that universe selection is occurring before we get the daily bar in the backtest, although the sequence would be reversed during live trading.
Is this a problem or working as expected? My preference is for the sequence in backtesting to  match that of live trading.
Louis Szeto
Hi Mike
We have no choice but to use a denser resolution (e.g. hour) since the algorithm had not filled the previous order yet:
This of course only happened in backtest. In live trading, your MOO order (1) will be already active at Day 1 00:00, and shall be filled by your broker at Day 1 09:30.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Mike Lin
Thanks for explaining the difference between backtest and live trading results. I've since run a test on a live trading node that verifies that it behaves correctly as you described. Still would be nice if the results were consistent between live trading and backtesting.
Mike Lin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!