Back

Schedule function to run on last trading day of every month

I'm trying to scheudle a function to run on the last trading day of every month.  I see that there is a DateRule for MonthStart, but not one for MonthEnd.  I see that there is the NthLastBusinessDay method, but I'm having trouble getting that to run correctly.  Any tips?

Thank you!

Update Backtest








You can make use of the TradingCalendar

Check the following example:

First you use a Schedule with a DateRules.MonthStart() and set the last tradeable day in a field.

Schedule.On(DateRules.MonthStart(), TimeRules.At(0, 0), () =>
{
var monthLastDay = new DateTime(Time.Year, Time.Month, DateTime.DaysInMonth(Time.Year, Time.Month));
var monthTradingdays = TradingCalendar
.GetDaysByType(TradingDayType.BusinessDay, Time, monthLastDay)
.ToArray();

monthFirstTradableDay = monthTradingdays.First().Date;
monthLastTradableDay = monthTradingdays.Last().Date;
});

Then in another Schedule you will call every day you will check is the last traedable day in the month.

Schedule.On(DateRules.EveryDay(), TimeRules.At(16, 55), () =>
{
if (Time.Date == monthLastTradableDay)
{
// Do Stuff
}

});

Hope it helps!

1

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.


 JayJayD thank you very much for the code sample!

I am trying to write this in Python, but am getting tripped up in working with the object returned by GetDaysByType.   Is there any sort of documentation on using LINQ and Python?

0

I almost don't know the Python port, but I guess you can check what type returns the TradingCalendar and use the usual Python syntax:

e.g.: if TradingCalendar returns a List just use:

month_first_tradable_day = monthTradingdays[0].Date;
month_last_tradable_day = monthTradingdays[-1].Date;
1

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.


Here's my solution in Python.  The type returned by TradingCalendar is not a list; I didn't spend too much time figuring out how to use the LINQ object returned, but iterating over it with a standard foreach Python loop did the trick.

def Initialize(self):

self.Schedule.On(self.DateRules.EveryDay('SPY'), self.TimeRules.At(0, 0), Action(self.ScheduleEndOfMonthRebalance))
self.Schedule.On(self.DateRules.EveryDay('SPY'), self.TimeRules.BeforeMarketClose('SPY', 5), Action(self.Rebalance))


def ScheduleEndOfMonthRebalance(self):

month_last_day = DateTime(self.Time.year, self.Time.month, DateTime.DaysInMonth(self.Time.year, self.Time.month))
trading_days = self.TradingCalendar.GetDaysByType(TradingDayType.BusinessDay, self.Time, month_last_day)

#get the last day in trading_days
for x in trading_days:
self.month_last_trading_day = x.Date.date()


def Rebalance(self):

if self.Time.date() == self.month_last_trading_day:
self.Debug(str(self.Time) + " | last trading day of the month")
1

Nice solution =)

0

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.


Is it possible to use the Schedule function for a daily resolution backtest?

0

Update Backtest





0

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.


Loading...

This discussion is closed