Yahoo Finance CLI
A Node.js CLI for fetching comprehensive stock data from Yahoo Finance using the yahoo-finance2 library.
Requirements
- - Node.js
- INLINECODE1 installed globally or available as INLINECODE2
- INLINECODE3
Install
CODEBLOCK0
Usage
The tool is available as yf. It outputs JSON, which can be piped to jq for filtering.
CODEBLOCK1
Modules
Quote (Real-time Price & Data)
Get real-time price, change, and basic data.
CODEBLOCK2
Quote Summary (Fundamentals & More)
Get detailed modules like earnings, financial data, and profiles.
CODEBLOCK3
Insights
Get technical and fundamental insights (valuation, outlook).
CODEBLOCK4
Search
Search for symbols.
CODEBLOCK5
Historical Data (Deprecated)
Get historical OHLCV data. Note:
historical is deprecated; use
chart instead.
CODEBLOCK6
Trending
See what's trending.
CODEBLOCK7
Examples
Quick Price Check
CODEBLOCK8
Next Earnings Date
CODEBLOCK9
Analyst Recommendations
CODEBLOCK10
Company Profile
CODEBLOCK11
Historical OHLCV
CODEBLOCK12
Search for Symbols
CODEBLOCK13
Trending Symbols (US)
CODEBLOCK14
Insights (valuation, outlook)
CODEBLOCK15
Troubleshooting
- - Cookies: The tool automatically handles cookies (stored in
~/.yf2-cookies.json). If you encounter issues, try deleting this file. - JSON Output: The output is pure JSON. Use
jq to parse it for scripts or readability.
Additional tips:
- - If you see authentication or parsing errors, delete the cookie file and retry:
CODEBLOCK16
- - On macOS with zsh, prefer single quotes around JSON option arguments and use double quotes inside (see examples above).
- If you want a compact numeric value only (no jq), use a short jq filter, e.g.:
CODEBLOCK17
Yahoo Finance 命令行工具
一个使用 yahoo-finance2 库从雅虎财经获取全面股票数据的 Node.js 命令行工具。
系统要求
- - Node.js
- 全局安装或作为 yf 可用的 yahoo-finance2
- jq
安装
bash
brew install jq
npm install yahoo-finance2
sudo ln -s /opt/homebrew/bin/yahoo-finance /usr/local/bin/yf
使用方法
该工具以 yf 命令形式提供。输出为 JSON 格式,可通过管道传递给 jq 进行过滤。
bash
yf <模块> <股票代码> [查询选项]
模块
Quote(实时价格与数据)
获取实时价格、涨跌幅及基础数据。
bash
yf quote AAPL
yf quote AAPL | jq .regularMarketPrice
Quote Summary(基本面与更多信息)
获取详细模块,如收益、财务数据和公司简介。
bash
获取特定子模块
yf quoteSummary AAPL {modules:[assetProfile, financialData, defaultKeyStatistics]}
常用请求模块:
- assetProfile(公司信息、行业)
- financialData(目标价、利润率、现金流)
- defaultKeyStatistics(企业价值、流通股、股本)
- calendarEvents(财报日期)
- earnings(历史与趋势)
- recommendationTrend(分析师评级)
- upgradeDowngradeHistory
Insights(洞察)
获取技术和基本面洞察(估值、展望)。
bash
yf insights AAPL
Search(搜索)
搜索股票代码。
bash
yf search Apple
yf search BTC-USD
Historical Data(历史数据,已弃用)
获取历史 OHLCV 数据。注意:historical 已弃用,请改用 chart。
bash
已弃用 - 请改用 chart
yf historical AAPL {period1:2024-01-01,period2:2024-12-31}
推荐:使用 chart
yf chart AAPL {period1:2024-01-01,period2:2024-12-31}
Trending(热门趋势)
查看热门趋势。
bash
yf trendingSymbols US
示例
快速价格查询
bash
输出完整 JSON 后使用 jq 过滤
yf quote NVDA | jq {symbol: .symbol, price: .regularMarketPrice, changePct: .regularMarketChangePercent}
下一个财报日期
bash
在 zsh/bash 中使用单引号包裹 JSON 选项
yf quoteSummary TSLA {modules:[calendarEvents]} | jq .calendarEvents.earnings.earningsDate
分析师推荐
bash
yf quoteSummary AAPL {modules:[recommendationTrend]}
公司简介
bash
yf quoteSummary MSFT {modules:[assetProfile]}
历史 OHLCV 数据
bash
使用 chart(推荐)
yf chart AAPL {period1:2024-01-01,period2:2024-12-31,interval:1d} | jq .quotes[0:5]
使用 historical(已弃用,但仍可用)
yf historical AAPL {period1:2024-01-01,period2:2024-12-31,interval:1d} | jq .[0:5]
搜索股票代码
bash
yf search Apple
yf search BTC-USD
热门股票代码(美国)
bash
yf trendingSymbols US
洞察(估值、展望)
bash
yf insights AAPL
故障排除
- - Cookies: 该工具会自动处理 cookies(存储在 ~/.yf2-cookies.json)。如果遇到问题,请尝试删除此文件。
- JSON 输出: 输出为纯 JSON 格式。使用 jq 进行解析以便脚本处理或提高可读性。
其他提示:
- - 如果遇到身份验证或解析错误,请删除 cookie 文件后重试:
bash
rm -f ~/.yf2-cookies.json
yf quote AAPL
- - 在 macOS 的 zsh 环境下,建议对 JSON 选项参数使用单引号,内部使用双引号(参见上方示例)。
- 如果只需要紧凑的数值(不使用 jq),可以使用简短的 jq 过滤器,例如:
bash
yf quote AAPL | jq -r .regularMarketPrice