I have below code which filter the stocks within 5% of 52week high. However, I want to subscribe those stocks which met criteria and check on on_data event if price increase more than 1% since open. How to implement on_data event to check the price increase 1% for stocks which are in the selected_symbols
self.universe_settings.resolution = Resolution.DAILY
self.add_universe_selection(ETFConstituentsUniverseSelectionModel("SPY", self.universe_settings, self.etf_constituents_filter))
self.add_universe_selection(
ETFConstituentsUniverseSelectionModel("SPY", self.universe_settings, self.etf_constituents_filter)
def etf_constituents_filter(self, constituents):
# Define your filtering logic here
# Get historical data for the last 52 weeks
history = self.history([c.symbol for c in constituents], 252, Resolution.DAILY)
selected_symbols = []
for constituent in constituents:
symbol = constituent.symbol
if symbol not in history.index:
continue
# Calculate the 52-week high
prices = history.loc[symbol]['close']
high_52_week = prices.max()
# Get the current price
current_price = prices.iloc[-1]
# Check if the current price is within 5% of the 52-week high
if current_price >= 0.95 * high_52_week:
selected_symbols.append(symbol)
self.Debug(f"Updated buyable stocks: {self.buyable_stocks}")
return selected_symbols
Mia Alissi
Here's the corrected version of the code with the necessary adjustments:
Key Changes:
self.universe_settings.resolution
toself.UniverseSettings.Resolution
.self.add_universe_selection
toself.AddUniverseSelection
.self.history
toself.History
.self.add_equity
toself.AddEquity
.self.Debug
toself.Debug
.on_data
toOnData
.Resolution.Daily
instead ofResolution.DAILY
.slice.Bars.ContainsKey(symbol)
instead ofslice.bars.contains_key(symbol)
.trade_bar.Close
instead oftrade_bar.close
.Sources: Handling Data - QuantConnect.com, Event Handlers - QuantConnect.com, Dropbox Base Data Universe Selection Algorithm, Custom Data Universe Algorithm, E T F Constituent Universe R S I Alpha Model Algorithm
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.
Dharmesh Khalasi
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!