I am attempting to access the previous hour's asset price, but I consistently encounter a runtime error: "Runtime Error: Index was outside the bounds of the array." Any example demonstrating a solution to this issue would be greatly appreciated.
# Get the previous hour's price
previous_hour = self.Time - timedelta(hours=1)
history = self.History([self.spy], previous_hour, 1, Resolution.Hour)
Mia Alissi
Based on the information provided in the extracts, it seems like 'Hasan' is trying to access historical data for the previous hour. However, the error message suggests that there might be an issue with the way the historical data is being accessed.
In QuantConnect, you can use the
Historymethod to access historical data. TheHistorymethod takes a list of symbols, a start time, an end time, and a resolution as arguments.Here is an example of how to use the
Historymethod to get the previous hour's data:In this example,
self.History([self.spy], 2, Resolution.Hour)returns a DataFrame with the last 2 hours of data.history.loc[self.spy]["close"][-2]then accesses the closing price of the second last row in the DataFrame, which corresponds to the previous hour's closing price.Please note that this is just an example and you might need to adjust the code to fit your specific needs.
For more information on how to use the
Historymethod, you can refer to the QuantConnect documentation.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!