stocks splitting api google or yahoo
Asked Answered
R

2

7

I am looking for a way to get stock splitting information. Using the yahoo stock API I can get all types of info on any symbol but I don't think I can get the split ratio or even whether it split. Does anyone know of a way of getting this info?

Rancho answered 25/5, 2011 at 5:21 Comment(0)
H
16

This is how the quantmod R package does it. The split information is in the "Dividend Only" CSV:
http://ichart.finance.yahoo.com/x?s=IBM&a=00&b=2&c=1962&d=04&e=25&f=2011&g=v&y=0&z=30000

Hyehyena answered 25/5, 2011 at 11:39 Comment(6)
How are you deriving split info from the "Dividend Only" CSV?Rancho
Nice, thank you, this is exactly what I was looking for! Where can I get info on all the different search criteria's? I found it on third part website's but nothing official from yahoo. Any thoughts?Rancho
gummy-stuff has the most information. I think I found the ichart.finance.yahoo.com/x? URL on the Wealth-Lab forums. I don't believe Yahoo has any official documentation.Hyehyena
Also, where else I can find other yahoo URL's for finance. I've used finance.yahoo.com/d/… and now ichart.finance.yahoo.com/…. Are there other ones? Where can I find this info?Rancho
Sorry, saw your reply after I replied. Thank you.Rancho
is there a way to download all the recent stock splits in one go, rather than stock by stock? thanks!Enormous
G
7

You can do it easily in python 3 with the help of the pandas datareader package. Starting defining a function which will return the split history as a dataframe:

def split_history(stock, date_start, date_end, limit_denominator=1000):
    from decimal import Decimal
    from fractions import Fraction
    from pandas_datareader import data as web
    df = web.DataReader(stock, data_source='yahoo-actions', start=date_start, end=date_end)
    is_split = df['action']=='SPLIT'
    df = df[is_split]
    ratios = []
    for index, row in df.iterrows():
        # Taking the inverse of the row['value'] as it is Yahoo finance convention
        ratio = Fraction(1/Decimal(row['value'])).limit_denominator(limit_denominator)
        ratios.append("{num} for {denom}".\
                            format(num=ratio.numerator, denom=ratio.denominator))
    df['ratio'] = ratios
    return df

Now we can get the splits of Microsoft ('MSFT') as an example:

stock = 'MSFT'
date_start = '1987-01-01'
date_end = '2020-07-22'
split_history(stock, date_start, date_end)
            action  value       ratio
2003-02-18  SPLIT   0.500000    2 for 1
1999-03-29  SPLIT   0.500000    2 for 1
1998-02-23  SPLIT   0.500000    2 for 1
1996-12-09  SPLIT   0.500000    2 for 1
1994-05-23  SPLIT   0.500000    2 for 1
1992-06-15  SPLIT   0.666667    3 for 2
1991-06-27  SPLIT   0.666667    3 for 2
1990-04-16  SPLIT   0.500000    2 for 1
1987-09-21  SPLIT   0.500000    2 for 1

It handles also properly the reverse stock splits:

stock = 'PHM.MC'
split_history(stock, date_start, date_end)
                action  value   ratio
 2020-07-22     SPLIT   12.0    1 for 12

ps: probably there are better ways to input the dates. ps2: also, the limit_denominator is there to avoid wrong roundings. You can extend it in rare split ratio cases.

Gutierrez answered 22/7, 2020 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.