I would like to schedule my algorithm to run every hour after market open. The code below runs every hour. How can I incorporate a check to see if the market is open, and then run it every hour? I am backtesting the code by backdating the dates, so I would like the code to work with backtesting. Any example would be greatly appreciated.
self.Schedule.On(self.DateRules.EveryDay(), \
self.TimeRules.Every(TimeSpan.FromHours(1)), \
self.GenerateBuySignal)
Mia Alissi
You can use the
IsMarketOpenfunction to check if the market is open before running your algorithm. Here is an example of how you can incorporate this into your code:In this example, "SPY" is the ticker symbol for the asset you are trading. Replace it with the symbol of the asset you are trading.
This code will check if the market for "SPY" is open every hour. If the market is open, it will run the
GenerateBuySignalfunction. If the market is not open, it will skip the function.Please note that you need to subscribe to the asset with the same resolution as the scheduled event or higher. In this case, you should subscribe to the asset with at least hourly resolution data.
Sources:
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.
Hasan
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!