Hi,
I am very new to QuantConnect, so the question may seem stupid. After studying the Bootcamp, I am trying to write my first toy example, which is to select the top 1 weighted stock from ETF “SPY”. When I back test this example, no trade occurs, and I received the following error message:
Backtest Handled Error: AAPL R735QTJ8XC9X: The security does not have an accurate price as it has not yet received a bar of data.
Can anyone point to me where I got wrong? Any help is much appreciated!
Thanks
from AlgorithmImports import *
# Initialize asynchronous settings for speed and use the ETFConstituentsUniverseSelectionModel
# to select the top 1 SPY constituents by weight, focusing on blue-chip stocks with minimal risk.
class FrameworkETFConstituentsUniverseSelectionAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2024, 1, 1)
self.set_end_date(2024, 1, 15)
self.set_cash(1000000)
# self.spy = self.add_equity("SPY").symbol
self.universe_settings.asynchronous = True
self.add_universe_selection(ETFConstituentsUniverseSelectionModel("SPY", universe_filter_func=self.etf_constituents_filter))
self.universe_settings.resolution = Resolution.DAILY
self.weight_by_symbol = {}
def etf_constituents_filter(self, constituents: List[ETFConstituentUniverse]) -> List[Symbol]:
# The top 10 weighted securities are considered better active selections
# Save the weights for position sizing
selected = sorted([c for c in constituents if c.weight],
key=lambda c: c.weight, reverse=True)[:1]
self.weight_by_symbol = {c.symbol: c.weight for c in selected}
return list(self.weight_by_symbol.keys())
def on_securities_changed(self, changes: SecurityChanges) -> None:
# Liquidate the ones not in top 10 weights
# for security in changes.removed_securities:
# history = self.history(security.symbol, 7, Resolution.DAILY)
# self.log(f"Removed: {security.symbol}")
self.changes = changes
for security in changes.removed_securities:
if security.invested:
self.liquidate(security.symbol)
for security in changes.added_securities:
# Historical data
history = self.History(security.symbol, 7, Resolution.DAILY)
self.log(f"Added: {security.symbol}")
self.log(f'We got {len(history)} from our history request for {security.symbol}')
self.set_holdings(security.symbol, 0.1)
Mia Alissi
Here is the corrected version of the algorithm:
This version addresses the type-related issues and ensures that the algorithm correctly handles securities and their historical data.
Sources: Momentum Effect In Commodities Futures, E T F Constituent Universe R S I Alpha Model Algorithm, A Risk Parity Approach to Leveraged ETFs, Momentum Short Term Reversal Strategy, Head & Shoulders TA Pattern Detection
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.
Louis Szeto
Hi Leo
No data feed has been piped yet when security was just added to the universe, so we cannot fill your order without any price data. Actually, you can do the following:
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.
Leo Ma
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!