介绍
# Yahoo Finance CLI
一个 Python CLI 工具,用于使用 yfinance 从 Yahoo Finance 获取全面的股票数据。
## Requirements
- Python 3.11+ - uv(用于内联脚本依赖)
## Installing uv
该脚本需要 `uv` —— 一个极快的 Python 包管理器。检查是否已安装:
```bash uv --version ```
如果未安装,请使用以下方法之一进行安装:
### macOS / Linux ```bash curl -LsSf https://astral.sh/uv/install.sh | sh ```
### macOS (Homebrew) ```bash brew install uv ```
### Windows ```powershell powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex" ```
### pip (any platform) ```bash pip install uv ```
安装完成后,重启终端或运行: ```bash source ~/.bashrc # or ~/.zshrc on macOS ```
## Installation
`yf` 脚本使用 PEP 723 内联脚本元数据 —— 依赖项会在首次运行时自动安装。
```bash # Make executable chmod +x /path/to/skills/yahoo-finance/yf
# Optionally symlink to PATH for global access ln -sf /path/to/skills/yahoo-finance/yf /usr/local/bin/yf ```
首次运行会将依赖项安装到 uv 的缓存中。后续运行将是即时的。
## Commands
### Price (quick check) ```bash yf AAPL # shorthand for price yf price AAPL ```
### Quote (detailed) ```bash yf quote MSFT ```
### Fundamentals ```bash yf fundamentals NVDA ``` 显示:市盈率、每股收益 (EPS)、市值、利润率、ROE/ROA、分析师目标价。
### Earnings ```bash yf earnings TSLA ``` 显示:下一个财报日期、EPS 预估、包含惊喜值的财报历史。
### Company Profile ```bash yf profile GOOGL ``` 显示:板块、行业、员工数、网站、地址、业务描述。
### Dividends ```bash yf dividends KO ``` 显示:股息率/收益率、除息日、派息比率、近期股息历史。
### Analyst Ratings ```bash yf ratings AAPL ``` 显示:买入/持有/卖出分布、平均评级、近期上调/下调情况。
### Options Chain ```bash yf options SPY ``` 显示:接近价内的看涨和看跌期权,包括行权价、买卖价、成交量、持仓量 (OI)、隐含波动率 (IV)。
### History ```bash yf history GOOGL 1mo # 1 month history yf history TSLA 1y # 1 year yf history BTC-USD 5d # 5 days ``` 范围:1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max
### Compare ```bash yf compare AAPL,MSFT,GOOGL yf compare RELIANCE.NS,TCS.NS,INFY.NS ``` 并列对比,包括价格、变动、52 周范围、市值。
### Search ```bash yf search "reliance industries" yf search "bitcoin" yf search "s&p 500 etf" ```
## Symbol Format
- **US stocks:** AAPL, MSFT, GOOGL, TSLA - **Indian NSE:** RELIANCE.NS, TCS.NS, INFY.NS - **Indian BSE:** RELIANCE.BO, TCS.BO - **Crypto:** BTC-USD, ETH-USD - **Forex:** EURUSD=X, GBPUSD=X - **ETFs:** SPY, QQQ, VOO
## Examples
```bash # Quick price check yf AAPL
# Get valuation metrics yf fundamentals NVDA
# Next earnings date + history yf earnings TSLA
# Options chain for SPY yf options SPY
# Compare tech giants yf compare AAPL,MSFT,GOOGL,META,AMZN
# Find Indian stocks yf search "infosys"
# Dividend info for Coca-Cola yf dividends KO
# Analyst ratings for Apple yf ratings AAPL ```
## Troubleshooting
### "command not found: uv" 按照上述说明安装 uv。
### Rate limiting / connection errors Yahoo Finance 可能会对过多的请求进行速率限制。请等待几分钟并重试。
### "No data" for a symbol - 验证代码是否存在:`yf search "company name"` - 某些数据(期权、股息)并非适用于所有证券
## Technical Notes
- 使用 PEP 723 内联脚本元数据管理 uv 依赖 - Rich 库提供彩色的、格式化的表格 - 首次运行会将依赖安装到 uv 缓存中(约 5 秒) - 后续运行即时完成(缓存环境) - 使用回退值优雅地处理 NaN/None 值