Skip to content

Instantly share code, notes, and snippets.

@shashankvemuri
Last active January 4, 2024 05:28
Show Gist options
  • Save shashankvemuri/3de4b93d934cf61974efebb1b5dd58fe to your computer and use it in GitHub Desktop.
Save shashankvemuri/3de4b93d934cf61974efebb1b5dd58fe to your computer and use it in GitHub Desktop.
Import these dependencies and set the variables
# Imports
from pandas_datareader import data as pdr
from pandas import ExcelWriter
import yfinance as yf
import pandas as pd
import datetime
import time
import requests
yf.pdr_override()
# Get tickers for all S&P 500 stocks from Wikipedia
def tickers_sp500():
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
html = requests.get(url).text
df = pd.read_html(html, header=0)[0]
tickers = df['Symbol'].tolist()
return tickers
# Variables
tickers = tickers_sp500()
tickers = [item.replace(".", "-") for item in tickers] # Yahoo Finance uses dashes instead of dots
index_name = '^GSPC' # S&P 500
start_date = datetime.datetime.now() - datetime.timedelta(days=365)
end_date = datetime.date.today()
exportList = pd.DataFrame(columns=['Stock', "RS_Rating", "50 Day MA", "150 Day Ma", "200 Day MA", "52 Week Low", "52 week High"])
returns_multiples = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment