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 ev
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.