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

s
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
h
hhtt
徐速兄,能不能帮我把1月2日的开市价调出来?

股票
AAL
AAPL
ABBV
AMD
ARM
AVGO
BABA
BAC
BMY
COIN
COST
CVE
DVN
EBAY
EDU
ENPH
EXPE
F
FMCC
FTNT
GIS
GOOG
GOOS
GTLB
HD
INTC
JPM
LCID
LLY
LMND
LNG
MARA
META
MRNA
MSFT
MSTR
NEE
NEM
NN
NOK
NVDA
NVO
PBR
PDD
PFE
PLTR
PYPL
REGN
RIOT
SERA
SGRP
SMCI
SNOW
SOFI
SQ
T
TAL
TCEHY
TD
TME
TSLA
TTD
U
UAL
UBER
UBOT
UNH
VEEV
VRTX
VZ
ZG
s
slow_quick
让我试试。你是要1月2日开市价(Open)不是收市价(Close)?
s
slow_quick
请检查一下数据对不对

Ticker Start Price End Price Price Return Dividend Return Gross Return
AAL 13.65 13.09 -4.10% -4.10%
AAPL 187.15 181.91 -2.80% -2.80%
ABBV 154.90 161.46 4.23% 4.23%
AMD 144.28 136.01 -5.73% -5.73%
ARM 73.50 68.39 -6.95% -6.95%
AVGO 1,092.12 1,049.01 -3.95% -3.95%
BABA 76.05 74.66 -1.83% -1.83%
BAC 33.39 33.80 1.23% 1.23%
BMY 51.45 52.04 1.15% 1.17% 2.31%
COIN 173.02 155.60 -10.07% -10.07%
COST 655.58 648.35 -1.10% -1.10%
CVE 16.81 16.40 -2.44% -2.44%
DVN 45.62 45.63 0.02% 0.02%
EBAY 43.26 42.53 -1.69% -1.69%
EDU 72.06 77.08 6.97% 6.97%
ENPH 130.66 118.07 -9.64% -9.64%
EXPE 151.67 144.82 -4.52% -4.52%
F 12.04 11.68 -2.99% -2.99%
FMCC 0.86 0.80 -6.98% -6.98%
FTNT 58.10 58.39 0.50% 0.50%
GIS 65.00 65.74 1.14% 1.14%
GOOG 139.60 138.04 -1.12% -1.12%
GOOS 11.72 11.33 -3.33% -3.33%
GTLB 61.59 57.58 -6.51% -6.51%
HD 344.21 338.59 -1.63% -1.63%
INTC 49.20 46.87 -4.74% -4.74%
JPM 169.09 171.41 1.37% 0.62% 1.99%
LCID 4.17 3.89 -6.71% -6.71%
LLY 580.41 614.50 5.87% 5.87%
LMND 16.00 16.61 3.81% 3.81%
LNG 170.70 168.86 -1.08% -1.08%
MARA 26.64 26.27 -1.39% -1.39%
META 351.32 347.12 -1.20% -1.20%
MRNA 99.88 108.66 8.79% 8.79%
MSFT 373.86 367.94 -1.58% -1.58%
MSTR 692.49 655.81 -5.30% -5.30%
NEE 60.55 61.80 2.06% 2.06%
NEM 41.26 40.12 -2.76% -2.76%
NN 4.42 4.52 2.26% 2.26%
NOK 3.39 3.45 1.77% 1.77%
NVDA 492.44 479.98 -2.53% -2.53%
NVO 101.71 107.63 5.82% 5.82%
PBR 16.09 16.35 1.62% 1.62%
PDD 143.88 147.74 2.68% 2.68%
PFE 28.88 29.09 0.73% 0.73%
PLTR 16.95 16.25 -4.13% -4.13%
PYPL 61.22 58.45 -4.52% -4.52%
REGN 873.91 912.17 4.38% 4.38%
RIOT 17.07 15.86 -7.09% -7.09%
SERA 5.81 5.88 1.20% 1.20%
SGRP 1.01 1.02 0.99% 0.99%
SMCI 280.00 288.80 3.14% 3.14%
SNOW 195.00 183.72 -5.78% -5.78%
SOFI 9.71 8.42 -13.29% -13.29%
SQ 75.50 68.15 -9.74% -9.74%
T 16.84 17.15 1.84% 1.84%
TAL 12.54 12.59 0.40% 0.40%
TCEHY 37.59 37.82 0.61% 0.61%
TD 64.04 64.10 0.09% 0.09%
TME 8.82 9.03 2.32% 2.32%
TSLA 250.08 237.93 -4.86% -4.86%
TTD 71.65 68.40 -4.54% -4.54%
U 39.98 37.05 -7.33% -7.33%
UAL 41.13 40.47 -1.60% -1.60%
UBER 60.73 57.80 -4.82% -4.82%
UBOT 20.19 18.30 -9.36% -9.36%
UNH 526.84 545.42 3.53% 3.53%
VEEV 191.16 186.11 -2.64% -2.64%
VRTX 405.00 416.15 2.75% 2.75%
VZ 37.73 39.37 4.35% 4.35%
ZG 55.73 52.75 -5.35% -5.35%
b
bobpainting
真牛!
h
hhtt
谢谢!
s
slow_quick
Start Price 是 1月2日的开市价(Open),End Price 是收市价
s
slow_quick
不是我牛是ChatGPT牛。。。

问:how to get company name and sector for a stock using yfinance

答:

To get the company name and sector for a stock using the yfinance library in Python, you can follow these steps:

1. Install yfinance if you haven't already: pip install yfinance   2. Use the following Python code to get the company name and sector:

 

import yfinance as yf def get_company_info(ticker_symbol): # Create a Ticker object for the given stock symbol ticker = yf.Ticker(ticker_symbol) try: # Get the info dictionary for the stock info = ticker.info # Extract company name and sector from the info dictionary company_name = info['longName'] sector = info['sector'] return company_name, sector except Exception as e: print(f"Error: Unable to retrieve information for {ticker_symbol}.") print(f"Error details: {e}") return None, None # Example: Get information for Apple Inc. (AAPL) ticker_symbol = 'AAPL' company_name, sector = get_company_info(ticker_symbol) if company_name and sector: print(f"Company Name: {company_name}") print(f"Sector: {sector}")
h
hhtt
谢谢!新年开市价和年底收市价,就是我要的。
s
slow_quick
我只是顺藤摸瓜改了几个地方
b
bobpainting
你也牛!我看不懂程序
b
bobpainting
怪不得这么火,功能很强大。你用的很好
吃货99
厉害!
s
slow_quick
看看这个。。。

https://bbs.wenxuecity.com/bbs/computer/257550.html

"行业"是 Yahoo Finance "Industry" 数据,我也可以直接 Copy 你的

我还在学习,哪天退休了用不起Bloomberg就用Yahoo Finance

如果谁要程序我也可以传上来,就是好玩而已

s
slow_quick
借不热门的电脑坛测试 html
h
hz82000
比特币关联的跌不少,去年涨多了?
g
gccard
g
goingplaces
需要那么复杂吗?看不到作用何在除了 mental exercise。NVDA 今天breakout RSI 沒有跟上

所有历史数据 Yahoo finance 都有。每天的 Open 和 Close.

你如果真想弄些有用的 搞一个程序 monitor 每天的 New Highs New Lows, stock above 200, 50 dma, stocks below 209, 50 dma.  看这些数据和市场表现的关系。懂些股市的人都会明白。难的是 quantify 这种关系 down to a number reading.

历史 back tested, 这个模型可以 catch the near market lows within 5% range.  也就是说 downside is mitigated,  对市场頂 不大行。原因在我以前的贴子有解释过数次 the why (市场心理学哈)

h
hz82000
mstr 今天怎么了?比特币是涨的