from pandas_datareader import data as web
def load_historical_data(tickers: list[str], start_date: datetime.date, end_date) -> pd.DataFrame:
"""Download historical data since start_date from Stooq"""
history_df = pd.DataFrame()
for idx, ticker in enumerate(tickers):
ticker_df: pd.DataFrame = web.DataReader(ticker, "stooq", start_date, end_date)
if len(ticker_df) == 0:
print(f"failed to get {ticker} data")
continue
history_df = history_df.join(ticker_df["Close"].rename(ticker), how="outer")
print("#", end="\n" if (idx + 1) % 20 == 0 else "")
history_df.dropna(how="any", inplace=True) # Only keep days when all stocks were traded
history_df.sort_index(inplace=True)
return history_df
# Acquire stocks comprising the NASDAQ 100
tickers = ["ADBE", "ADP", "ABNB", "GOOGL", "GOOG", "AMZN", "AMD", "AEP", "AMGN", "ADI",
"ANSS", "AAPL", "AMAT", "ASML", "AZN", "TEAM", "ADSK", "BKR", "BIIB", "BKNG",
"AVGO", "CDNS", "CDW", "CHTR", "CTAS", "CSCO", "CCEP", "CTSH", "CMCSA", "CEG",
"CPRT", "CSGP", "COST", "CRWD", "CSX", "DDOG", "DXCM", "FANG", "DLTR", "DASH",
"EA", "EXC", "FAST", "FTNT", "GEHC", "GILD", "GFS", "HON", "IDXX", "ILMN",
"INTC", "INTU", "ISRG", "KDP", "KLAC", "KHC", "LRCX", "LIN", "LULU", "MAR",
"MRVL", "MELI", "META", "MCHP", "MU", "MSFT", "MRNA", "MDLZ", "MDB", "MNST",
"NFLX", "NVDA", "NXPI", "ORLY", "ODFL", "ON", "PCAR", "PANW", "PAYX", "PYPL",
"PDD", "PEP", "QCOM", "REGN", "ROP", "ROST", "SIRI", "SBUX", "SNPS", "TTWO",
"TMUS", "TSLA", "TXN", "TTD", "VRSK", "VRTX", "WBA", "WBD", "WDAY", "XEL", "ZS"]
df = load_historical_data(tickers, datetime.date(2023, 1, 1), datetime.date.today())