Hello QuantConnect team!
I have an issue with the OnEndOfAlgorithm method. It often executes before my system finishes trading, which creates errors when crucial variables are not yet assigned.
What is going on? And how can I make a function that will always execute at the end of my algorithm?
Thanks!
Mia Alissi
The
OnEndOfAlgorithm
method is an event handler that is triggered when your algorithm has finished executing as its final step. This is typically used for performing final analysis or saving the state of your algorithm. If it's executing before your system finishes trading, it might be because your trading operations are not fully completed or are running asynchronously.Here is an example of how you can use the
OnEndOfAlgorithm
method in Python:This method is called at the end of a backtest or live trading operation and is intended for closing out logs. It does not require any argument input and does not provide a return.
If you want to ensure that certain operations are completed before the end of your algorithm, you might want to consider structuring your code such that these operations are completed within the main trading loop or within other event handlers like
OnData
.For more information, you can refer to the QuantConnect documentation on event handlers.
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.
Tom Frew
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!