I am developing a strategy with the Brain Ranking dataset.
This universe contains 1.000 stocks and based on the ranking of the stocks I create a portfolio.
In the attached backtest I select the top 10 stocks for my portfolio. However, I don't buy 10 stocks, because the universe has stocks outside of the US stock market:
Backtest Handled Error: GDI WKGKBTQLVCYT: 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.
How can I make sure that I filter out these stocks before I buy them. Now I find out that this stock is not available and I only buy 9. It would be nice to filter out these before and make sure that self.selection got 10 stocks that I can buy.
Is there any way to detect those stocks in my filtered universe?
Mia Alissi
To address the issue Alex is facing with stocks not having an accurate price at the time of purchase, a check can be added in the
OnData
method to ensure that the slice contains data for the symbol before attempting to buy. This can be done usingslice.ContainsKey(symbol)
orslice.Contains(symbol)
to confirm the Slice object has price data for the symbol before using it. Here's how you can modify theOnData
method to include this check:This modification ensures that the algorithm attempts to buy stocks only if their price data is available in the current slice. If the data is not available, it skips the stock for that time step, preventing the error Alex encountered. This approach helps in dynamically filtering out stocks that do not have price data available at the time of trading, ensuring that the algorithm only trades stocks with available data.
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.
Alex Veraart
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!