LEAN is the open source
algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a
global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool
Qualifying Alpha Streams Reentered Weekly Learn
more
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?
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.
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
42.9k
,
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();
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
JayJayD
42.9k
,
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:
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.
#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")
3
Jared Broad
STAFF
,
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.
LaurentM
45
,
Is it possible to use the Schedule function for a daily resolution backtest?
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...
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!