股票分割 API 谷歌或雅虎

发布于 2024-11-09 13:29:01 字数 96 浏览 3 评论 0原文

我正在寻找一种获取股票分割信息的方法。使用雅虎股票 API,我可以获取任何交易品种的所有类型的信息,但我认为我无法获取分割比率,甚至无法获取它是否分割。有谁知道获取此信息的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-11-16 13:29:01

这就是 quantmod R 包的方式做到了。分割信息位于“仅股息”CSV 中:
<一href="http://icart.finance.yahoo.com/x?s=IBM&a=00&b=2&c=1962&d=04&e=25&f=2011&g=v&y =0&z=30000 ">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

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

在梵高的星空下 2024-11-16 13:29:01

您可以借助 pandas datareader 包在 python 3 中轻松完成此操作。
开始定义一个函数,该函数将以数据帧的形式返回分割历史记录:

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

现在我们可以以 Microsoft(“MSFT”)的分割为例:

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

它还可以正确处理反向股票分割:

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

ps:可能有更好的方法来输入日期。
ps2:此外,限制分母是为了避免错误的舍入。您可以在罕见的分流比情况下扩展它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文