I am newbie learning how to work with QuantConnect. One of my first things is to learn to run scheduled events. This is my first attempt. Any comments or help to solve my issues is greatly appreciated.
Attached backtest will show the following results:
695 | 13:25:18: Launching analysis for 68018fd385ee1bb284ef0050c3a326f0 with LEAN Engine v2.4.0.0.4278
696 | 13:25:28: Method: BeforeMarketOpen Start - 10/10/13 9:00:00:000
697 | 13:25:28: Method: BeforeMarketOpen End - 10/10/13 9:00:00:000
698 | 13:25:28: Method: TimelyCheck Start - 10/10/13 9:31:00:000
699 | 13:25:28: Purchased Stock
700 | 13:25:28: Method: TimelyCheck Start - 10/10/13 11:31:00:000
701 | 13:25:28: Method: TimelyCheck Start - 10/10/13 13:31:00:000
702 | 13:25:28: Method: TimelyCheck Start - 10/10/13 15:31:00:000
703 | 13:25:28: Method: BeforeMarketClose Start - 10/10/13 15:35:00:000
704 | 13:25:29: Algorithm Id:(68018fd385ee1bb284ef0050c3a326f0) completed in 11.21 seconds at 0k data points per second. Processing total of 8 data points.
Questions that I have:
- I put a sleep event in BeforeMarketOpen, but I don't see it being applied. What's going on?
- Why is OnEndOfDay not being called?
Additionally, if "AddEquity(_spy, Resolution.Hour);" is commented, I get the following error:
715 | 13:30:59:
During the algorithm initialization, the following exception has occurred: not found in portfolio. Request this data when initializing the algorith
Gurumeher Sawhney
The Thread.Sleep event should execute in the cloud, but it is not good practice when using QuantConnect's API. Since LEAN is event-driven, it is better to use the Time property of QCAlgorithm to manage waits.
The reason why OnEndOfDay is not being called is because the method is triggered at the end of the algorithm day, not market close. As a result, the algorithm ended before OnEndOfDay was called. Extending the backtest to 2 days allowed OnEndOfDay to be called.
AddEquity needs to be called so that the algorithm is being fed data. Since SPY is the only security data being subscribed to it needs to exist in Initialize(). Otherwise, OnData(Slice data) will not be able to purchase SPY. The documentation linked provides a good explanation of QuantConnect's architecture. If you have not attempted the BootCamp courses yet I highly recommend those as well.
Pranav Shah
Thanks Gurumeher Sawhney
I did go through the BootCamp courses, but was curious to see if I could run the scheduled functions without the AddEquity.
I also looked at the documentation regarding "Time" in QCAlgorithm, but do not see what you are talking about.
Pranav Shah
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!