Institutional Flows · 2026-04-24 · 11 min read
三大法人買賣超 API:用外資、投信、自營商資料做台股籌碼分析
三大法人買賣超是台股市場很有特色的籌碼資料。對 developer、quant 和 AI agent workflow 來說,重點不只是看到外資、投信、自營商買超或賣超,而是要把資料整理成穩定 schema,處理市場別、日期、單位、買賣方向、成交量對齊與 rolling features,才能接進回測、dashboard 或自動化股票研究流程。
TL;DR
三大法人買賣超 API 應該清楚提供外資、投信、自營商的買進、賣出、買賣超、單位、日期、市場別與股票代號。對量化交易來說,單日買賣超通常不夠,還需要連續買超天數、買賣超佔成交量比重、rolling sum、rolling z-score、法人分歧程度與成交量對齊。
籌碼資料可以作為台股量化因子或 AI agent 的研究資料,但不應該被直接解讀成買賣建議。資料產品的核心是提供一致、可回測、可監控的 institutional flow data,而不是宣稱法人買超就一定會上漲。
什麼是三大法人買賣超?
台股常說的三大法人,通常指外資、投信與自營商。買賣超資料描述的是特定法人類別在某個交易日對特定股票的買進、賣出與淨買賣狀況。
常見分類包括:
- 外資及陸資
- 投信
- 自營商
- 自營商自行買賣
- 自營商避險
- 三大法人合計
不同資料來源可能會有不同細分欄位,因此 API schema 應該明確標示資料口徑,而不是只回傳一個模糊的 `institutional_net_buy`。
為什麼法人籌碼資料對台股 API 重要?
如果台股 financial data API 只提供股價和財報,就會少掉一種台股使用者非常常見的資料層:籌碼資料。三大法人買賣超常被用來觀察資金流向、機構投資人行為與市場結構變化。
法人籌碼資料可以支援:
- 籌碼 dashboard
- 每日法人買賣超追蹤
- 量化因子研究
- 外資 / 投信連續買超分析
- 買賣超佔成交量比重
- sector flow analysis
- AI agent 股票研究摘要
- 風險標記與異常資金流提醒
如果你還在理解台股 API 的整體資料分類,可以先看 台股 API 完整指南。
三大法人資料有哪些欄位?
一個實用的三大法人 API 不應該只回傳單一數字。至少應包含日期、股票代號、市場別、法人類別、買進、賣出、買賣超與單位。
| 欄位 | 說明 | 注意事項 |
|---|---|---|
| date | 交易日期 | 建議使用 ISO 8601,例如 2026-04-24 |
| symbol | 股票代號 | 需和其他行情 / 財報 endpoint 一致 |
| market | 市場別,例如 twse、tpex | 上市與上櫃資料來源與口徑要區分 |
| investor_type | 法人類別 | foreign、investment_trust、dealer 等值應固定 |
| buy | 買進數量或金額 | 必須標示單位 |
| sell | 賣出數量或金額 | 必須標示單位 |
| net_buy | 買賣超,通常為買進減賣出 | 正值代表買超,負值代表賣超 |
| unit | 資料單位 | shares、lots、TWD 等不能混淆 |
| source | 資料來源或資料供應標記 | 方便 audit 與資料追蹤 |
買進、賣出、買賣超與單位設計
三大法人資料最容易出錯的地方是單位。使用者可能想看的是張數、股數或金額。如果 API 沒有清楚標示單位,後續做因子、圖表或回測時會非常容易出錯。
常見資料單位
| 單位 | 說明 | 適合用途 |
|---|---|---|
| shares | 股數 | 和成交量直接對齊 |
| lots | 張數 | 台股使用者介面常見顯示方式 |
| TWD | 新台幣金額 | 買賣金額、資金流向、跨股票比較 |
建議 API 可以提供明確參數讓使用者選擇:
GET /v1/tw/stocks/2330/institutional-flows?from=2025-01-01&to=2025-12-31&unit=shares
GET /v1/tw/stocks/2330/institutional-flows?from=2025-01-01&to=2025-12-31&unit=lots
GET /v1/tw/stocks/2330/institutional-flows?from=2025-01-01&to=2025-12-31&unit=value上方 endpoint 是示意。實際 API host 與路徑請以 TW Market Data docs 為準。
三大法人 API response schema
三大法人資料可以用長表格式表示。這種格式比把所有法人欄位攤成很多 column 更容易擴充,也更適合 database、ETL 與 AI agent tool calling。
Long format response
{
"data": [
{
"date": "2026-04-24",
"symbol": "2330",
"market": "twse",
"investor_type": "foreign",
"buy": 12500000,
"sell": 9800000,
"net_buy": 2700000,
"unit": "shares",
"currency": "TWD"
},
{
"date": "2026-04-24",
"symbol": "2330",
"market": "twse",
"investor_type": "investment_trust",
"buy": 860000,
"sell": 420000,
"net_buy": 440000,
"unit": "shares",
"currency": "TWD"
},
{
"date": "2026-04-24",
"symbol": "2330",
"market": "twse",
"investor_type": "dealer",
"buy": 520000,
"sell": 610000,
"net_buy": -90000,
"unit": "shares",
"currency": "TWD"
}
],
"meta": {
"source": "tw-market-data",
"from": "2026-04-24",
"to": "2026-04-24",
"unit": "shares",
"timezone": "Asia/Taipei"
}
}Wide format response
如果使用者主要是做 pandas 分析,也可以提供 wide format。這種格式適合直接和 OHLCV 合併。
{
"data": [
{
"date": "2026-04-24",
"symbol": "2330",
"market": "twse",
"foreign_net_buy": 2700000,
"investment_trust_net_buy": 440000,
"dealer_net_buy": -90000,
"total_institutional_net_buy": 3050000,
"unit": "shares"
}
],
"meta": {
"format": "wide",
"unit": "shares"
}
}Long format 比較彈性,wide format 比較方便。資料產品可以兩者都支援,或在 API 參數中讓使用者選擇。
如何把買賣超轉成量化因子?
單日買賣超通常不適合直接使用。比較常見的做法是把它轉成 rolling feature 或 normalized feature。
| 因子 | 公式概念 | 用途 | 注意事項 |
|---|---|---|---|
| 單日買賣超 | net_buy | 每日籌碼變化 | 容易受單日事件影響 |
| 連續買超天數 | net_buy > 0 的連續天數 | 資金流入持續性 | 不代表價格一定上漲 |
| 買賣超佔成交量比重 | net_buy / volume | 衡量法人買賣對成交量的相對影響 | 成交量單位必須一致 |
| rolling net buy | 近 N 日 net_buy 加總 | 中短期資金流向 | N 的選擇需要回測驗證 |
| rolling z-score | net_buy 與過去平均 / 標準差比較 | 偵測異常資金流 | 低成交量股票容易產生極端值 |
| institutional disagreement | 外資、投信、自營商方向是否分歧 | 衡量法人意見一致性 | 不同法人行為含義不同 |
Factor schema 示意
{
"date": "2026-04-24",
"symbol": "2330",
"factor_name": "foreign_net_buy_volume_ratio_5d",
"value": 0.036,
"window": 5,
"data_used": [
"institutional_flows",
"daily_ohlcv"
],
"unit": "ratio"
}如果你要把籌碼因子放進量化策略,可以參考 台股量化交易入門。
若要接進回測系統,可以參考 Python 台股回測系統實作。
如何和成交量、股價與財報資料對齊?
三大法人買賣超資料通常不應該單獨使用。它更常和 OHLCV、成交量、產業分類與基本面資料一起使用。
和成交量對齊
如果要計算買賣超佔成交量比重,必須確認兩邊單位一致。例如 institutional flow 是股數,成交量也應該是股數,而不是張數或金額。
和股價對齊
如果要分析法人買超後的價格行為,必須小心避免 look-ahead bias。當日買賣超資料若在盤後才完整取得,回測時不能用它決定同日收盤前的交易。
Python 串接三大法人 API 範例
以下示範如何用 Python 呼叫三大法人買賣超 API,轉成 pandas DataFrame,並和 OHLCV 資料合併。
import os
import requests
import pandas as pd
API_KEY = os.getenv("TW_MARKET_DATA_API_KEY")
BASE_URL = "https://api.example.com"
headers = {
"Authorization": f"Bearer {API_KEY}"
}
params = {
"from": "2025-01-01",
"to": "2025-12-31",
"unit": "shares",
"format": "wide"
}
response = requests.get(
f"{BASE_URL}/v1/tw/stocks/2330/institutional-flows",
headers=headers,
params=params,
timeout=20
)
response.raise_for_status()
payload = response.json()
flows = pd.DataFrame(payload["data"])
flows["date"] = pd.to_datetime(flows["date"])
flows = flows.sort_values("date").reset_index(drop=True)
print(flows.head())上方 endpoint 是示意。實際 API host 與路徑請以 TW Market Data docs 為準。
建立籌碼特徵
flows["foreign_net_buy_5d"] = flows["foreign_net_buy"].rolling(5).sum()
flows["investment_trust_net_buy_5d"] = flows["investment_trust_net_buy"].rolling(5).sum()
flows["dealer_net_buy_5d"] = flows["dealer_net_buy"].rolling(5).sum()
flows["total_institutional_net_buy_5d"] = (
flows["foreign_net_buy_5d"]
+ flows["investment_trust_net_buy_5d"]
+ flows["dealer_net_buy_5d"]
)
print(flows[[
"date",
"foreign_net_buy_5d",
"investment_trust_net_buy_5d",
"dealer_net_buy_5d",
"total_institutional_net_buy_5d"
]].tail())和 OHLCV 合併
ohlcv = pd.DataFrame([
{
"date": "2025-01-02",
"symbol": "2330",
"close": 1005,
"volume": 25000000
}
])
ohlcv["date"] = pd.to_datetime(ohlcv["date"])
data = ohlcv.merge(flows, on=["date", "symbol"], how="left")
data["foreign_net_buy_volume_ratio"] = (
data["foreign_net_buy"] / data["volume"]
)
print(data.head())這裡的重點是資料對齊,而不是策略本身。正式回測還需要考慮資料可得時間、交易成本、滑價與風控。
AI agent 如何使用法人籌碼資料?
AI agent 不應該用語言模型內部知識猜法人買賣超。正確做法是透過 tool calling 查詢 institutional flow API,再用 LLM 做摘要、比較與風險標記。
Agent tool schema 示意
{
"name": "get_institutional_flows",
"description": "Get institutional investor buy/sell flows for a Taiwan stock.",
"parameters": {
"symbol": "2330",
"from": "2025-01-01",
"to": "2025-12-31",
"unit": "shares",
"format": "wide"
}
}Agent response schema 示意
{
"symbol": "2330",
"data_used": [
"institutional_flows",
"daily_ohlcv"
],
"summary": "Foreign investors were net buyers over the selected window, while dealer flows were mixed.",
"flow_features": {
"foreign_net_buy_5d": 12000000,
"investment_trust_net_buy_5d": 1800000,
"dealer_net_buy_5d": -600000
},
"risk_flags": [
"requires_volume_normalization",
"not_a_standalone_trading_signal",
"requires_backtesting"
],
"not_investment_advice": true
}這種結構化輸出能讓 agent 清楚標示資料來源、時間區間與限制。對 production workflow 來說,它比一句法人買超所以看多更可靠。
常見資料品質問題
三大法人資料常見問題不在於難以理解,而在於資料口徑、單位與對齊方式容易出錯。
| 問題 | 影響 | API 應提供的協助 |
|---|---|---|
| 單位不清楚 | 張數、股數、金額混用 | 提供 unit 欄位與查詢參數 |
| 市場別混淆 | 上市與上櫃資料口徑混在一起 | 提供 market 欄位 |
| 自營商細分不清楚 | 自行買賣與避險行為被混在一起 | 提供 dealer 子類別或 metadata |
| 和成交量單位不一致 | net_buy / volume 比例錯誤 | 統一 volume 與 flow 單位 |
| 資料可得時間不清楚 | 回測可能使用未來資料 | 提供 updated_at 或 available_at |
| 缺值未標示原因 | 誤以為法人沒有買賣 | 提供 missing reason 或 trading status |
建議的 API endpoint 設計
以下是三大法人與籌碼資料相關 endpoint 的設計示意。
GET /v1/tw/stocks/{symbol}/institutional-flows
GET /v1/tw/stocks/{symbol}/institutional-flows?from=2025-01-01&to=2025-12-31
GET /v1/tw/stocks/{symbol}/institutional-flows?unit=shares
GET /v1/tw/stocks/{symbol}/institutional-flows?format=wide
GET /v1/tw/institutional-flows/rankings?investor_type=foreign&side=buy
GET /v1/tw/institutional-flows/rankings?investor_type=investment_trust&window=5d
GET /v1/tw/stocks/{symbol}/flow-factors實際 endpoint 命名不一定要完全相同。重點是資料單位、法人類別、市場別、日期與 response schema 必須清楚,才能支援量化研究、dashboard 和 AI agent workflow。
上方 endpoint 是示意。實際路徑請以 TW Market Data docs 為準。
FAQ
三大法人買賣超 API 可以取得哪些資料?
通常可以取得外資、投信、自營商的買進、賣出、買賣超資料。更完整的 API 會提供市場別、股票代號、日期、單位、三大法人合計、自營商細分與資料更新時間。
三大法人買超代表股價一定會上漲嗎?
不代表。法人買賣超是籌碼資料,可以作為研究或因子的一部分,但不能單獨視為買賣建議。仍需要搭配股價、成交量、基本面、風險控管與回測驗證。
外資、投信、自營商資料可以直接加總嗎?
可以建立三大法人合計欄位,但要先確認單位一致,並理解不同法人類別的行為含義不同。部分資料來源也會對自營商或外資自營商有不同口徑,API 應以 metadata 清楚說明。
三大法人資料適合做量化因子嗎?
適合,但通常需要轉成 rolling features 或 normalized features,例如連續買超天數、近 5 日買賣超、買賣超佔成交量比重或 rolling z-score。因子是否有效仍需要回測。
Python 可以抓三大法人買賣超資料嗎?
可以。Python 可以透過 requests 呼叫三大法人 API,再用 pandas 轉成 DataFrame,計算 rolling sum、z-score、成交量比例,並接進回測或 dashboard。
AI agent 可以用法人籌碼資料分析股票嗎?
可以。AI agent 可以透過 tool calling 查詢三大法人資料,摘要外資、投信、自營商近期買賣超變化,並標記風險。但 agent 不應該自己猜資料,也不應把分析結果當成投資建議。
下一步
如果你正在建立台股籌碼資料 workflow,建議先處理三件事:
- 1. 明確定義法人類別與資料單位
- 2. 把買賣超和成交量、股價、交易日曆對齊
- 3. 將單日資料轉成可回測的 rolling features
Need structured Taiwan institutional flow data for your quant workflow, dashboard, or AI agent?
本文討論資料工程、API 設計、籌碼資料、量化因子與 AI workflow,不構成投資建議。