介绍
# Yahoo Finance CLI
一个用于使用 `yahoo-finance2` 库从 Yahoo Finance 获取全面股票数据的 Node.js CLI 工具。
## Requirements(要求)
- Node.js - 全局安装 `yahoo-finance2` 或作为 `yf` 可用 - `jq`
## Install(安装)
```bash brew install jq npm install yahoo-finance2 sudo ln -s /opt/homebrew/bin/yahoo-finance /usr/local/bin/yf ```
## Usage(用法)
该工具作为 `yf` 可用。它输出 JSON,可以管道传给 `jq` 进行过滤。
```bash yf <module> <symbol> [queryOptions] ```
## Modules(模块)
### Quote (Real-time Price & Data)(报价:实时价格与数据) 获取实时价格、变动和基本数据。 ```bash yf quote AAPL yf quote AAPL | jq '.regularMarketPrice' ```
### Quote Summary (Fundamentals & More)(报价摘要:基本面与更多) 获取详细模块,如收益、财务数据和概况。 ```bash # Get specific sub-modules yf quoteSummary AAPL '{"modules":["assetProfile", "financialData", "defaultKeyStatistics"]}'
# Common modules to request: # - assetProfile (Company info, sector) # - financialData (Target price, margins, cash) # - defaultKeyStatistics (Enterprise value, float, shares) # - calendarEvents (Earnings dates) # - earnings (History and trend) # - recommendationTrend (Analyst ratings) # - upgradeDowngradeHistory ```
### Insights(洞察) 获取技术和基本面洞察(估值、前景)。 ```bash yf insights AAPL ```
### Search(搜索) 搜索股票代码。 ```bash yf search "Apple" yf search "BTC-USD" ```
### Historical Data (Deprecated)(历史数据:已弃用) 获取历史 OHLCV 数据。注意:`historical` 已被弃用;请改用 `chart`。 ```bash # Deprecated - use chart instead yf historical AAPL '{"period1":"2024-01-01","period2":"2024-12-31"}'
# Recommended: use chart yf chart AAPL '{"period1":"2024-01-01","period2":"2024-12-31"}' ```
### Trending(趋势) 查看当前趋势。 ```bash yf trendingSymbols US ```
## Examples(示例)
**Quick Price Check(快速价格查询)** ```bash # Full JSON then filter with jq yf quote NVDA | jq '{symbol: .symbol, price: .regularMarketPrice, changePct: .regularMarketChangePercent}' ```
**Next Earnings Date(下一个财报日期)** ```bash # Use single quotes around the JSON option in zsh/bash yf quoteSummary TSLA '{"modules":["calendarEvents"]}' | jq '.calendarEvents.earnings.earningsDate' ```
**Analyst Recommendations(分析师评级)** ```bash yf quoteSummary AAPL '{"modules":["recommendationTrend"]}' ```
**Company Profile(公司概况)** ```bash yf quoteSummary MSFT '{"modules":["assetProfile"]}' ```
**Historical OHLCV(历史 OHLCV)** ```bash # Using chart (recommended) yf chart AAPL '{"period1":"2024-01-01","period2":"2024-12-31","interval":"1d"}' | jq '.quotes[0:5]'
# Using historical (deprecated, but still works) yf historical AAPL '{"period1":"2024-01-01","period2":"2024-12-31","interval":"1d"}' | jq '.[0:5]' ```
**Search for Symbols(搜索股票代码)** ```bash yf search 'Apple' yf search 'BTC-USD' ```
**Trending Symbols (US)(趋势股票代码:美国)** ```bash yf trendingSymbols US ```
**Insights (valuation, outlook)(洞察:估值、前景)** ```bash yf insights AAPL ```
## Troubleshooting(故障排除)
- **Cookies:** 该工具自动处理 cookies(存储在 `~/.yf2-cookies.json` 中)。如果遇到问题,请尝试删除此文件。 - **JSON Output:** 输出是纯 JSON。使用 `jq` 进行解析,以便用于脚本或提高可读性。
Additional tips(额外提示): - 如果您看到身份验证或解析错误,请删除 cookie 文件并重试:
```bash rm -f ~/.yf2-cookies.json yf quote AAPL ```
- 在使用 zsh 的 macOS 上,JSON 选项参数周围首选使用单引号,并在内部使用双引号(参见上面的示例)。 - 如果你只想要一个紧凑的数值(不用 jq),可以使用一个简短的 jq 过滤器,例如:
```bash yf quote AAPL | jq -r '.regularMarketPrice' ```