yfinance-data

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

yfinance 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.

使用yfinance Python库从Yahoo Finance获取金融与市场数据。
重要提示:yfinance与Yahoo公司无关联。数据仅供研究和教育使用。

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 RequestData CategoryPrimary Method
Stock price, quoteCurrent price
ticker.info
or
ticker.fast_info
Price history, chart dataHistorical OHLCV
ticker.history()
or
yf.download()
Balance sheetFinancial statements
ticker.balance_sheet
Income statement, revenueFinancial statements
ticker.income_stmt
Cash flowFinancial statements
ticker.cashflow
DividendsCorporate actions
ticker.dividends
Stock splitsCorporate actions
ticker.splits
Options chain, calls, putsOptions data
ticker.option_chain()
Earnings, EPSAnalysis
ticker.earnings_history
Analyst price targetsAnalysis
ticker.analyst_price_targets
Recommendations, ratingsAnalysis
ticker.recommendations
Upgrades/downgradesAnalysis
ticker.upgrades_downgrades
Institutional holdersOwnership
ticker.institutional_holders
Insider transactionsOwnership
ticker.insider_transactions
Company overview, sectorGeneral info
ticker.info
Compare multiple stocksBulk download
yf.download()
Screen/filter stocksScreener
yf.Screener
+
yf.EquityQuery
Sector/industry dataMarket data
yf.Sector
/
yf.Industry
NewsNews
ticker.news

将用户的请求与以下一个或多个数据类别匹配,然后使用
references/api_reference.md
中的对应代码。
用户请求数据类别主要方法
股票价格、报价当前价格
ticker.info
ticker.fast_info
价格历史、图表数据历史OHLCV数据
ticker.history()
yf.download()
资产负债表财务报表
ticker.balance_sheet
利润表、营收财务报表
ticker.income_stmt
现金流量表财务报表
ticker.cashflow
股息公司行动
ticker.dividends
股票拆分公司行动
ticker.splits
期权链、认购期权、认沽期权期权数据
ticker.option_chain()
收益、每股收益(EPS)分析数据
ticker.earnings_history
分析师目标价分析数据
ticker.analyst_price_targets
建议、评级分析数据
ticker.recommendations
评级上调/下调分析数据
ticker.upgrades_downgrades
机构持股人持股情况
ticker.institutional_holders
内部人交易持股情况
ticker.insider_transactions
公司概况、行业板块基本信息
ticker.info
多只股票对比批量下载
yf.download()
股票筛选筛选工具
yf.Screener
+
yf.EquityQuery
板块/行业数据市场数据
yf.Sector
/
yf.Industry
新闻新闻资讯
ticker.news

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

... 使用参考文档中的合适方法

undefined
undefined

Key rules

关键规则

  1. Always wrap in try/except — Yahoo Finance may rate-limit or return empty data
  2. Use
    yf.download()
    for multi-ticker comparisons
    — it's faster with multi-threading
  3. For options, list expiration dates first with
    ticker.options
    before calling
    ticker.option_chain(date)
  4. For quarterly data, use
    quarterly_
    prefix:
    ticker.quarterly_income_stmt
    ,
    ticker.quarterly_balance_sheet
    ,
    ticker.quarterly_cashflow
  5. For large date ranges, be mindful of intraday limits — 1m data only goes back ~7 days, 1h data ~730 days
  6. Print DataFrames clearly — use
    .to_string()
    or
    .to_markdown()
    for readability, or select key columns
  1. 始终使用try/except包裹代码——Yahoo Finance可能会限制请求频率或返回空数据
  2. 多股票对比使用
    yf.download()
    ——它支持多线程,速度更快
  3. 获取期权数据时,先通过
    ticker.options
    列出到期日期
    ,再调用
    ticker.option_chain(date)
  4. 获取季度数据,使用
    quarterly_
    前缀:
    ticker.quarterly_income_stmt
    ticker.quarterly_balance_sheet
    ticker.quarterly_cashflow
  5. 处理大日期范围时,注意日内数据限制——1分钟数据仅可追溯约7天,1小时数据约730天
  6. 清晰打印DataFrame——使用
    .to_string()
    .to_markdown()
    提升可读性,或选择关键列展示

Valid periods and intervals

有效周期与时间间隔

Periods
1d
,
5d
,
1mo
,
3mo
,
6mo
,
1y
,
2y
,
5y
,
10y
,
ytd
,
max
Intervals
1m
,
2m
,
5m
,
15m
,
30m
,
60m
,
90m
,
1h
,
1d
,
5d
,
1wk
,
1mo
,
3mo

周期
1d
,
5d
,
1mo
,
3mo
,
6mo
,
1y
,
2y
,
5y
,
10y
,
ytd
,
max
时间间隔
1m
,
2m
,
5m
,
15m
,
30m
,
60m
,
90m
,
1h
,
1d
,
5d
,
1wk
,
1mo
,
3mo

Step 4: Present the Data

步骤4:呈现数据

After fetching data, present it clearly:
  1. Summarize key numbers in a brief text response (current price, market cap, P/E, etc.)
  2. Show tabular data formatted for readability — use markdown tables or formatted DataFrames
  3. Highlight notable items — earnings beats/misses, unusual volume, dividend changes
  4. 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).

获取数据后,需清晰呈现:
  1. 用简短文字总结关键数据(当前价格、市值、市盈率等)
  2. 以可读格式展示表格数据——使用Markdown表格或格式化后的DataFrame
  3. 突出重要信息——收益超预期/未达预期、异常成交量、股息变化等
  4. 提供上下文信息——必要时与行业平均水平、历史区间或分析师共识进行对比
若用户需要图表或可视化,可结合合适的可视化方法(例如生成HTML图表或描述趋势)。

Reference Files

参考文件

  • references/api_reference.md
    — Complete yfinance API reference with code examples for every data category
Read the reference file when you need exact method signatures or edge case handling.
  • references/api_reference.md
    —— 完整的yfinance API参考文档,包含每个数据类别的代码示例
当你需要准确的方法签名或处理边缘情况时,请查阅此参考文件。