I'm attempting to schedule a single event on the backtest start date. Here was my attempt, but it does not work when the backtest the start date is not a trading day:
self.Schedule.On(self.DateRules.On(self.Time.year, self.Time.month, self.Time.day),
self.TimeRules.BeforeMarketClose(self.tickers[0], 15),
self.Rebalance)
Please let me know if there is a way to set the date to be the next trading day.
Thanks!
Hao Bin Zhang
Hi,
You can try this:Â
self.DateRules.EveryDay("SPY")Â
#Trigger an event every day a specific symbol is trading.
If you are trading in the SPY universe you can set it so it would only trade if the market is open.Â
For reference:Â
https://www.quantconnect.com/docs/algorithm-reference/scheduled-events
Â
Brad Hallett
Thanks for your response, Hao. I believe this will perform the task every day whereas I am looking for it to perform the task on a single day.
Hao Bin Zhang
Hi,Â
Perhaps try this?
self.Schedule.On(self.DateRules.On(2013, 10, 7), \ self.TimeRules.At(13, 0), \ self.SpecificTime)
def SpecificTime(self): self.Log("SpecificTime: Fired at : {0}".format(self.Time))
This will trigger on the day at a specific time.Â
Brad Hallett
Thanks for this, Hao. Unfortunately, this code will only work if it is fired on a day that the market is open. I'm hoping to have the code detect if the market is open...
Brad Hallett
Here is my solution:
upcoming_trading_days = self.TradingCalendar.GetDaysByType(TradingDayType.BusinessDay, self.Time, self.Time+timedelta(days=5)) for dates in upcoming_trading_days: next_trading_day = dates.Date break self.Schedule.On(self.DateRules.On(next_trading_day.year, next_trading_day.month, next_trading_day.day), self.TimeRules.BeforeMarketClose(self.tickers[0], 15), self.Rebalance)
Â
Yiyun Wu
Hi Brad,
Do you mind specify how you want to use the schedule event? Or you can attach your backtesting here, then we can find out if there could be a better way to solve your problem.
Thanks
Brad Hallett
Hello,
My goal is to input my initial trades at the end of the first trading day after the algorithm has started. The code above has solved this but I am open to any recommendations for improvement to my code.
Thanks,
Brad
Brad Hallett
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!