Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Last active February 13, 2024 21:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save quantra-go-algo/ac5180bf164a7894f70969fa563627b2 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/ac5180bf164a7894f70969fa563627b2 to your computer and use it in GitHub Desktop.
Python code to get price data for all S&P500 tickers
# Import packages
import yfinance as yf
import pandas as pd
# Read and print the stock tickers that make up S&P500
tickers = pd.read_html(
'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
print(tickers.head())
# Get the data for this tickers from yahoo finance
data = yf.download(tickers.Symbol.to_list(),'2021-1-1','2021-7-12', auto_adjust=True)['Close']
print(data.head())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment