Instead of looping all symbols on by one and get the history for each symbol, is it optimal to get history for all symbols as below code as an example. Please confirm it below optimization is correct and best practice
Before Optimization
for index, row in self.breakout_symbols:
symbol_str = row['Symbol']
symbol = Symbol.Create(symbol_str, SecurityType.Equity, Market.USA)
breakout_date = row['Date']
initial_price = row['close_price']
history = self.History(symbol, 1, Resolution.Daily)
if history.empty:
continue
current_price = history['close'].iloc[0]
//logic for using current_priceAfter Optimization
symbols_to_check = []
for idx, row in self.breakout_symbols:
symbol = Symbol.Create(row['Symbol'], SecurityType.Equity, Market.USA)
symbol_index_map[symbol] = idx
symbols_to_check.append(symbol)
history = self.History(symbols_to_check, 1, Resolution.Daily) if symbols_to_check else pd.DataFrame()
for symbol, idx in symbol_index_map.items():
row = self.breakout_stocks.loc[idx]
breakout_date = row['Date']
initial_price = row['close_price']
sym_hist = history.xs(symbol, level=0) if not history.empty else pd.DataFrame()
if sym_hist.empty:
continue
current_price = sym_hist['close'].iloc[0]
//logic for using current_price
Mia Alissi
Here’s a detailed explanation for Dharmesh on the best practice for fetching historical data for multiple symbols in QuantConnect, using your code as an example:
Best Practice for Fetching History for Multiple Symbols
Before Optimization: Per-symbol Requests (Not Optimal)
After Optimization: Batch (Vectorized) Request (Optimal, Recommended)
.xs(symbol, level=0)or.loc[symbol], only proceeding if not empty.Why the Batch Approach is Best
Best Practice Code Structure
self.History(symbols, ...)once, producing a MultiIndex pandas DataFrame.Example Extraction:
Summary:
The batch-history method demonstrated in your 'After Optimization' version is the correct and recommended best practice in QuantConnect. It is faster, more reliable, and more scalable for professional algorithmic trading research or production code.
Let me know if you’d like a further example or clarification!
Sources: Key Concepts - QuantConnect.com, Getting Started - QuantConnect.com, Using Data - QuantConnect.com, History Algorithm, Gaussian Naive Bayes Model
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.
Dharmesh Khalasi
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!