從Yahoo Finance下載股票信息的小程序

來源: slow_quick 2024-01-08 11:36:29 [] [博客] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (3266 bytes)
本文內容已被 [ slow_quick ] 在 2024-01-08 11:37:32 編輯過。如有問題,請報告版主或論壇管理刪除.

這幾天工作不忙閑著玩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

請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”