I'm having an issue where I have many cases of the following error for various securities:
Backtest Handled Error: CIVI VTZFLBN7T1LX: 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.
My algorithm is a very simple:
- dynamic universe
- buys securities that are added
- liquidates securities that are removed
- uses warm-up period
Any ideas what might be going on?
Thank you in advance for any help!
Mia Alissi
Based on the error message you're receiving, it seems like you're trying to trade a security that hasn't yet received a bar of data. This can happen if you're trying to trade a security immediately after it's been added to your universe, before the algorithm has had a chance to receive and process price data for that security.
In your
OnData
method, you're checking if the security is tradable and if it has data before placing a trade. However, you're placing the trade immediately after these checks, which might be causing the issue.One way to handle this is to use the
slice.Contains(symbol)
method to confirm that theSlice
object has price data for the security before using the data. This method returnsTrue
if data for the specified symbol is present in the slice.Here's how you can modify your
OnData
method to include this check:This should help ensure that you're only trying to trade securities that have received a bar of data.
For more information, you can refer to these discussions on the QuantConnect forum where similar issues have been discussed:
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.
Craig Nathan
Thanks for your response, but I believe I am already satisfying this check that you're suggesting with this existing clause in my if statement:
When I add your suggestion, I get errors because (a) the data parameter in the OnData() method already is a slice object, and (b) slice objects don't have a method or attribute ‘Contains’. So, I believe the existing clause in my if statement, `data.ContainsKey(symbol)`, is already doing what you're suggesting.
Mia Alissi
I apologize for the confusion. You're correct that the
Slice
object in theOnData
method doesn't have aContains
method. Thedata.ContainsKey(symbol)
check you're already doing is indeed the correct way to verify that theSlice
object has data for the specified symbol.However, the error message you're seeing suggests that the security doesn't have an accurate price because it hasn't yet received a bar of data. This might be due to the fact that you're trying to trade the security immediately after it's added to your universe, before the algorithm has had a chance to receive and process price data for that security.
One possible solution could be to introduce a delay or a check that ensures that the necessary data is available before trading. For example, you could use the
History
method to request historical data for the security and only trade if this data is available.You're already calling the
History
method in your code, but it seems like you're not using the returned data to check if the necessary data is available. You could modify your code to only callSetHoldings
if theHistory
method returns data for the security:This will ensure that you're only trying to trade securities for which you have historical data, which should help avoid the error you're seeing.
For more information, you can refer to these discussions on the QuantConnect forum where similar issues have been discussed:
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.
Craig Nathan
Thanks for the follow up, but this did not resolve the issue either.
Mia Alissi
I'm sorry to hear that the issue persists. Without more specific information about the error or the context in which it occurs, it's challenging to provide a more precise solution.
However, it's worth noting that the error message you're seeing typically occurs when an algorithm attempts to trade a security that hasn't yet received any data. This can happen if the security was recently added to the universe and the algorithm hasn't yet received or processed any data for it.
One possible workaround could be to introduce a delay before trading a newly added security, giving the algorithm some time to receive and process the necessary data. This could be done by storing the symbols of newly added securities and only trading them after a certain number of data updates have been received.
If the issue persists, I would recommend reaching out to the QuantConnect support team at support@quantconnect.com. They might be able to provide more specific guidance based on the details of your algorithm and the exact error message you're seeing.
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.
Craig Nathan
I was able to fix it by adding the check for self.Securities[symbol].Price > 0. Now backtesting with no errors! (removed history call and check as this was redundant)
Mia Alissi
I'm glad to hear that you were able to resolve the issue by adding a check for
self.Securities[symbol].Price > 0
. This check ensures that the security has a valid price before attempting to trade it, which can help avoid errors when the algorithm hasn't yet received any data for the security.Your updated code looks correct based on the information provided. It correctly checks that the security has data, has a valid price, is not already invested, and is tradable before calling
SetHoldings
.Here's the relevant part of your
OnData
method for reference:This code correctly uses the
data.ContainsKey(symbol)
method to check if theSlice
object has data for the security, checks that the security's price is valid and greater than 0, and checks that the security is not already invested and is tradable before callingSetHoldings
.If you encounter any further issues or have any other questions, feel free to ask. Happy coding!
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.
Craig Nathan
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!