AI Agents · 2026-04-24 · 12 min read
台股 AI Agent Workflow:讓 LLM 串接行情、財報、籌碼與風控資料
台股 AI agent 不應該靠語言模型自己猜股價、財報或三大法人買賣超。合理的 workflow 是讓 LLM 透過 tool calling 查詢可信的台股資料 API,再用結構化 schema 整理資料、標示風險、產生研究摘要,最後交給 risk manager 或 human reviewer 檢查。
TL;DR
台股 AI Agent Workflow 的核心是 LLM + tools + structured output。LLM 負責規劃查詢、摘要資料與產生研究結論;TW Market Data 這類台股 financial data API 負責提供行情、OHLCV、財報、月營收、三大法人、技術指標、ETF 成分股、交易日曆與風控資料。
好的 agent workflow 應該包含 tool schema、data_used、時間區間、missing data、risk_flags、confidence、not_investment_advice 與 audit log。Agent 不應該在沒有資料來源的情況下產生買賣建議,也不應該未經回測、風控與人為審核就直接下單。
為什麼台股 AI agent 需要資料 API?
AI agent 要分析股票,首先需要可信資料。語言模型本身不適合作為金融資料來源,因為它可能不知道最新股價、最新財報、最新法人買賣超,也可能在沒有資料來源時產生看似合理但錯誤的回答。
台股 AI agent 需要資料 API 的原因包括:
- 查詢最新或指定日期的行情資料
- 取得歷史 OHLCV 做技術指標或回測
- 取得財報、月營收與基本面資料
- 取得三大法人買賣超與籌碼資料
- 取得 ETF holdings 與 index constituents
- 取得交易日曆,避免日期對齊錯誤
- 計算風險指標,例如波動率、最大回撤與流動性
- 在回答中標示 data_used 與資料時間區間
如果你還在理解台股 API 的整體資料分類,可以先看 台股 API 完整指南。
LLM 不能直接猜金融資料
LLM 可以協助整理、推理、比較與產生摘要,但不應該直接作為股價、財報或法人籌碼資料來源。金融資料通常具有時間敏感性與高精度需求,錯一個日期、單位或欄位都可能導致錯誤結論。
常見錯誤包括:
- 模型猜測股價或成交量
- 使用過期財報資料
- 把報表期誤認為公告日
- 把外資、投信、自營商口徑混在一起
- 把 ETF holdings 和 index constituents 混用
- 忘記價格是否 adjusted
- 沒有標示資料缺口
- 直接產生 buy / sell 建議
較好的做法是:LLM 只能透過明確工具查資料,並且在每次回答中列出使用了哪些資料。
台股 AI agent 的基本工具
一個台股 AI agent workflow 可以從幾個基本工具開始。這些工具不需要一開始就很複雜,但 schema 必須穩定。
| Tool | 功能 | 主要資料 | 常見使用者 |
|---|---|---|---|
| search_stocks | 查股票代號、名稱、市場別 | 股票基本資料 | 所有 agents |
| get_daily_ohlcv | 查歷史股價與成交量 | OHLCV | Technicals Agent, Risk Manager |
| get_financial_statements | 查財報與基本面資料 | 損益表、資產負債表、現金流量表 | Fundamentals Agent |
| get_monthly_revenue | 查月營收 | 月營收、年增率、月增率 | Fundamentals Agent |
| get_institutional_flows | 查三大法人買賣超 | 外資、投信、自營商 | Institutional Flow Agent |
| get_technical_indicators | 查技術指標 | SMA、RSI、MACD、ATR | Technicals Agent |
| get_etf_holdings | 查 ETF holdings 或 index constituents | 成分股、權重、產業分類 | Universe Agent |
| calculate_risk_metrics | 計算風險指標 | 報酬率、波動率、成交量 | Risk Manager |
如果你要先了解 multi-agent 架構,可以參考 AI Hedge Fund 台股版。若要從 Python 端開始串接資料,建議先看 Python 抓台股資料教學。
Tool calling schema 設計
Tool schema 要讓 agent 清楚知道工具可以做什麼、需要哪些參數、會回傳什麼資料,以及有哪些限制。以下是基本設計原則:
- 工具名稱要清楚,例如 get_daily_ohlcv
- parameters 要明確,例如 symbol、from、to、adjusted
- 回傳資料要有 data 和 meta
- meta 要標示 timezone、currency、unit、adjusted、source
- 錯誤訊息要可讀
- 缺資料要明確標示
- 不要讓工具同時做太多事
- 不要讓資料查詢 tool 直接產生投資建議
股票查詢 tool:search_stocks
Agent 第一步通常是確認使用者輸入的是哪個股票、ETF 或商品。台股有上市、上櫃、ETF 和指數,不能只用一個字串模糊處理。
Tool schema
{
"name": "search_stocks",
"description": "Search Taiwan stocks, ETFs, and listed instruments by symbol or name.",
"parameters": {
"query": "台積電",
"market": "twse",
"instrument_type": "stock"
}
}Tool response
{
"data": [
{
"symbol": "2330",
"name": "台積電",
"market": "twse",
"instrument_type": "stock",
"currency": "TWD",
"is_active": true
}
],
"meta": {
"source": "tw-market-data"
}
}這個 tool 的工作只是確認標的,不應該在這一步產生任何買賣分析。上方 endpoint 與 schema 是示意,實際路徑請以 TW Market Data docs 為準。
行情與 OHLCV tool:get_daily_ohlcv
OHLCV 是技術分析、風控與回測的基礎。Agent 查詢 OHLCV 時,應該明確要求時間區間與 adjusted price 狀態。
Tool schema
{
"name": "get_daily_ohlcv",
"description": "Get daily OHLCV data for a Taiwan stock.",
"parameters": {
"symbol": "2330",
"from": "2025-01-01",
"to": "2025-12-31",
"adjusted": true
}
}Tool response
{
"data": [
{
"date": "2025-01-02",
"symbol": "2330",
"open": 1000,
"high": 1010,
"low": 995,
"close": 1005,
"volume": 25000000,
"turnover": 25200000000
}
],
"meta": {
"adjusted": true,
"timezone": "Asia/Taipei",
"currency": "TWD"
}
}如果你要理解 OHLCV、K 線與 adjusted price,可以參考 台股歷史股價 API 設計。
財報 tool:get_financial_statements
財報 tool 應該讓 agent 查詢損益表、資產負債表、現金流量表與財務比率。最重要的是回傳 announcement_date,避免 agent 在回測或研究中使用尚未公開的資料。
Tool schema
{
"name": "get_financial_statements",
"description": "Get financial statements for a Taiwan stock.",
"parameters": {
"symbol": "2330",
"statement": "income_statement",
"from": "2024Q1",
"to": "2025Q4"
}
}Tool response
{
"data": [
{
"symbol": "2330",
"period": "2025Q4",
"announcement_date": "2026-03-15",
"revenue": 625000000,
"gross_profit": 330000000,
"operating_income": 285000000,
"net_income": 238000000,
"eps": 9.21,
"currency": "TWD",
"unit": "thousand"
}
],
"meta": {
"statement": "income_statement",
"version": "latest"
}
}如果你要理解財報資料、公告日與基本面因子,可以參考 台股財報 API 教學。
三大法人 tool:get_institutional_flows
三大法人資料是台股 AI agent 的差異化資料層。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"
}
}Tool response
{
"data": [
{
"date": "2025-01-02",
"symbol": "2330",
"foreign_net_buy": 2700000,
"investment_trust_net_buy": 440000,
"dealer_net_buy": -90000,
"total_institutional_net_buy": 3050000,
"unit": "shares"
}
],
"meta": {
"timezone": "Asia/Taipei",
"unit": "shares"
}
}三大法人買賣超資料可以參考 三大法人買賣超 API。
技術指標 tool:get_technical_indicators
技術指標 tool 可以讓 agent 查詢 SMA、RSI、MACD、布林通道與 ATR。Agent 不應該自己猜指標值,也不應該只因為單一指標就產生買賣建議。
Tool schema
{
"name": "get_technical_indicators",
"description": "Get technical indicators for a Taiwan stock.",
"parameters": {
"symbol": "2330",
"from": "2025-01-01",
"to": "2025-12-31",
"adjusted": true,
"indicators": [
{
"name": "sma",
"window": 20
},
{
"name": "rsi",
"window": 14
},
{
"name": "macd",
"fast_window": 12,
"slow_window": 26,
"signal_window": 9
}
]
}
}技術指標資料管線可以參考 台股技術分析 API。
ETF / universe tool:get_etf_holdings
Agent 需要知道分析範圍。例如使用者可能問 0050 成分股最近的基本面變化,這時 agent 應該先查 ETF holdings,再逐一查財報或行情。
Tool schema
{
"name": "get_etf_holdings",
"description": "Get ETF holdings for a Taiwan ETF.",
"parameters": {
"etf_symbol": "0050",
"as_of": "2026-04-24"
}
}Tool response
{
"data": [
{
"etf_symbol": "0050",
"as_of_date": "2026-04-24",
"holding_symbol": "2330",
"holding_name": "台積電",
"weight": 0.61,
"sector": "Semiconductors"
}
],
"meta": {
"data_type": "etf_holdings"
}
}ETF 與指數成分股資料可以參考 台股 ETF 與指數成分股 API。
風控 tool:calculate_risk_metrics
風控 tool 應該和研究 tool 分開。它的工作不是產生 bullish 或 bearish,而是檢查波動率、最大回撤、流動性、集中度與交易限制。
Tool schema
{
"name": "calculate_risk_metrics",
"description": "Calculate risk metrics for a Taiwan stock or portfolio.",
"parameters": {
"symbols": [
"2330"
],
"from": "2025-01-01",
"to": "2025-12-31",
"metrics": [
"volatility",
"max_drawdown",
"average_turnover",
"liquidity"
]
}
}Tool response
{
"data": {
"symbol": "2330",
"annualized_volatility": 0.22,
"max_drawdown": -0.18,
"average_turnover": 0.04,
"liquidity_score": 0.91
},
"risk_flags": [
"requires_position_limit",
"single_asset_analysis"
]
}如果你要理解回測與風控,可以參考 Python 台股回測系統實作,並搭配 台股量化交易入門建立完整研究流程。
Agent response schema
Agent 的最終輸出應該是可檢查的 structured JSON,而不是單純一段自然語言。這樣 dashboard、risk manager 或 human reviewer 才能追蹤資料來源與風險。
{
"symbol": "2330",
"analysis_type": "stock_research",
"final_signal": "neutral",
"confidence": 0.61,
"summary": "Fundamentals are stable and technical indicators are moderately positive, but risk checks and updated financial data are required.",
"data_used": [
"daily_ohlcv",
"financial_statements",
"institutional_flows",
"technical_indicators",
"risk_metrics"
],
"date_range": {
"from": "2025-01-01",
"to": "2025-12-31"
},
"missing_data": [
"latest_monthly_revenue"
],
"risk_flags": [
"not_a_standalone_trading_signal",
"requires_backtesting",
"requires_human_review"
],
"not_investment_advice": true
}Recommended fields
| Field | Type | Description |
|---|---|---|
| symbol | string | 股票代號 |
| analysis_type | string | 分析類型,例如 stock_research |
| final_signal | string | bullish、bearish、neutral 或 custom signal |
| confidence | number | 0 到 1 的信心分數 |
| data_used | array | 使用了哪些資料 |
| missing_data | array | 缺少哪些資料 |
| risk_flags | array | 風險標記 |
| not_investment_advice | boolean | 明確標示不是投資建議 |
一個完整台股 AI agent workflow 範例
以下是一個研究型 workflow,示範 agent 如何分析一檔股票。
User:
"請分析 2330 最近的基本面、技術面與法人籌碼。"
Agent plan:
1. search_stocks("2330")
2. get_daily_ohlcv(symbol="2330", from="2025-01-01", to="2025-12-31", adjusted=true)
3. get_financial_statements(symbol="2330", statement="income_statement", from="2024Q1", to="2025Q4")
4. get_institutional_flows(symbol="2330", from="2025-01-01", to="2025-12-31", unit="shares")
5. get_technical_indicators(symbol="2330", indicators=["sma20", "rsi14", "macd"])
6. calculate_risk_metrics(symbols=["2330"], from="2025-01-01", to="2025-12-31")
7. generate structured response with data_used and risk_flags這種 workflow 的重點是每一步都能被 audit。若某個 tool 沒有資料,agent 應該標示 missing_data,而不是自行補上答案。
如何降低 hallucination?
降低 hallucination 的方法不是只靠 prompt,而是靠資料邊界與 workflow 設計。
- 所有金融數字都必須來自 API tool
- Agent 回答必須包含 data_used
- Agent 回答必須標示時間區間
- Agent 必須標示 missing_data
- 不允許沒有資料來源的 buy / sell 結論
- 不允許把單一指標直接變成交易建議
- 不允許用最新資料回推過去回測
- 不允許 agent 未經風控直接下單
- 所有 signal 應可回測
- 所有 response 應可被 audit
AI agent 的可靠性來自資料、schema、工具邊界與風控,而不是來自回答語氣。
從 research assistant 到 production workflow
台股 AI agent 可以先從 research assistant 做起,而不是一開始就做自動交易。這樣可以先驗證資料品質、tool schema、response format 與風險流程。
Research assistant
Research assistant 的任務包括:
- 查詢股票資料
- 摘要財報
- 比較三大法人買賣超
- 整理技術指標
- 產生風險檢查清單
- 輸出 dashboard summary
Production workflow
Production workflow 需要更多控制:
- authentication
- rate limit handling
- cache
- data validation
- retry
- audit log
- monitoring
- backtesting
- risk manager
- human approval
- compliance review
對金融資料產品來說,AI agent 不是獨立存在的功能,而是建立在穩定 API、schema、文件與風控流程之上的 workflow。
建議的 API endpoint 設計
以下是支援台股 AI agent workflow 的 endpoint 設計示意。
GET /v1/tw/stocks/search
GET /v1/tw/stocks/{symbol}/profile
GET /v1/tw/stocks/{symbol}/ohlcv
GET /v1/tw/stocks/{symbol}/financials
GET /v1/tw/stocks/{symbol}/monthly-revenue
GET /v1/tw/stocks/{symbol}/institutional-flows
GET /v1/tw/stocks/{symbol}/technical-indicators
GET /v1/tw/etfs/{symbol}/holdings
GET /v1/tw/indices/{index_code}/constituents
GET /v1/tw/calendar/trading-days實際 endpoint 命名不一定要完全相同。重點是 response schema 穩定、metadata 清楚、錯誤處理一致,並且能被 agent tool calling 安全使用。實際路徑請以 TW Market Data docs 為準。
FAQ
台股 AI Agent 是什麼?
台股 AI Agent 是使用 LLM 搭配台股資料 API 的自動化研究 workflow。它可以透過 tools 查詢行情、財報、三大法人、技術指標與風控資料,再產生結構化摘要與風險標記。
LLM 可以直接分析股票嗎?
LLM 可以協助整理與推理,但不應該自己猜股價、財報或法人買賣超。金融資料應該由可信 API tool 提供,LLM 負責把資料整理成可檢查的輸出。
台股 AI Agent 需要哪些 API?
至少需要股票搜尋、OHLCV、財報、月營收、三大法人、技術指標、ETF holdings、交易日曆與風控資料。不同 agent 可以使用不同工具。
AI agent 可以直接下單嗎?
不建議。本文討論的是研究與分析 workflow。實際交易需要券商下單 API、完整回測、風控、人為審核、合規流程與監控機制。
如何避免 AI agent 亂編股票資料?
可以要求所有金融數字都必須來自 API tool,並在回應中列出 data_used、時間區間、missing_data 與 risk_flags。沒有資料來源時,agent 應該明確說明資料不足。
TW Market Data 在 AI agent workflow 裡扮演什麼角色?
TW Market Data 提供穩定、結構化、developer-friendly 的台股 financial data API,讓 agent 可以查詢行情、財報、籌碼、技術指標、ETF 與交易日曆資料,而不是依賴模型記憶。
下一步
如果你正在建立台股 AI agent workflow,建議先處理三件事:
- 1. 定義 tools,而不是先寫長 prompt
- 2. 讓所有 agent output 都是 structured JSON
- 3. 把 hallucination control、risk manager 和 human review 放進流程
Need structured Taiwan market data for your AI agent, quant workflow, or research assistant?
本文討論資料工程、AI agent workflow、tool calling 與風控設計,不構成投資建議。