Skill 寫法圖鑑

backtesting-frameworks

wshobson | 金融與投資 | 原文 | MIT

建構穩健的交易策略回測系統,處理前視偏誤、存活者偏誤和交易成本

中文翻譯

---
name: backtesting-frameworks
description: 為交易策略建構穩健的回測系統,正確處理前視偏誤(look-ahead bias)、存活者偏誤(survivorship bias)和交易成本。在開發交易演算法、驗證策略,或建構回測基礎設施時使用。
---

# 回測框架

建構穩健、可上線等級的回測系統,避開常見的陷阱,產出可靠的策略績效估計。

## 何時使用這個 Skill

- 開發交易策略的回測
- 建構回測基礎設施
- 驗證策略績效
- 避開常見的回測偏誤
- 實作 walk-forward 分析
- 比較不同的策略方案

## 核心概念

### 1. 回測偏誤

| 偏誤             | 說明               | 緩解方式              |
| ---------------- | ------------------------- | ----------------------- |
| **Look-ahead**   | 使用未來的資訊  | Point-in-time 資料      |
| **Survivorship** | 只在存活下來的標的上測試 | 納入已下市的證券 |
| **Overfitting**  | 對歷史資料做曲線擬合  | 樣本外測試   |
| **Selection**    | 只挑好看的策略 | 預先註冊        |
| **Transaction**  | 忽略交易成本    | 真實的成本模型   |

### 2. 正確的回測結構

```
Historical Data
      │
      ▼
┌─────────────────────────────────────────┐
│              Training Set               │
│  (Strategy Development & Optimization)  │
└─────────────────────────────────────────┘
      │
      ▼
┌─────────────────────────────────────────┐
│             Validation Set              │
│  (Parameter Selection, No Peeking)      │
└─────────────────────────────────────────┘
      │
      ▼
┌─────────────────────────────────────────┐
│               Test Set                  │
│  (Final Performance Evaluation)         │
└─────────────────────────────────────────┘
```

### 3. Walk-Forward 分析

```
Window 1: [Train──────][Test]
Window 2:     [Train──────][Test]
Window 3:         [Train──────][Test]
Window 4:             [Train──────][Test]
                                     ─────▶ Time
```

## 詳細的實作範例與模式

詳細的章節(從 `## Implementation Patterns` 開始)放在 `references/details.md`。上面的導覽摘要不夠用時,再讀那個檔案。

## 最佳實務

### 該做的

- **用 point-in-time 資料** - 避免前視偏誤
- **納入交易成本** - 要有貼近真實的估計
- **做樣本外測試** - 一定要保留資料
- **用 walk-forward** - 不要只做 train/test
- **蒙地卡羅分析** - 了解不確定性

### 不該做的

- **不要過度擬合** - 限制參數數量
- **不要忽略存活者偏誤** - 要納入已下市的標的
- **不要隨便使用調整過的資料** - 要了解做了哪些調整
- **不要用完整歷史資料做最佳化** - 要保留測試集
- **不要忽略容量** - 市場衝擊很重要

英文原文

---
name: backtesting-frameworks
description: Build robust backtesting systems for trading strategies with proper handling of look-ahead bias, survivorship bias, and transaction costs. Use when developing trading algorithms, validating strategies, or building backtesting infrastructure.
---

# Backtesting Frameworks

Build robust, production-grade backtesting systems that avoid common pitfalls and produce reliable strategy performance estimates.

## When to Use This Skill

- Developing trading strategy backtests
- Building backtesting infrastructure
- Validating strategy performance
- Avoiding common backtesting biases
- Implementing walk-forward analysis
- Comparing strategy alternatives

## Core Concepts

### 1. Backtesting Biases

| Bias             | Description               | Mitigation              |
| ---------------- | ------------------------- | ----------------------- |
| **Look-ahead**   | Using future information  | Point-in-time data      |
| **Survivorship** | Only testing on survivors | Use delisted securities |
| **Overfitting**  | Curve-fitting to history  | Out-of-sample testing   |
| **Selection**    | Cherry-picking strategies | Pre-registration        |
| **Transaction**  | Ignoring trading costs    | Realistic cost models   |

### 2. Proper Backtest Structure

```
Historical Data
      │
      ▼
┌─────────────────────────────────────────┐
│              Training Set               │
│  (Strategy Development & Optimization)  │
└─────────────────────────────────────────┘
      │
      ▼
┌─────────────────────────────────────────┐
│             Validation Set              │
│  (Parameter Selection, No Peeking)      │
└─────────────────────────────────────────┘
      │
      ▼
┌─────────────────────────────────────────┐
│               Test Set                  │
│  (Final Performance Evaluation)         │
└─────────────────────────────────────────┘
```

### 3. Walk-Forward Analysis

```
Window 1: [Train──────][Test]
Window 2:     [Train──────][Test]
Window 3:         [Train──────][Test]
Window 4:             [Train──────][Test]
                                     ─────▶ Time
```

## Detailed worked examples and patterns

Detailed sections (starting with `## Implementation Patterns`) live in `references/details.md`. Read that file when the navigation summary above is insufficient.

## Best Practices

### Do's

- **Use point-in-time data** - Avoid look-ahead bias
- **Include transaction costs** - Realistic estimates
- **Test out-of-sample** - Always reserve data
- **Use walk-forward** - Not just train/test
- **Monte Carlo analysis** - Understand uncertainty

### Don'ts

- **Don't overfit** - Limit parameters
- **Don't ignore survivorship** - Include delisted
- **Don't use adjusted data carelessly** - Understand adjustments
- **Don't optimize on full history** - Reserve test set
- **Don't ignore capacity** - Market impact matters