Hello,
I'm new to QC, and what I trying to achieve is the following:
- Once a day, my code being called (after or before market hours)
- My code iterates all stocks in the US market, and selects investment candidates based on some criterias
- Makes orders if needed, renew existing pending orders if expired.
For start, I tried to do the following:
class SquareYellowDolphin(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 8, 21)
self.SetEndDate(2023, 10, 21)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily # Set universe resolution to daily
self.AddUniverse(self.coarse_selection_function)
def coarse_selection_function(self, coarse):
self.Debug(f'Coarse selection called at [{self.Time}]')
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in sortedByDollarVolume[:100]]
def OnData(self, data: Slice):
self.Debug(f'OnData called at [{self.Time}]')
Expectation: ‘coarse_selection_function’ will be called once a day, will select stocks according to criteria. ‘OnData’ will be called once a day, and could review selected stocks and do whatever needed.
Actual: ‘coarse_selection_function’ called once a day (as expected) but ‘OnData’ called every minute.
How to achieve what I need (e.g. make ‘OnData’ called once a day)? Am I in the right direction at all, or it should be done somehow differently? Appreciate any help, thanks.
Mia Alissi
To ensure your algorithm's main logic executes just once a day, you should use QuantConnect's
Schedule
method to trigger specific events at desired times, rather than relying on theOnData
method which processes data streams often (as in minute or second resolution). Here's how you can adjust your algorithm to accomplish your goals:Schedule a Daily Event: Utilize the scheduling feature to run your code at a specific time each day. This can be achieved by setting
self.Schedule
in yourInitialize
method.Daily Task Logic: Put your daily logic in a separate method that will be triggered by the scheduled event.
Order Management: Within the scheduled daily task function, manage new order placements or the renewal of existing orders.
Below is an adjusted version of your code using scheduled events:
Key Points
self.schedule.on()
for the scheduled events. This ensures the specified method runs at precise times, such as after the market opens.daily_task()
with your actual investment criteria and order management.This approach allows you to decouple the stream processing
OnData()
from your main daily logic, relying instead on a more deterministic schedule.Sources: Scheduled Events - QuantConnect.com, Scheduled Events Algorithm, Key Concepts - QuantConnect.com, Scheduled Queuing Algorithm, Dual Thrust Trading 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.
Evgeni Aizikovich
Thank you for answering! And another quick question: how to configure universe to US market stocks? To use “US Equity Security Master” dataset in coarse selection?
Thanks!
Louis Szeto
Hi Evgeni
The US Equity Security Master is only for data normalization factoring and an event notice. It is not available for universe selection. Instead, you may consider combining the Upcoming Splits dataset with fundamental selection. You can find how to chain universes here.
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.
Evgeni Aizikovich
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!