I ran into a problem that sounds very simple to begin with, but still I could not find a solution to it. What I want to do is buy my fine universe selection on the open of the same day. Attached is a very simple back-test to demonstrate the problem. On 11-7-2023 my fine universe selection returns a list containing only ‘MSFT’ (Microsoft) as an example. on 9:20am the same day, 10 minutes before market open I set up a MarketOnOpenOrder, which I was hoping gets filled 10 minutes later at market open with the open price of 11-7-2023. Instead the order gets filled with the open price of 11-8-2023. After some discussion with the friendly support staff (kudos to Alex) I learned that in lean in back-testing a daily candle goes from midnight to midnight, i.e. the daily candle for 11-7-2023 is already underway at 9:20am. This is why the order gets filled with the open of next days candle. Understanding this unfortunately does not helping me solve my problem. One quick solution, suggested by Mia, does not work. Running the back-test on higher resolution (e.g. Minute, even though this looks like a terrible waste of resources) by setting line 21 of the back-test to:
self.UniverseSettings.Resolution = Resolution.Minute
This produces the error message:
Backtest Handled Error: MSFT R735QTJ8XC9X: The security does not have an accurate price as it has not yet received a bar of data. Before placing a trade (or using SetHoldings) warm up your algorithm with SetWarmup, or use slice.Contains(symbol) to confirm the Slice object has price before using the data. Data does not necessarily all arrive at the same time so your algorithm should confirm the data is ready before using it. In live trading this can mean you do not have an active subscription to the asset class you're trying to trade. If using custom data make sure you've set the 'Value' property.
and nothing gets bought.
My question is very simple, does anybody know how to buy the fine universe selection at the same day on market open? This sounds like a very common thing to do, and I have a hard time believing I am the first who ran into this issue.
Thank you for any kind of help,
Markus
Melanie Schiefele
Sorry I forgot to mention one thing about the Minute resolution: I tried, but had no luck using a warm up period as suggested in the error message.
Thanks again,
Markus
Mia Alissi
Based on the information provided in the extracts, it seems that the issue you're facing is related to the timing of your universe selection and order placement. In QuantConnect, the universe selection is done at midnight by default. Therefore, when you're trying to place a MarketOnOpen order at 9:20 am, the universe selection for that day has already been done and the order will be filled with the open price of the next day.
One possible solution to this issue is to use minute resolution data for your universe selection. However, as you've mentioned, this approach results in an error message because the security does not have an accurate price as it has not yet received a bar of data.
Unfortunately, the extracts provided do not contain a direct solution to your problem. However, they do suggest that you might need to adjust the timing of your universe selection and order placement to ensure that the orders are filled at the desired time.
Here is a relevant extract that might help:
"UniverseSelectionModel is called every day at midnight 00:00 by default. self.UniverseSettings.Resolution determines the requested data resolution for symbols returned by the UniverseSelectionModel. If you use daily resolution here, you only get the daily data for those symbols. Therefore, all market orders will be converted to the MakretOnOpen() orders. When you try to place the market order x minutes before the market close, there's no minute price data because you only request the daily data, you'll get the error message like "The order quantity for XXX cannot be calculated: the price of the security is zero"." source
I would recommend checking out the QuantConnect documentation and forum discussions for more information on how to handle this issue.
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.
Melanie Schiefele
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!