Given a Coarse and Fine resolution of daily that occurs at 12:00am i.e.
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
How can I optimise this to only filter on the first trading day of the year? My backtests take a really long time because it goes through these Coarse and Fine filter functions every day.
I only want to rebalance on the first trading day of the year? I know we can use
self.AddEquity("SPY")
self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.BeforeMarketOpen("SPY"), self.Rebalance)
to trigger at 9:30am EST. but how can I make sure the Coarse and Fine filters only do heavy computations on the day of my Schedule.On functions (market open trading days)?
Thank you.
Alethea Lin
Hi Jason,
The workaround for this would be to introduce a manual checkpoint within the Coarse and Fine filters. The checkpoint checks if it has passed a certain time period. If not, it will return Universe.Unchanged to stay in the same universe. This allows the algorithm to skip the heavy computations and should speed up the backtest.
def Initialize(self): self.nextRebalance = self.Time def CoarseSelectionFunction(self, coarse): if self.Time < self.nextRebalance: return Universe.Unchanged def OnData(self, data): # Do your executions # Set next rebalance time self.nextRebalance = Expiry.EndOfMonth(self.Time)
Hope this helps!
Jason Mark
Hi Alethea,
Thank you for taking the time to analize my question and help me. I actually had a hard time articulating it. I learned something new. I didn't know about the
Expiry.EndOfMonth(self.Time)
function. I will try using that the next time I run into a similar situation. In your example, I believe
if self.Time < self.nextRebalance:
should be
if self.Time > self.nextRebalance:
to prevent daily universe filters. I think the only way to run the coarse and fine filters on the rebalance day prior to market open is if you know the trading day in advance and in my case I didn't know how to determine this. This is what I did to get around excessive computations. The following code filters once a month and rebalances once a month. The coarse and fine filter may not nessessarly be on the same day as the rebalance function but it worked in my case because the filters were fundamental in nature. I was able to change this to yearly filter and rebalance intervals by changing self.Time.month to self.Time.year.
def Initialize(self): self.lastFilterDate = None self.lastRebalanceDate = None self.UniverseSettings.Resolution = Resolution.Daily self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction) self.AddEquity("SPY") self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), self.Rebalance) ... def CoarseSelectionFunction(self, coarse): if self.lastFilterDate == self.Time.month: return Universe.Unchanged ... return coarseFiltered def FineSelectionFunction(self, fine): if self.lastFilterDate == self.Time.month: return Universe.Unchanged self.lastFilterDate = self.Time.month ... return fineFiltered def Rebalance(self): if self.lastRebalanceDate == self.Time.month: return self.lastRebalanceDate = self.Time.month ... pass
Regards,
Jason Mark
I just reanalyzed. You are correct about this
if self.Time < self.nextRebalance: return Universe.Unchanged
Jason Mark
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!