Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@stoffprof
Created February 17, 2018 16:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stoffprof/719ae8d876d3a5539112e74d292339c9 to your computer and use it in GitHub Desktop.
Save stoffprof/719ae8d876d3a5539112e74d292339c9 to your computer and use it in GitHub Desktop.
Read Yahoo price data
class YahooDailyReader():
def __init__(self, symbol=None, start=None, end=None):
import datetime, time
self.symbol = symbol
# initialize start/end dates if not provided
if end is None:
end = datetime.datetime.today()
if start is None:
start = datetime.datetime(2010,1,1)
self.start = start
self.end = end
# convert dates to unix time strings
unix_start = int(time.mktime(self.start.timetuple()))
day_end = self.end.replace(hour=23, minute=59, second=59)
unix_end = int(time.mktime(day_end.timetuple()))
url = 'https://finance.yahoo.com/quote/{}/history?'
url += 'period1={}&period2={}'
url += '&filter=history'
url += '&interval=1d'
url += '&frequency=1d'
self.url = url.format(self.symbol, unix_start, unix_end)
def read(self):
import requests, re, json
r = requests.get(self.url)
ptrn = r'root\.App\.main = (.*?);\n}\(this\)\);'
txt = re.search(ptrn, r.text, re.DOTALL).group(1)
jsn = json.loads(txt)
df = pd.DataFrame(
jsn['context']['dispatcher']['stores']
['HistoricalPriceStore']['prices']
)
df.insert(0, 'symbol', self.symbol)
df['date'] = pd.to_datetime(df['date'], unit='s').dt.date
# drop rows that aren't prices
df = df.dropna(subset=['close'])
df = df[['symbol', 'date', 'high', 'low', 'open', 'close',
'volume', 'adjclose']]
df = df.set_index('symbol')
return df
ydr = YahooDailyReader('IBM')
df = ydr.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment