I have been trying to implement the Boot Camp for Liquid Universes and it keeps giving the same error for every security added:
Backtest Handled Error: ORCL 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.
I tried many solutions such as warming up the data, checking for price to be non-zero (makes no errors but no securities are traded either), getting last known prices using set_security_initializer and it is still not working. I need human help.
# region imports
from AlgorithmImports import *
# endregion
class LiquidUniverseSelection(QCAlgorithm):
filtered_by_price = None
def initialize(self):
self.set_start_date(2019, 1, 11)
self.set_end_date(2019, 7, 1)
self.set_cash(100000)
self.add_universe(self.coarse_selection_filter)
# Ignore this for now, we'll cover it in the next task.
self.universe_settings.resolution = Resolution.DAILY
self.SetWarmup(timedelta(days=30)) # Warm up with 30 days of data
def coarse_selection_filter(self, coarse):
sorted_by_dollar_volume = sorted(coarse, key=lambda x: x.dollar_volume, reverse=True)
filtered_by_price = [x.symbol for x in sorted_by_dollar_volume if x.price > 10]
return filtered_by_price[:8]
def on_securities_changed(self, changes):
self.changes = changes
self.log(f"on_securities_changed({self.time}):: {changes}")
#1. Liquidate removed securities
for security in changes.removed_securities:
if security.invested:
self.liquidate(security.symbol)
#2. We want 10% allocation in each security in our universe
for security in self.changes.added_securities:
self.set_holdings(security.symbol, 0.1)
Mia Alissi
The error message you're seeing is indicating that the security does not have an accurate price as it has not yet received a bar of data. This can happen when you're trying to place a trade or use
SetHoldings
before the security has received any data.In your code, you're using
SetHoldings
in theon_securities_changed
method, which is called when securities are added or removed from your universe. If a security is added andSetHoldings
is called before the security has received any data, you'll see this error.One way to handle this is to check if the security has data before using
SetHoldings
. You can do this by usingslice.ContainsKey(symbol)
to confirm the Slice object has price before using the data. Here's how you can modify youron_securities_changed
method:This will ensure that
SetHoldings
is only called when the security has data.Also, keep in mind that the market can quickly change price from the time you place the
SetHoldings
request to when the order is filled. If your portfolio allocations sum to exactly 100%, orders may be rejected due to insufficient buying power. You should leave a buffer to account for market movements and fees. This is especially important with daily data where orders are placed overnight [source].If you're still having issues, please let me know!
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.
Quants
This is still not working
Quants
To whoever is getting the same error, It turns out that you should add the following to your initialize method:
So the final code looks like:
Craig Nathan
self.settings.daily_precice_end_time = True works fine for me.Looks like you need to move the SetHoldings() call to OnData(), since data for securities isn't added instantaneously. Doing this in OnData() ensures that data exists since it is event driven and only runs when new data is received.
See backtest below :)
Craig Nathan
Whoops - I overlooked the fact that we don't actually want to check self.changes.added_securities in OnData() - but we can invest only if we have not already invested. See code below!
Quants
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!