What is the best practice to get the historical prices and volumes in OnData() function?
I'd like to get the following data in OnData() to compare.
- current price and volume of spy
- 1 day ago high and low prices of spy
- 2 days ago high and low prices of spy
- 1 days ago volume of spy
Mia Alissi
To get the historical prices and volumes in the
OnData()
function, you can use theHistory()
method. This method returns a pandas DataFrame if you request more than one bar of data. The DataFrame has a multi-index with the first level being the symbol and the second level being the time.Here is an example of how you can use the
History()
method to get the data you need:Please note that the
History()
method returns the historical data in reverse chronological order, soiloc[-1]
gives the data for 1 day ago andiloc[-2]
gives the data for 2 days ago.For more information about the
History()
method, you can refer to the QuantConnect documentation.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.
Norris Codman
Thank you so much for the quick answer, Mia! I'll try that.
Mia Alissi
You're welcome, Norris! If you have any more questions or run into any issues, don't hesitate to ask. Happy coding!
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.
Norris Codman
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!