Fundamental factor screening — filter stocks by PE/PB/ROE, financial statement fields, and other metrics for value or growth selection. Supports A-shares (via t
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-fundamental-filter-b11226a34699 ,按照其中的说明把「fundamental-filter」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Filter stocks using fundamental financial data (PE/PB/ROE, etc.) to build value or growth screen signals for backtesting. Supports multiple markets with different data sources.
| Market | Data Source | Method | Supported Metrics |
|---|---|---|---|
| A-shares | tushare daily_basic | extra_fields in config.json | pe, pb, pe_ttm, ps_ttm, dv_ttm, total_mv, circ_mv, roe |
| A-shares | Tushare statements | fundamental_fields in config.json | income, balancesheet, cashflow, fina_indicator fields |
| US stocks | yfinance Ticker.info | Direct API call | trailingPE, forwardPE, priceToBook, returnOnEquity, marketCap, dividendYield |
| HK stocks | yfinance Ticker.info | Direct API call | trailingPE, priceToBook, returnOnEquity, marketCap |
{
"source": "tushare",
"codes": ["000001.SZ", "600036.SH", "000858.SZ"],
"start_date": "2023-01-01",
"end_date": "2024-12-31",
"extra_fields": ["pe", "pb", "pe_ttm", "roe", "total_mv"],
"initial_cash": 1000000,
"commission": 0.001
}
The extra_fields columns are automatically merged into the daily DataFrame by the DataLoader.
Use fundamental_fields when the strategy needs PIT-safe financial statement data instead of daily valuation fields:
{
"source": "tushare",
"codes": ["000001.SZ", "600036.SH", "000858.SZ"],
"start_date": "2023-01-01",
"end_date": "2024-12-31",
"fundamental_fields": {
"income": ["total_revenue", "n_income"],
"balancesheet": ["total_hldr_eqy_exc_min_int"],
"fina_indicator": ["roe", "debt_to_assets"]
},
"initial_cash": 1000000,
"commission": 0.001
}
The backtest runner queries the configured tables through TushareFundamentalProvider and merges each published statement snapshot into daily bars only after its announcement/disclosure date. Statement columns are prefixed by table name:
| Requested field | SignalEngine column |
|---|---|
income.total_revenue | income_total_revenue |
income.n_income | income_n_income |
balancesheet.total_hldr_eqy_exc_min_int | balancesheet_total_hldr_eqy_exc_min_int |
fina_indicator.roe | fina_indicator_roe |
Representative financial-quality pre-filter:
revenue = row.get("income_total_revenue")
profit = row.get("income_n_income")
net_assets = row.get("balancesheet_total_hldr_eqy_exc_min_int")
roe = row.get("fina_indicator_roe")
passes = (
revenue is not None and revenue > 0
and profit is not None and profit > 0
and net_assets is not None and net_assets > 0
and roe is not None and roe >= 8.0
)
For HK/US stocks, fundamental data is not available as daily time-series via the backtest loader. Instead, use yfinance Ticker info for point-in-time screening:
import yfinance as yf
def screen_us_stocks(tickers, criteria):
"""Screen US/HK stocks by fundamental criteria."""
passed = []
for symbol in tickers:
info = yf.Ticker(symbol).info
pe = info.get("trailingPE")
pb = info.get("priceToBook")
roe = info.get("returnOnEquity") # Decimal (e.g., 0.25 = 25%)
mcap = info.get("marketCap")
if pe is None or pb is None or roe is None:
continue # Skip stocks with missing data
if (0 < pe < criteria["pe_max"]
and pb < criteria["pb_max"]
and roe > criteria["roe_min"]
and (mcap or 0) > criteria.get("mcap_min", 0)):
passed.append({
"symbol": symbol,
"pe": pe,
"pb": pb,
"roe": round(roe * 100, 1), # Convert to percentage
"mcap": mcap,
})
return passed
# Example: screen S&P 500 components
criteria = {"pe_max": 20, "pb_max": 3.0, "roe_min": 0.08, "mcap_min": 10_000_000_000}
results = screen_us_stocks(["AAPL", "MSFT", "JNJ", "JPM", "XOM"], criteria)
# HK stocks use the same yfinance interface
hk_tickers = ["0700.HK", "9988.HK", "1810.HK", "2318.HK", "0005.HK"]
results = screen_us_stocks(hk_tickers, criteria) # Same function works
| Parameter | Default | Description |
|---|---|---|
| pe_max | 20.0 | PE ceiling (exclude overvalued) |
| pb_max | 3.0 | PB ceiling |
| roe_min | 8.0 | ROE floor (%), exclude low-profitability |
| pe_min | 0.0 | PE floor (exclude loss-making stocks) |
| mcap_min | 0 | Market cap floor (for US/HK, in USD) |
extra_fields columns may contain NaN (new listings, ST stocks) — must fillna or dropnafundamental_fields columns are prefixed by table and may be NaN before the first statement is published in the backtest windowfundamental_fields is daily-only: an announcement date has no time of day, so an intraday interval is rejected rather than silently making a filing visible from the first bar of its own announcement day. "fundamental_subdaily": "next_day" opts in, with day D's filing visible from the first bar of D+1ann_date / f_ann_date; the runner's merge already enforces point-in-time visibilitype > 0Ticker.info is a point-in-time snapshot, not historical time-series — cannot directly use for daily rebalancing backtests on US/HK stockspip install pandas numpy yfinance
1/N = selected for long (N = number of stocks passing the screen), 0 = not selected