期權分析平台架構 v0.1
Version: 2026-10-14
語言:繁體中文
屬 DawnLuma Options(期權) 研究區域 — 另見 DawnLuma — 期權 與 平台總覽。
1. Purpose
Section titled “1. Purpose”本文描述 DawnLuma 內的 期權分析平台(Options Analysis Platform) 架構:涵蓋期權研究、分析及回測。
初期目標包括:
- 從多個數據供應商及市場匯入期權市場數據;
- 將原始市場數據與計算後的分析結果分開保存;
- 把不同供應商的專用格式標準化成統一的內部數據模型;
- 計算期權分析指標,例如 IV、Greeks、OI、成交量、流動性、skew 及 term structure;
- 使用歷史市場數據回測期權策略;
- 透過 API、圖表、報告,以及未來的 Web 介面展示結果;
- 令核心分析及回測邏輯不依賴任何特定市場數據供應商。
第一個原型計劃使用 美國期權數據,因為開發時較容易取得,而且成本較低。
之後架構應可透過獨立的 data adapter 支援 HKEX 期權數據,而毋須重寫分析或回測引擎。
在 DawnLuma 內,Options(期權) 區域與 Stocks(股票) 研究(包括 MarketLens 相關工作)在邏輯上保持分離。
未來的 Strategies(策略) 層可整合兩邊輸出。見 DawnLuma 平台、網站與 SEO 設計、股票、策略。
2. High-Level Design Principles
Section titled “2. High-Level Design Principles”2.1 Separation of Concerns
Section titled “2.1 Separation of Concerns”應用程式分為四個主要部分:
- Data Layer
- Analysis Engine
- Backtest Engine
- Output Layer
每一部分都應可以獨立測試及替換。
2.2 Provider Independence
Section titled “2.2 Provider Independence”分析引擎及回測引擎不應需要知道數據來自:
- ThetaData
- Massive
- Cboe
- HKEX
- another future provider
供應商專用格式必須先轉換成 統一的內部標準模型(canonical internal model),然後才進入分析層。
2.3 Market Independence
Section titled “2.3 Market Independence”美國與香港市場在以下方面存在差異:
- contract multipliers;
- trading calendars;
- fee structures;
- settlement rules;
- exercise conventions;
- market-data formats;
- liquidity characteristics.
這些差異應封裝在市場專屬元件之後。
2.4 Preserve Raw Data
Section titled “2.4 Preserve Raw Data”在授權條款容許的情況下,原始市場數據應以未修改形式保存。
以下衍生數值:
- 隱含波動率;
- Delta;
- Gamma;
- Theta;
- Vega;
- IV Rank;
- IV Percentile;
- liquidity score;
應該可以由原始數據重新計算出來。
2.5 Backtesting as a Separate Engine
Section titled “2.5 Backtesting as a Separate Engine”回測並不只是一個普通的分析計算。
回測引擎必須獨立處理:
- strategy definitions;
- entry rules;
- exit rules;
- contract selection;
- fill assumptions;
- bid/ask spread;
- slippage;
- commissions and fees;
- exercise and assignment;
- position tracking;
- realised and unrealised P&L;
- performance metrics.
3. High-Level Architecture
Section titled “3. High-Level Architecture”flowchart TD A[External Data Sources<br/>ThetaData / Massive / Cboe / HKEX<br/>Underlying Prices / Rates / Dividends]
subgraph DL[1. Data Layer] B[供應商 Adapters] C[驗證與標準化] D[統一標準數據模型] E[(Raw Data Storage)] F[(Normalised Data Storage)] end
subgraph AE[2. Analysis Engine] G["IV & Greeks"] H[OI 與成交量分析] I[流動性分析] J[IV Rank / Percentile] K[Skew 與 Term Structure] L[異常活動/訊號] end
subgraph BE[3. Backtest Engine] M[策略定義] N[進場/出場規則] O[合約選擇器] P[交易執行/成交模型] Q[費用/Slippage] R["持倉與 P&L 引擎"] S[績效指標] end
subgraph OL[4. Output Layer] T[REST API] U[Web UI] V[圖表] W[報告/匯出] end
A --> B B --> C C --> D B --> E D --> F
F --> G F --> H F --> I F --> J F --> K F --> L
F --> M G --> M H --> M I --> M J --> M K --> M L --> M
M --> N N --> O O --> P P --> Q Q --> R R --> S
G --> T H --> T I --> T J --> T K --> T L --> T S --> T
T --> U T --> V T --> W4. System Boundaries
Section titled “4. System Boundaries”4.1 Data Layer
Section titled “4.1 Data Layer”負責:
- 下載或查詢市場數據;
- 讀取供應商專用檔案或 API;
- 數據驗證;
- timestamp 標準化;
- 股票代號標準化;
- 合約 mapping;
- 重複數據處理;
- 保留原始數據;
- 產生統一標準內部物件。
數據層 不應:
- 計算策略盈利;
- 判斷哪一個策略最好;
- 產生交易建議;
- 包含 UI 邏輯。
4.2 Analysis Engine
Section titled “4.2 Analysis Engine”負責:
- 隱含波動率;
- Greeks;
- IV Rank;
- IV Percentile;
- Open Interest 分析;
- 期權成交量分析;
- 異常成交量偵測;
- 流動性分析;
- Bid/Ask spread 分析;
- volatility skew;
- term structure;
- 期權鏈分析;
- 研究訊號。
分析引擎應只使用統一的內部標準模型。
它不應知道數據來源是 HKEX、ThetaData、Massive、Cboe 或其他供應商。
4.3 Backtest Engine
Section titled “4.3 Backtest Engine”負責按照指定策略重播歷史數據。
引擎應支援:
- 單腿策略;
- 多腿策略;
- Long 與 Short 期權;
- 進場篩選條件;
- 出場條件;
- DTE 選擇;
- 按 Delta 選擇合約;
- 按 Strike 選擇合約;
- 考慮 Bid/Ask 的交易執行;
- 貼近真實情況的成交模型;
- slippage;
- 佣金;
- 行使;
- 被指派;
- 到期;
- 資金使用;
- 投資組合層級績效;
- Drawdown;
- Expectancy;
- 勝/負統計;
- Profit Factor。
引擎本身應與特定策略無關。
避免為每一個策略建立獨立回測引擎。
不建議的設計:
LongCallBacktesterBullCallSpreadBacktesterBullPutSpreadBacktesterIronCondorBacktester建議的設計:
BacktestEngine +StrategyDefinition4.4 Output Layer
Section titled “4.4 Output Layer”負責:
- REST API;
- 期權鏈展示;
- 分析 dashboard;
- 回測報告;
- 圖表;
- CSV/JSON 匯出;
- 未來 Web UI。
輸出層不應包含金融計算邏輯。
5. Canonical Data Model
Section titled “5. Canonical Data Model”統一內部數據模型是整個架構中最重要的設計決策之一。
同一套模型應能同時用於美國及香港市場數據。
5.1 OptionContract
Section titled “5.1 OptionContract”public record OptionContract{ public required string Market { get; init; } public required string Underlying { get; init; } public required DateOnly Expiry { get; init; } public required decimal Strike { get; init; } public required OptionRight Right { get; init; } public required int Multiplier { get; init; } public required string Currency { get; init; } public string? ExerciseStyle { get; init; } public string? SettlementType { get; init; }}5.2 OptionQuote
Section titled “5.2 OptionQuote”public record OptionQuote{ public required OptionContract Contract { get; init; } public required DateTimeOffset Timestamp { get; init; } public decimal? Bid { get; init; } public decimal? Ask { get; init; } public int? BidSize { get; init; } public int? AskSize { get; init; }}5.3 OptionTrade
Section titled “5.3 OptionTrade”public record OptionTrade{ public required OptionContract Contract { get; init; } public required DateTimeOffset Timestamp { get; init; } public required decimal Price { get; init; } public required int Size { get; init; }}5.4 UnderlyingPrice
Section titled “5.4 UnderlyingPrice”public record UnderlyingPrice{ public required string Market { get; init; } public required string Symbol { get; init; } public required DateTimeOffset Timestamp { get; init; } public required decimal Price { get; init; }}5.5 OpenInterest
Section titled “5.5 OpenInterest”public record OpenInterest{ public required OptionContract Contract { get; init; } public required DateOnly Date { get; init; } public required long Value { get; init; }}6. Provider Adapter Design
Section titled “6. Provider Adapter Design”數據供應商應抽象到一個共用介面之後。
public interface IOptionsDataProvider{ Task<IReadOnlyList<OptionContract>> GetContractsAsync( string underlying, DateOnly date);
Task<IReadOnlyList<OptionQuote>> GetQuotesAsync( OptionContract contract, DateTimeOffset from, DateTimeOffset to);
Task<IReadOnlyList<OptionTrade>> GetTradesAsync( OptionContract contract, DateTimeOffset from, DateTimeOffset to);}可能的實作包括:
ThetaDataProviderMassiveOptionsProviderCboeOptionsProviderHKEXOptionsProvider7. Data Flow
Section titled “7. Data Flow”flowchart LR A[供應商數據] B[供應商 Adapter] C[驗證] D[標準化] E[統一標準模型] F[(Raw Storage)] G[(Normalised Storage)] H[分析引擎] I[回測引擎] J[API/報告/UI]
A --> B B --> F B --> C C --> D D --> E E --> G G --> H G --> I H --> I H --> J I --> J8. US First, HKEX Later
Section titled “8. US First, HKEX Later”第一個原型可以先使用美國市場數據。
flowchart TD A[ThetaData / Massive] B[美國市場 Adapter] C[統一期權數據] D[分析引擎] E[回測引擎] F[輸出]
A --> B B --> C C --> D C --> E D --> E D --> F E --> F之後應在毋須修改核心引擎的情況下加入香港市場支援。
flowchart TD A1[美國市場供應商] A2[HKEX] B1[美國市場 Adapter] B2[HKEX Adapter] C[統一期權數據] D[分析引擎] E[回測引擎] F[輸出]
A1 --> B1 A2 --> B2 B1 --> C B2 --> C C --> D C --> E D --> E D --> F E --> F9. Market-Specific Configuration
Section titled “9. Market-Specific Configuration”市場專屬行為不應硬編碼在核心引擎內。
例子:
TradingCalendarCommissionModelSlippageModelContractMultiplierExerciseModelSettlementModelCurrencyMarketHoursFeeModel例如,不要寫成:
var contractSize = 100;應使用:
var contractSize = contract.Multiplier;這一點特別重要,因為香港股票期權的合約乘數會因正股而有所不同。
10. Analysis Engine
Section titled “10. Analysis Engine”flowchart TD A[統一期權數據]
A --> B[隱含波動率] A --> C[Greeks] A --> D[未平倉合約] A --> E[Volume] A --> F[Bid/Ask 與流動性]
B --> G[IV Rank] B --> H[IV Percentile] B --> I[波動率 Skew] B --> J[Term Structure]
D --> K[OI Change] E --> L[異常成交量] F --> M[流動性評分]
G --> N[訊號] H --> N I --> N J --> N K --> N L --> N M --> N11. Backtesting Architecture
Section titled “11. Backtesting Architecture”flowchart TD A[歷史數據] B[分析訊號] C[策略定義]
A --> D[合約選擇器] B --> D C --> D
D --> E[進場規則] E --> F[交易執行/成交模型] F --> G[已開倉持倉] G --> H[持倉監控] H --> I{Exit Rule Triggered?} I -- No --> H I -- Yes --> J[出場交易] J --> K["P&L"] K --> L[績效指標]12. StrategyDefinition Concept
Section titled “12. StrategyDefinition Concept”回測引擎不應硬編碼策略名稱。
Long Call
Section titled “Long Call”Strategy: Long Call
Leg 1- Action: BUY- Type: CALL- DTE: 30-45- Delta Target: 0.50Bull Call Spread
Section titled “Bull Call Spread”Strategy: Bull Call Spread
Leg 1- Action: BUY- Type: CALL- Delta Target: 0.50
Leg 2- Action: SELL- Type: CALL- Higher StrikeBull Put Spread
Section titled “Bull Put Spread”Strategy: Bull Put Spread
Leg 1- Action: SELL- Type: PUT- Delta Target: 0.25
Leg 2- Action: BUY- Type: PUT- Lower Strike13. Backtest Inputs
Section titled “13. Backtest Inputs”一個完整回測所需要的不只是期權策略本身。
例子:
- bullish signal;
- VCP breakout;
- 異常期權成交量;
- IV Percentile 門檻;
- DTE 範圍;
- Delta 範圍;
- 最低 OI;
- 最大 Bid/Ask spread;
- 最低流動性評分。
例子:
- 最接近 0.50 的 Delta;
- 30–45 DTE;
- 指定 Strike 距離;
- ATM / ITM / OTM;
- 最低成交量/OI。
交易執行假設
Section titled “交易執行假設”例子:
- 以 Ask 買入;
- 以 Bid 賣出;
- 以 Mid Price 成交;
- 可設定 Slippage;
- 部分成交假設。
例子:
- +50% 盈利;
- -30% 止蝕;
- 21 DTE;
- 技術訊號失效;
- 到期;
- Assignment/Exercise。
14. Backtest Success Metrics
Section titled “14. Backtest Success Metrics”評估一個策略時,不應只看勝率。
重要指標包括:
Number of TradesWin RateAverage WinnerAverage LoserExpectancyTotal ReturnAnnualised ReturnMaximum DrawdownProfit FactorSharpe RatioCapital UsageAverage Holding PeriodExposure基本 Expectancy 計算:
Expectancy=Win Rate × Average Win-Loss Rate × Average Loss15. Signal Backtesting vs Strategy Backtesting
Section titled “15. Signal Backtesting vs Strategy Backtesting”flowchart TD A[期權分析平台]
A --> B[訊號回測] A --> C[策略回測]
B --> D[IV] B --> E[OI] B --> F[Volume] B --> G[Liquidity]
C --> H[Long Call] C --> I[Bull Call Spread] C --> J[Bull Put Spread] C --> K[Cash-Secured Put]
D --> L[訊號是否具有預測價值?] E --> L F --> L G --> L
H --> M[訊號是否能轉化為有盈利能力的交易?] I --> M J --> M K --> M訊號研究例子:
Today's option volume > 5 × 20-day average
Then measure underlying return after:+1 day+3 days+5 days+10 days另一個例子:
IV Percentile > 90
Then measure:5-day IV change10-day IV change20-day IV change建議次序:
- 先判斷訊號是否具有預測價值;
- 然後再判斷哪一種期權策略最適合表達該訊號。
這有助減少 overfitting。
16. Out-of-Sample Testing
Section titled “16. Out-of-Sample Testing”不要使用整個歷史數據集來調整參數。
例子:
2020–2024Development / Parameter Research
2025–2026Out-of-Sample Validation一旦參數在開發期內確定,進行樣本外測試時就應保持不變。
17. Initial Prototype Scope
Section titled “17. Initial Prototype Scope”Phase 1 — 數據
Section titled “Phase 1 — 數據”- 選擇美國期權數據供應商。
- 實作一個供應商 adapter。
- 為少量高流動性正股下載歷史數據。
- 把供應商數據轉換成統一標準模型。
- 在 console application 顯示解析後的數據。
建議初期使用的正股:
SPYQQQAAPLMSFTNVDAPhase 2 — 基本分析
Section titled “Phase 2 — 基本分析”Implement:
- bid/ask spread;
- mid price;
- simple liquidity measures;
- IV;
- Delta;
- Gamma;
- Theta;
- Vega.
Phase 3 — Long Call 回測
Section titled “Phase 3 — Long Call 回測”第一個研究問題:
當出現 bullish signal 時,買入一張約 30–45 DTE、Delta 約 0.50 的 Call,在計入真實 Bid/Ask spread、slippage 及費用後,是否仍然具有正的 expectancy?
第一次回測的主要目的,是驗證整個回測引擎是否正確運作。
Phase 4 — 多腿策略
Section titled “Phase 4 — 多腿策略”加入:
- Bull Call Spread
- Bull Put Spread
- Cash-Secured Put
Iron Condor 以及更複雜的策略應放到較後階段。
Phase 5 — 期權訊號
Section titled “Phase 5 — 期權訊號”加入以下歷史研究:
- 異常期權成交量;
- OI change;
- IV Rank;
- IV Percentile;
- skew;
- liquidity.
18. Storage Architecture
Section titled “18. Storage Architecture”對於大型歷史數據集,不要一開始就把所有數據載入 PostgreSQL。
flowchart TD A[供應商原始檔案] B[Object/File Storage] C[Parquet] D[DuckDB / Polars / Python] E[分析結果] F[(PostgreSQL)]
A --> B B --> C C --> D D --> E E --> F建議做法:
- 原始供應商檔案保持不可修改;
- 把原始檔案轉換成 Parquet;
- 使用 DuckDB / Polars 處理大型分析查詢;
- 把最終 metadata、執行紀錄、用戶、策略定義及結果存放於 PostgreSQL。
19. Suggested Project Structure
Section titled “19. Suggested Project Structure”dawnluma-options/│├── docs/│ ├── architecture-overview.md│ ├── data-model.md│ ├── data-providers.md│ ├── analysis-engine.md│ ├── backtest-engine.md│ ├── storage.md│ └── roadmap.md│├── src/│ ├── DawnLuma.Options.Domain/│ ├── DawnLuma.Options.Data/│ ├── DawnLuma.Options.Analytics/│ ├── DawnLuma.Options.Backtesting/│ ├── DawnLuma.Options.Api/│ └── DawnLuma.Options.Web/│├── tests/│ ├── DawnLuma.Options.Domain.Tests/│ ├── DawnLuma.Options.Data.Tests/│ ├── DawnLuma.Options.Analytics.Tests/│ └── DawnLuma.Options.Backtesting.Tests/│└── README.md20. Future Strategies Layer Integration
Section titled “20. Future Strategies Layer Integration”在 DawnLuma 內,Stocks 與 Options 研究應保持為兩個獨立系統。
未來的 Strategies 層可整合兩邊輸出。
flowchart LR A[Stocks<br/>MarketLens 研究] B[Strategies 層] C[期權分析平台]
A -->|Stock signals| B C -->|Options analytics| B
B --> D[整合研究] D --> E[股票 + 期權 + 策略分析]Stocks(股票研究)
Section titled “Stocks(股票研究)”- 趨勢;
- VCP;
- 相對強弱;
- 突破;
- volume;
- 股票評分。
期權分析平台
Section titled “期權分析平台”- IV;
- OI;
- 期權成交量;
- Greeks;
- 流動性;
- skew;
- term structure;
- 回測。
Strategies 層
Section titled “Strategies 層”- 把股票訊號與期權分析結合;
- 對機會排序;
- 比較不同策略結構;
- 進行跨系統研究。
Strategies 層應透過 API、events 或匯出的分析數據集取得資料,而不是直接查詢 Stocks 或 Options 系統的私人內部數據庫 schema。
21. Recommended First Development Steps
Section titled “21. Recommended First Development Steps”建立這份架構文件。
定義統一標準 domain models:
OptionContractOptionQuoteOptionTradeUnderlyingPriceOpenInterest定義:
IOptionsDataProvider實作一個美國市場數據供應商 adapter。
例子:
ThetaData→ ThetaDataProvider→ Canonical Models→ Console Output暫時不要開發 UI。
加入 raw / Parquet 儲存。
實作基本期權分析功能。
建立通用 Backtest Engine。
建立第一個 Long Call StrategyDefinition。
加入 Bull Call Spread 及 Bull Put Spread。
透過以下方式加入 HKEX 支援:
HKEX Data→ HKEXOptionsProvider→ Canonical Models→ Existing Analysis Engine→ Existing Backtest Engine目標是:加入 HKEX 支援時,只需要新增 adapter 及市場專屬設定,而不是重寫整個系統。
22. Architecture Summary
Section titled “22. Architecture Summary”flowchart TD A[市場數據供應商]
A --> B[供應商 Adapters] B --> C[統一標準數據模型] C --> D[(Storage)]
D --> E[分析引擎] D --> F[回測引擎] E --> F
E --> G[API / UI / Reports] F --> G
H[Stocks 研究] -. Future Integration .-> I[Strategies 層] G -. Options Analytics .-> I核心架構原則是:
數據來源、分析、回測與展示層必須保持分離。
這種分層設計可以令系統:
- 先由美國市場數據開始;
- 之後再支援 HKEX 數據;
- 更換數據供應商;
- 加入新策略;
- 加入新的分析功能;
- 重用同一個回測引擎;
- 與 DawnLuma Stocks 研究交換訊號,同時避免兩個系統高度耦合。