聊天視窗

量化投資的藝術:策略設計、實作與風險控管 - 第 3 章

第三章:策略設計基礎—因子模型與交易信號

發布於 2026-03-06 07:20

# 第三章:策略設計基礎—因子模型與交易信號 ## 3.1 章節導覽 本章將帶領讀者從**理論**走向**實踐**,透過因子模型的構造與交易信號的提取,說明如何將乾淨的資料轉化為可執行的投資策略。章節結構如下: 1. 因子設計原則與範例因子。 2. 信號生成與倉位調整。 3. 策略組合與風險調控。 4. 實戰案例:均值回歸策略。 5. 章節小結與進階閱讀建議。 > **思考題**:在實務中,因子往往需要「可持續」的解釋力與「可擴充」的彈性。你如何在保持簡潔的同時,保留足夠的資訊量? --- ## 3.2 因子設計原則 ### 3.2.1 透明度(Transparency) 因子必須是可解釋的,尤其在量化研究中,解釋力往往成為投資人對模型信任的關鍵。可透過簡單數學表達式、業務洞察或經濟學理論來說明。 ### 3.2.2 可持續性(Persistence) 因子需要在時間上具備一定的可持續性。常見的方法是計算**相對強度**(RS)或**動量**(Momentum)等,並用「滯後回歸」或「GARCH」模型檢驗其預測力。 ### 3.2.3 無噪音(Noise‑Free) 過度複雜的因子往往包含大量噪音,降低策略穩定性。可以採用**主成份分析**(PCA)或**因子正則化**(如 Lasso)來減少多餘變量。 ### 3.2.4 交叉驗證(Cross‑Validation) 避免過度擬合,建議使用**時間序列交叉驗證**(Time‑Series CV)或「滚动窗口」法。 --- ## 3.3 典型因子範例 | 因子類型 | 名稱 | 計算公式 | 參考文獻 | |---|---|---|---| | 估值因子 | 市盈率(PE) | `close / earnings_per_share` | Fama & French (1998) | | 動量因子 | 12‑month Momentum | `log(close_t / close_{t-252})` | Jegadeesh & Titman (1993) | | 風險因子 | 波動率 | `σ(close)` | - | | 成長因子 | 淨利成長 | `log(earnings_t / earnings_{t-1})` | - | > **實作提示**:使用 Pandas 的 `rolling()` 或 `expanding()` 方法,可快速計算滑動統計量。 --- ## 3.4 信號生成與倉位調整 ### 3.4.1 Z‑score 轉化 將因子值標準化,可使用 Z‑score: ```python factor_z = (factor - factor.mean()) / factor.std() ``` Z‑score 可以平衡不同單位因子,方便合併。 ### 3.4.2 排名與權重 一般做法是先將股票按 Z‑score 進行排序,選出前 N% 或 N 筆作為多頭,後 N% 或 N 筆作為空頭。權重可採**等權重**或**價值加權**。 ```python # 取前 10% 多頭、後 10% 空頭 top_n = int(0.1 * len(factor_z)) long = factor_z.nlargest(top_n).index short = factor_z.nsmallest(top_n).index ``` ### 3.4.3 風險調控 為避免單一因子失效,可將多個因子**加權平均**後再生成信號,或使用 **多因子回歸** 以調整各因子影響。 --- ## 3.5 策略組合與風險調控 ### 3.5.1 組合權重 若同時運用多個策略,可採「**Kelly**」或「**均方**」法來分配資金: ```python # Kelly 的簡易估算 k = 2 * (mean_ret - risk_free_rate) / std_ret**2 ``` ### 3.5.2 資產配置 即使單一因子策略表現優異,也建議分散投資於多個資產類別(如 ETF、債券、商品)以降低集中風險。 ### 3.5.3 動態止損 引入 **ATR** 或 **最大回撤** 監控,設定停損點: ```python atr = pd.Series(...) stop_loss = entry_price * (1 - 3 * atr) ``` --- ## 3.6 實戰案例:均值回歸策略 以下以 **Pairs Trading** 為例,說明如何構建、測試與優化一個簡單的均值回歸策略。 ### 3.6.1 資料準備 ```python import pandas as pd import numpy as np from statsmodels.tsa.stattools import coint # 讀取資料(已經經過 Chapter 2 的處理) df = pd.read_parquet('data/prices.parquet') # 只保留兩隻股票 pair = df[df['ticker'].isin(['AAPL', 'MSFT'])] pair = pair.pivot(index='datetime', columns='ticker', values='close').ffill() ``` ### 3.6.2 共整合檢定 ```python # 共整合檢定 coint_res = coint(pair['AAPL'], pair['MSFT']) print('p‑value:', coint_res[1]) # p‑value < 0.05 通常被接受為共整合 ``` ### 3.6.3 擬合線性關係 ```python beta = np.corrcoef(pair['AAPL'], pair['MSFT'])[0,1] # 亦可使用 OLS 估計 import statsmodels.api as sm model = sm.OLS(pair['AAPL'], sm.add_constant(pair['MSFT'])).fit() beta = model.params[1] ``` ### 3.6.4 構造價差(Spread) ```python spread = pair['AAPL'] - beta * pair['MSFT'] # 以 20 日移動平均為均值 ma = spread.rolling(window=20).mean() std = spread.rolling(window=20).std() # Z‑score zscore = (spread - ma) / std ``` ### 3.6.5 進出場訊號 ```python entry_long = zscore < -1 exit_long = zscore >= 0 entry_short = zscore > 1 exit_short = zscore <= 0 ``` ### 3.6.6 回測框架 ```python # 這裡僅示範簡易回測,實際可使用 Backtrader / Zipline 等框架 positions = pd.DataFrame(index=pair.index, columns=['long','short']) positions['long'] = 0 positions['short'] = 0 positions.loc[entry_long, 'long'] = 1 positions.loc[entry_short, 'short'] = -1 positions.loc[exit_long, 'long'] = 0 positions.loc[exit_short, 'short'] = 0 # 累積持倉 positions['long'] = positions['long'].replace(np.nan, 0).cumsum() positions['short'] = positions['short'].replace(np.nan, 0).cumsum() # 產生報酬 returns = pair.pct_change().dropna() strategy_ret = (positions['long'].shift(1) * returns['AAPL'] + positions['short'].shift(1) * returns['MSFT']) print(strategy_ret.cumsum().iloc[-1]) ``` > **實務提醒**:在真實交易前,務必驗證資料的**缺失值**、**時間對齊**與**滑點**;使用「模擬賬戶」測試交易成本與滑點影響。 --- ## 3.7 小結 1. **因子設計**:透明、可持續、無噪音、交叉驗證。 2. **信號生成**:Z‑score、排名、風險調控。 3. **策略組合**:Kelly、資產配置、動態止損。 4. **實戰案例**:Pairs Trading 演示從共整合到回測的完整流程。 ### 進階閱讀 - Fama, E. F., & French, K. R. (1998). *The Cross‑Section of Expected Stock Returns.* - Jegadeesh, N., & Titman, S. (1993). *Returns to Buying Winners and Selling Losers.* - Robert Engle (1982). *Autoregressive Conditional Heteroskedasticity with Estimates of the Variance of United Kingdom Inflation.* > **小提醒**:在實務中,策略永遠不會一次就完美。持續的監控與調整是長期成功的關鍵。