从Yahoo Finance下载股票信息的小程序

本帖于 2024-01-08 11:37:32 时间, 由普通用户 slow_quick 编辑

这几天工作不忙闲着玩ChatGPT,请教怎么下载股票信息。

 

 

Stock Return from 12/29/2023 to 1/5/2024
Ticker Start Price End Price Price Return Dividend Return Gross Return
AAPL 192.53 181.91 -5.52%   -5.52%
NVDA 495.22 479.98 -3.08%   -3.08%
PFE 28.79 29.09 1.04%   1.04%
"""Python program to download stock price from Yahoo Finance and calculate return"""

import yfinance as yf
import pandas as pd
import datetime

def get_period_return(tickers: list[str], start_date: datetime.date, end_date: datetime.date) -> pd.DataFrame:
    hist = yf.download(tickers=tickers, start=f'{start_date:%Y-%m-%d}', end=f'{end_date:%Y-%m-%d}', actions=True)
    hist.fillna(method='backfill', inplace=True)
    d0 = hist.index[0]
    d1 = hist.index[-1]
    p0 = hist.loc[d0, 'Close']
    p1 = hist.loc[d1, 'Close']
    p_rt = p1/p0 - 1
    dvd = hist.loc[:, ('Dividends', slice(None))].sum()
    dvd.index = dvd.index.get_level_values(level=1)
    dvd_rt = dvd/p0
    g_rt = p_rt + dvd_rt
    p0.name = f'Start Price'
    p1.name = f'End Price'
    p_rt.name = f'Price Return'
    dvd_rt.name = f'Dividend Return'
    g_rt.name = f'Gross Return'
    
    res = pd.concat([p0, p1, p_rt, dvd_rt, g_rt], axis=1)
    res = res.rename_axis('Ticker').reset_index()
    
    res.loc[res['Dividend Return']==0, 'Dividend Return'] = pd.NA
    return res

# Usage
start_date = datetime.date(2023, 12, 29)
end_date = datetime.date(2024, 1, 5)
tickers = ['AAPL', 'PFE', 'NVDA']

period_return = get_period_return(tickers, start_date, end_date)

# do whatever you want with period_return dataframe.  e.g.
period_return.to_clipboard()
# and then paste to spreadsheet

所有跟帖: 

徐速兄,能不能帮我把1月2日的开市价调出来? -hhtt- 给 hhtt 发送悄悄话 hhtt 的博客首页 (9575 bytes) () 01/08/2024 postreply 11:40:56

让我试试。你是要1月2日开市价(Open)不是收市价(Close)? -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (0 bytes) () 01/08/2024 postreply 11:54:00

请检查一下数据对不对 -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (27465 bytes) () 01/08/2024 postreply 12:00:18

真牛! -bobpainting- 给 bobpainting 发送悄悄话 (0 bytes) () 01/08/2024 postreply 12:04:04

不是我牛是ChatGPT牛。。。 -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (13773 bytes) () 01/08/2024 postreply 12:18:40

我只是顺藤摸瓜改了几个地方 -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (0 bytes) () 01/08/2024 postreply 12:21:59

怪不得这么火,功能很强大。你用的很好 -bobpainting- 给 bobpainting 发送悄悄话 (0 bytes) () 01/08/2024 postreply 12:24:34

你也牛!我看不懂程序 -bobpainting- 给 bobpainting 发送悄悄话 (0 bytes) () 01/08/2024 postreply 12:22:48

谢谢! -hhtt- 给 hhtt 发送悄悄话 hhtt 的博客首页 (0 bytes) () 01/08/2024 postreply 12:07:08

Start Price 是 1月2日的开市价(Open),End Price 是收市价 -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (0 bytes) () 01/08/2024 postreply 12:08:42

谢谢!新年开市价和年底收市价,就是我要的。 -hhtt- 给 hhtt 发送悄悄话 hhtt 的博客首页 (0 bytes) () 01/08/2024 postreply 12:20:00

看看这个。。。 -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (657 bytes) () 01/08/2024 postreply 13:11:01

借不热门的电脑坛测试 html -slow_quick- 给 slow_quick 发送悄悄话 slow_quick 的博客首页 (0 bytes) () 01/08/2024 postreply 13:13:18

厉害! -吃货99- 给 吃货99 发送悄悄话 (0 bytes) () 01/08/2024 postreply 12:26:13

比特币关联的跌不少,去年涨多了? -hz82000- 给 hz82000 发送悄悄话 hz82000 的博客首页 (0 bytes) () 01/08/2024 postreply 13:16:38

-gccard- 给 gccard 发送悄悄话 (0 bytes) () 01/08/2024 postreply 14:27:49

需要那么复杂吗?看不到作用何在除了 mental exercise。NVDA 今天breakout RSI 沒有跟上 -goingplaces- 给 goingplaces 发送悄悄话 (625 bytes) () 01/08/2024 postreply 14:53:05

mstr 今天怎么了?比特币是涨的 -hz82000- 给 hz82000 发送悄悄话 hz82000 的博客首页 (0 bytes) () 01/08/2024 postreply 15:15:35

请您先登陆,再发跟帖!