I came across this while testing and end of month strategy. It seems, that QC is skipping some months entries, when using schedule.on():
from AlgorithmImports import *
from datetime import timedelta
from math import *
class GLDeom(QCAlgorithm) :
def Initialize(self) :
self.set_start_date(2009, 1, 1)
self.set_end_date(2009, 12, 31)
self.set_time_zone(TimeZones.NEW_YORK)
self.set_cash(10000)
self.set_brokerage_model(BrokerageName.ALPACA, AccountType.MARGIN)
self.gld = self.add_equity("GLD", Resolution.MINUTE).symbol
# Schedule entry at end of month, 2 minutes before market close
self.schedule.on(
self.date_rules.month_end(),
self.time_rules.before_market_close(self.gld, 2),
self.enter_position
)
def enter_position(self) :
self.debug(f"{self.time.date()}")
def on_data(self, slice) :
pass
Result:
5|3:34:46: 2009-03-31
6|3:34:46: 2009-04-30
7|3:34:46: 2009-06-30
8|3:34:46: 2009-07-31
9|3:34:46: 2009-08-31
10|3:34:46: 2009-09-30
11|3:34:46: 2009-11-30
12|3:34:46: Algorithm 'eae88f894063b8abd523cd01ca81e37e' completed
13|3:34:46: 2009-12-31
So monhts 1, 2, 5 and 10 are missing. What could be causing this?
Mia Alissi
The issue you're encountering with the
schedule.on()
method skipping some months can be attributed to the way themonth_end
method is used. Here are some key points to consider:Symbol Specification: The
month_end
method can take a symbol as an argument to determine the last tradable date for that specific symbol. If no symbol is provided, it defaults to the last calendar day of the month, which might not be a trading day.Days Offset: Ensure that the
days_offset
parameter is set to zero. Any non-zero value might cause the event to fire on a different day than expected.Extended Market Hours: The
extended_market_hours
parameter defaults toTrue
. If your symbol does not trade on extended market hours, this might affect the tradable dates considered. You might want to set this parameter toFalse
if you are not interested in extended market hours.Trading Calendar: Check the trading calendar for the specified symbol to ensure there are no holidays or non-trading days that might affect the scheduled events.
To address the issue, you can modify the
month_end
method to include the symbol and ensure that theextended_market_hours
parameter is set according to your requirements. Here's how you can adjust your code:By specifying the symbol and setting
extended_market_hours
toFalse
, you should see more consistent scheduling at the end of each month.Sources: Scheduled Events - QuantConnect.com, Scheduled Events Algorithm, Paired Switching, Turn Of The Month In Equity Indexes, Seasonality Effect Based On Same Calendar Month Returns
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.
David Maucher
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!