self.SetCash(100000)
start_time = datetime(2022, 1, 1)
end_time = datetime(2022, 1, 4)
stocks = ["UNH", "GS", "HD", "MSFT", "MCD", "AMGN", "V", "CRM", "CAT", "HON"]
stock_symbols = [self.AddEquity(stock, Resolution.Daily).Symbol for stock in stocks]
self.stocks_df = self.History(stock_symbols,start_time,end_time)
self.Debug(f"Stocks Historical DataFrame:\n{self.stocks_df.to_string()}")
if not self.stocks_df.empty:
for symbol in stock_symbols:
symbol_bars = self.stocks_df.loc[symbol] # or use the string representation of the symbol
self.Debug(f"{symbol} Bars:\n{symbol_bars.to_string()}")
close_diff = symbol_bars["close"].diff()
self.Debug(f"{symbol} Close Price Difference:\n{close_diff.to_string()}")
It's a quite simple code.I don't know why i can't get the history data this way. OUTPUT:Stocks Historical DataFrame: Empty DataFrame Columns: [] Index: []
Mia Alissi
The issue you're experiencing might be due to the way you're using the
self.History
method. In your code, you're trying to useself.history(TradeBar,stock_symbols , start_time, end_time)
which is not the correct way to use this method according to the QuantConnect documentation.The correct way to use the
self.History
method when requesting historical data for multiple symbols is to pass the list of symbols, the start time, and the end time as arguments. Here is an example from the documentation:In your case, you should modify your code to look like this:
This should return a DataFrame with the historical data for the symbols in
stock_symbols
betweenstart_time
andend_time
. If you still get an empty DataFrame, make sure that thestart_time
andend_time
are within the data range for the symbols you're requesting.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.
Zzz xk
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!