Skip to content

Instantly share code, notes, and snippets.

@boyboi86
Created May 19, 2020 04:21
Show Gist options
  • Save boyboi86/ab5ac0de470256c9b154f281ad715c7c to your computer and use it in GitHub Desktop.
Save boyboi86/ab5ac0de470256c9b154f281ad715c7c to your computer and use it in GitHub Desktop.
This func draws data directly from yahoo finance. This requires pandas_datareader, only for simple resampling uses. Otherwise pls obtain proper data set.
import pandas as pd
import datetime as dt
import pandas_datareader.data as pdr
p = print
# =============================================================================
# return dataframe
# =============================================================================
def YF_data(period, tickers):
df = pd.DataFrame() # dataframe to store close price of each ticker
# =============================================================================
# Sample Period yearly basis
# =============================================================================
period = period
sampleperiod = 365 * period
startdate = dt.date.today() - dt.timedelta(days = sampleperiod)
# =============================================================================
# Ticker symbol inputs
# =============================================================================
tickers = tickers
# =============================================================================
# return dataframe
# =============================================================================
#df = pd.DataFrame() # dataframe to store close price of each ticker
attempt = 0 # initializing passthrough variable
drop = [] # initializing list to store tickers whose close price was successfully extracted
while len(tickers) != 0 and attempt <= 5:
tickers = [j for j in tickers if j not in drop] # removing stocks whose data has been extracted from the ticker list
for i in range(len(tickers)):
try:
temp = pdr.get_data_yahoo(tickers[i], startdate, dt.date.today())
temp.dropna(inplace = True)
df[tickers[i]] = temp["Adj Close"]
drop.append(tickers[i])
except:
p(tickers[i]," :failed to fetch data...retrying")
continue
attempt+=1
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment