發掘潛力標的必備利器:用 Luckdata Yahu Financials API 搭建多維度財務篩選器
在茫茫股海中,想要從成千上萬檔股票中挑選出最具投資價值的「潛力股」,僅憑肉眼與直覺顯然力有未逮。財務指標篩選器(Financial Screener)正是協助投資人快速鎖定符合特定條件標的的工具。它能自動化地對市盈率、市淨率、營收增速、ROE 等多重指標進行篩選,讓選股從「大海撈針」變成「有的放矢」。
本篇文章將從實際需求出發,運用 Luckdata Yahu Financials API 的 screeners 模組,手把手搭建一款支援自訂多維條件的 財務指標篩選器,並示範如何以 Flask + Bootstrap 將結果呈現在互動式網頁上。
為什麼要用 API 做財務篩選
結構化與即時性
多數券商或財經網站的篩選條件有限,且數據更新時效不一。透過 API,可直接取得即時且結構清晰的 JSON 格式財務數據。高度自訂化
使用者可彈性組合任意條件,如同時篩選「PE < 20」、「ROE > 15%」、「營收成長 > 10%」等,達到精準選股。可延伸至自動化策略
篩選邏輯可整合進量化策略、定時任務或交易訊號流程,打造自動化選股到執行的一體化投資系統。
Luckdata 提供的 screeners 模組,正好為這些場景量身打造。透過以下幾個主要端點,就能實現多維度選股邏輯:
/screeners/get-filters
:取得可用篩選欄位與分類;/screeners/get-symbols-by-predefined
:取得預設條件下的股票清單;/screeners/list-by-ticker
:針對單一股票回查其篩選屬性。
screener 篩選功能與欄位總覽
若要構建一個彈性篩選器,首先必須瞭解能夠篩選哪些欄位。Luckdata 提供 /screeners/get-filters
端點,能依據區域與分類取得所有支援的篩選欄位:
GET https://luckdata.io/api/yahu-financials/vclchw21z2no?region=US&category=keystats,financials,valuation,profitability_ratios_and_dividends&etype=equity
範例回傳內容(節錄):
{"filters": [
{
"id": "trailingPE",
"name": "市盈率 (TTM)",
"type": "number"
},
{
"id": "priceToBook",
"name": "市淨率",
"type": "number"
},
{
"id": "revenueQuarterlyGrowth",
"name": "季度營收年增率 (%)",
"type": "number"
},
{
"id": "returnOnEquity",
"name": "股東權益報酬率 (ROE, %)",
"type": "number"
}
// … 更多欄位 …
]
}
透過這些欄位,你可以為前端使用者動態產生篩選選項,提供靈活的操作介面。
實戰示範:以 PE、ROE、營收增速篩選股票
目前 Luckdata 並未提供多條件自訂 POST 篩選的專屬 API,但可藉由 /screeners/get-symbols-by-predefined
結合特定篩選 ID 做初步查詢,並針對個別股票進一步用 /screeners/list-by-ticker
驗證是否符合細部條件。
步驟一:取得預設熱門標的列表
import requestsAPI_KEY = 'your-luckdata-key'
# MOST_ACTIVES 表示最活躍股票,可替換為其他預設篩選代碼
url = 'https://luckdata.io/api/yahu-financials/ke6gzl1mrinc'
params = {
'count': 100,
'start': 0,
'scrIds': 'MOST_ACTIVES'
}
headers = {'X-Luckdata-Api-Key': API_KEY}
response = requests.get(url, headers=headers, params=params)
tickers = [item['symbol'] for item in response.json().get('finance', {}).get('result', [])[0].get('quotes', [])]
步驟二:針對每檔個股查詢其財務欄位資料
qualified = []for ticker in tickers:
detail_url = 'https://luckdata.io/api/yahu-financials/5xbac871zs11'
params = {'ticker': ticker}
resp = requests.get(detail_url, headers=headers, params=params)
data = resp.json()
try:
pe = data['trailingPE']
roe = data['returnOnEquity']
rev_growth = data['revenueQuarterlyGrowth']
if pe < 20 and roe > 15 and rev_growth > 10:
qualified.append({
'symbol': ticker,
'PE': pe,
'ROE': roe,
'RevenueGrowth': rev_growth
})
except KeyError:
continue
for stock in qualified:
print(f"{stock['symbol']}: PE={stock['PE']}, ROE={stock['ROE']}%, 營收增速={stock['RevenueGrowth']}%")
建立簡易前端:Flask + Bootstrap 呈現篩選結果
為了提升互動性,我們用 Flask 製作一個表單,讓使用者自訂條件並查看結果。
Flask 後端範例程式碼:
from flask import Flask, render_template, requestimport requests
app = Flask(__name__)
API_KEY = 'your-luckdata-key'
@app.route('/', methods=['GET', 'POST'])
def index():
results = []
if request.method == 'POST':
pe = float(request.form.get('pe'))
roe = float(request.form.get('roe'))
rev = float(request.form.get('rev'))
ticker_resp = requests.get(
'https://luckdata.io/api/yahu-financials/ke6gzl1mrinc',
headers={'X-Luckdata-Api-Key': API_KEY},
params={'scrIds': 'MOST_ACTIVES', 'count': 100, 'start': 0}
)
tickers = [i['symbol'] for i in ticker_resp.json()['finance']['result'][0]['quotes']]
for ticker in tickers:
detail = requests.get(
'https://luckdata.io/api/yahu-financials/5xbac871zs11',
headers={'X-Luckdata-Api-Key': API_KEY},
params={'ticker': ticker}
).json()
try:
if detail['trailingPE'] < pe and detail['returnOnEquity'] > roe and detail['revenueQuarterlyGrowth'] > rev:
results.append({
'symbol': ticker,
'PE': detail['trailingPE'],
'ROE': detail['returnOnEquity'],
'RevenueGrowth': detail['revenueQuarterlyGrowth']
})
except:
continue
return render_template('index.html', results=results)
HTML 模板 templates/index.html
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>財務篩選器範例</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<h1 class="mb-4">多維度財務指標篩選器</h1>
<form method="post" class="row g-3 mb-4">
<div class="col-auto">
<label class="form-label">PE <=</label>
<input name="pe" type="number" step="0.1" value="20" class="form-control">
</div>
<div class="col-auto">
<label class="form-label">ROE >=</label>
<input name="roe" type="number" step="0.1" value="15" class="form-control">
</div>
<div class="col-auto">
<label class="form-label">營收增速 >=</label>
<input name="rev" type="number" step="0.1" value="10" class="form-control">
</div>
<div class="col-auto align-self-end">
<button type="submit" class="btn btn-primary">開始篩選</button>
</div>
</form>
{% if results %}
<table class="table table-striped">
<thead>
<tr>
<th>股票</th><th>PE</th><th>ROE (%)</th><th>營收增速 (%)</th>
</tr>
</thead>
<tbody>
{% for s in results %}
<tr>
<td>{{ s.symbol }}</td>
<td>{{ s.PE }}</td>
<td>{{ s.ROE }}</td>
<td>{{ s.RevenueGrowth }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</body>
</html>
進階思考與優化方向
動態欄位選擇
自動從/screeners/get-filters
載入欄位,產生勾選式或多欄條件篩選 UI,提升互動性與彈性。圖表可視化分析
結合歷史資料,利用 ECharts 或 Recharts 呈現歷年財務指標走勢與比較。自動化任務與推播
透過排程任務每日自動執行篩選,並以 Email、Slack 或 LINE 等方式通知用戶新標的。串接量化策略平台
將篩選結果輸出至回測平台,驗證策略效果與調整篩選邏輯。
小結
從瞭解可用欄位、透過預設條件取得標的,再以單檔查詢篩選結果,本文展示如何利用 Luckdata Yahu Financials API 實作一套完整的 多維度財務指標篩選器。這樣的系統既靈活又具擴展性,能協助投資者高效率地發掘潛力股、優化選股流程。