yfinance-data
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseyfinance Data Skill
yfinance 数据技能
Fetches financial and market data from Yahoo Finance using the yfinance Python library.
Important: yfinance is not affiliated with Yahoo, Inc. Data is for research and educational purposes.
Step 1: Ensure yfinance Is Available
步骤1:确保yfinance可用
Before running any code, install yfinance if needed:
python
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "yfinance"])Always include this at the top of your script.
运行任何代码前,若需要请安装yfinance:
python
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "yfinance"])请始终将此代码放在脚本顶部。
Step 2: Identify What the User Needs
步骤2:明确用户需求
Match the user's request to one or more data categories below, then use the corresponding code from .
references/api_reference.md| User Request | Data Category | Primary Method |
|---|---|---|
| Stock price, quote | Current price | |
| Price history, chart data | Historical OHLCV | |
| Balance sheet | Financial statements | |
| Income statement, revenue | Financial statements | |
| Cash flow | Financial statements | |
| Dividends | Corporate actions | |
| Stock splits | Corporate actions | |
| Options chain, calls, puts | Options data | |
| Earnings, EPS | Analysis | |
| Analyst price targets | Analysis | |
| Recommendations, ratings | Analysis | |
| Upgrades/downgrades | Analysis | |
| Institutional holders | Ownership | |
| Insider transactions | Ownership | |
| Company overview, sector | General info | |
| Compare multiple stocks | Bulk download | |
| Screen/filter stocks | Screener | |
| Sector/industry data | Market data | |
| News | News | |
将用户的请求与以下一个或多个数据类别匹配,然后使用中的对应代码。
references/api_reference.md| 用户请求 | 数据类别 | 主要方法 |
|---|---|---|
| 股票价格、报价 | 当前价格 | |
| 价格历史、图表数据 | 历史OHLCV数据 | |
| 资产负债表 | 财务报表 | |
| 利润表、营收 | 财务报表 | |
| 现金流量表 | 财务报表 | |
| 股息 | 公司行动 | |
| 股票拆分 | 公司行动 | |
| 期权链、认购期权、认沽期权 | 期权数据 | |
| 收益、每股收益(EPS) | 分析数据 | |
| 分析师目标价 | 分析数据 | |
| 建议、评级 | 分析数据 | |
| 评级上调/下调 | 分析数据 | |
| 机构持股人 | 持股情况 | |
| 内部人交易 | 持股情况 | |
| 公司概况、行业板块 | 基本信息 | |
| 多只股票对比 | 批量下载 | |
| 股票筛选 | 筛选工具 | |
| 板块/行业数据 | 市场数据 | |
| 新闻 | 新闻资讯 | |
Step 3: Write and Execute the Code
步骤3:编写并执行代码
General pattern
通用模式
python
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "yfinance"])
import yfinance as yf
ticker = yf.Ticker("AAPL")python
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "yfinance"])
import yfinance as yf
ticker = yf.Ticker("AAPL")... use the appropriate method from the reference
... 使用参考文档中的合适方法
undefinedundefinedKey rules
关键规则
- Always wrap in try/except — Yahoo Finance may rate-limit or return empty data
- Use for multi-ticker comparisons — it's faster with multi-threading
yf.download() - For options, list expiration dates first with before calling
ticker.optionsticker.option_chain(date) - For quarterly data, use prefix:
quarterly_,ticker.quarterly_income_stmt,ticker.quarterly_balance_sheetticker.quarterly_cashflow - For large date ranges, be mindful of intraday limits — 1m data only goes back ~7 days, 1h data ~730 days
- Print DataFrames clearly — use or
.to_string()for readability, or select key columns.to_markdown()
- 始终使用try/except包裹代码——Yahoo Finance可能会限制请求频率或返回空数据
- 多股票对比使用——它支持多线程,速度更快
yf.download() - 获取期权数据时,先通过列出到期日期,再调用
ticker.optionsticker.option_chain(date) - 获取季度数据,使用前缀:
quarterly_、ticker.quarterly_income_stmt、ticker.quarterly_balance_sheetticker.quarterly_cashflow - 处理大日期范围时,注意日内数据限制——1分钟数据仅可追溯约7天,1小时数据约730天
- 清晰打印DataFrame——使用或
.to_string()提升可读性,或选择关键列展示.to_markdown()
Valid periods and intervals
有效周期与时间间隔
| Periods | |
|---|---|
| Intervals | |
| 周期 | |
|---|---|
| 时间间隔 | |
Step 4: Present the Data
步骤4:呈现数据
After fetching data, present it clearly:
- Summarize key numbers in a brief text response (current price, market cap, P/E, etc.)
- Show tabular data formatted for readability — use markdown tables or formatted DataFrames
- Highlight notable items — earnings beats/misses, unusual volume, dividend changes
- Provide context — compare to sector averages, historical ranges, or analyst consensus when relevant
If the user seems to want a chart or visualization, combine with an appropriate visualization approach (e.g., generate an HTML chart or describe the trend).
获取数据后,需清晰呈现:
- 用简短文字总结关键数据(当前价格、市值、市盈率等)
- 以可读格式展示表格数据——使用Markdown表格或格式化后的DataFrame
- 突出重要信息——收益超预期/未达预期、异常成交量、股息变化等
- 提供上下文信息——必要时与行业平均水平、历史区间或分析师共识进行对比
若用户需要图表或可视化,可结合合适的可视化方法(例如生成HTML图表或描述趋势)。
Reference Files
参考文件
- — Complete yfinance API reference with code examples for every data category
references/api_reference.md
Read the reference file when you need exact method signatures or edge case handling.
- —— 完整的yfinance API参考文档,包含每个数据类别的代码示例
references/api_reference.md
当你需要准确的方法签名或处理边缘情况时,请查阅此参考文件。