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
- 全局安装 yahoo-finance2 或作为 yf 可用
- 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