get unadjusted stock data

from datetime import datetime
import json
from urllib.parse import urlencode
from urllib.request import urlopen
def gethistory(symbol, start=None, end=None):
    # Return 3 tuples (quotes, dividends and splits) with data from start
    # to end datetimes for the given symbol.
    if start is None:
        start = datetime(1980, 1, 1)
    if end is None:
        end = datetime.now()
    url = 'https://query1.finance.yahoo.com/v7/finance/chart/{}?{}'
    qs = dict(
        period1=int(start.timestamp())
        , period2=int(end.timestamp())
        , interval='1d'
        , indicators='quote'
        , includeTimestamps='true'
        , events='div|split'
    )
    with urlopen(url.format(symbol, urlencode(qs))) as rs:
        result = json.load(rs)['chart']['result'][0]
    # Data for quote fields (o, h, l, c, v) are stored in separate lists
    # with indexes that correspond to a list of timestamps. Iterate over
    # the timestamps list to map quote values, converting the timestamps to
    # datetimes along the way.
    quotes = result['indicators']['unadjquote'][0]
    o = quotes['unadjopen']
    h = quotes['unadjhigh']
    l = quotes['unadjlow']
    c = quotes['unadjclose']
    quotes = result['indicators']['quote'][0]
    v = quotes['volume']
    quotes = tuple((
        datetime.fromtimestamp(tm).date()
        , o[t], h[t], l[t], c[t], v[t]
    ) for t, tm in enumerate(result['timestamp']))
    # Remove duplicate dates
    quotes = tuple(sorted(
        dict((q[0], q) for q in quotes).values()
        , key=lambda q: q[0]
    ))
    # Both dividends and splits are stored under 'events' as objects with
    # timestamps for keys.
    dividends = ()
    splits = ()
    events = result.get('events')
    if events:
        e = events.get('dividends')
        dividends = e and tuple((
            datetime.fromtimestamp(i['date']).date()
            , i.get('amount')
        ) for tm, i in e.items())
        dividends = sorted(dividends, key=lambda d: d[0])
        e = events.get('splits')
        splits = e and tuple((
            datetime.fromtimestamp(i['date']).date()
            , i.get('denominator')
            , i.get('numerator')
        ) for tm, i in e.items())
        splits = sorted(splits, key=lambda s: s[0])
    return quotes, dividends, splits

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source