I'm looking to determine if a day is the first day of the month. I know there is the schedule event function that leverages DateRules.MonthStart("SPY")
I just want a boolean that is true if it is MonthStart and False otherwise. It does not necessarily have to trigger an event
How should I set this up? I would assume it is straightforwad but cannot find the annswer
Gahl Goziker
Hi Goofball,
The GetTradingDays method provides reference on which dates the market is open.
We can then use that to find the first day in the month when the market was open, and check if we’re currently on that day.
Example:
def OnData(self, data): today = data.Time.date() if self.FirstTradingDayOfMonth(today): self.Debug(str(today) + " is the first trading day of the month.") else: self.Debug(str(today) + " isn't the first trading day of the month.") def FirstTradingDayOfMonth(self, today): firstDayOfCurrentMonth = datetime.date(today.year, today.month, 1) tradingDays = self.TradingCalendar.GetTradingDays(firstDayOfCurrentMonth, self.EndDate) firstTradingDay = next(day for index, day in enumerate(tradingDays) if day.BusinessDay) self.Debug("Comparing " + str(today) + " to " + str(firstTradingDay.Date.date())) # Compare datetime.date objects return today == firstTradingDay.Date.date()
Please see the attached backtest for reference.
Best,
Gahl Goziker
https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_863eeb45a411ce818eb03272ef109aee.html (
Goofball
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!