Hello,
I want to execute coarse filter only at a specific time interval. Like weekly, 15 days or monthly. I will take example of monthly -
if self.Time.month == self.current:
return Universe.Unchanged
self.current = self.Time.month
How is it different from -
if self.Time.month - self.current < 2:
return Universe.Unchanged
self.current = self.Time.month
Considering we have initialized self.current on init -
self.current = self.Time.month
I get different results for both the scenarios above.
Also, how can I make the code run every 15 days or every week?
Thanks,
Ray
Rahul Chowdhury
Hey Ray,
The algorithm returns different results because
if self.Time.month == self.current: return Universe.Unchanged self.current = self.Time.month
rebalances once a month, while
if self.Time.month - self.current < 2: return Universe.Unchanged self.current = self.Time.month
rebalances once every two months.You can rebalance every week by using the week number of the datetime object, which is accessed with time.isocalender()[1]
if self.Time.isocalendar()[1] == self.current: return Universe.Unchanged self.current = self.Time.isocalender()[1]
One way to rebalance every 15 days is to use a pointer to keep track of our next rebalance date and wait until that date.
if self.Time < self.nextRebalance: return Universe.Unchanged self.nextRebalance = self.Time + timedelta(days=15)
Best
Rahul
Ray Trader
Hi Rahul,
Thanks for the details. I will check it out. Is there a place for official documentation for methods available for "Time" or it is a default python class that can be checked?
Rahul Chowdhury
Hi Ray,
Time returns a datetime object, which is a default python class. You can find more information about datetime in the python docs.
Ray Trader
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!