CH3-回测
Backtesting
A key difference between a traditional investment management process and a quantitative investment process is the possibility of backtesting a quantitative investment strategy to see how it would have performed in the past
量化投资流程和传统投资管理流程最重要的区别是:量化投资策略可以进行回测以观察它在过去的表现。
常用的回测工具
- Excel
The major disadvantage of Excel is that it can be used to backtest only fairly simple models.But, as I explained in the previous chapter, simple models are often the best!
Excel的主要缺点是只能用于简单模型的回测。但是,简单模型往往是最好的。
- MATLAB
MATLAB优点很多,功能强大,但是价格昂贵。有一些替代品:O-Matrix Octave
TradeStation
High-End Backtesting Platforms
寻找并使用历史数据库
While finding sources of data on the Internet is even easier than finding prospective strategies, there are a number of issues and pitfalls with many of these databases that I will discuss later in this section. These issues apply mostly to stock and exchange-traded fund (ETF) data only. Here are the most important ones.
虽然在互联网上寻找数据源比寻找有效的策略容易,但这些数据中存在问题和陷阱。下面是最主要的问题,这些问题仅适用股票和交易基金数据。
- 数据是否已经过分拆和股息调整?
- 是否剔除了幸存偏差数据?
- 你的策略用最高最低价吗?
使用最高最低价的回测没有使用开盘收盘价的回测可靠。
- 业绩度量
夏普比率
年化夏普比率的计算:一般而言,假设每个交易时段的长度为T,T可以是1个月、1天、1小时等,若要计算平均收益率、标准差以及相应的年化指标,就必须先算出一年的交易时段数$N_T$
$年化夏普比率= \sqrt {N_T} * 基于T的夏普比率$
年化标准差和日标准差的关系推理:
其中cov是指协方差。
最大挫跌和最长挫跌期
这个指标算起来挺麻烦,从图中看的话,倒是挺直观。
避免常见的回测陷阱
- 前视偏差
例如,“在日最低价的1%之内买入股票”的交易规则,就有前视偏差,因为在当日市场收盘前,是不可能知道日最低价的。
- 数据迁就偏差
因迁就历史数据的噪声而过度优化模型参数,造成策略的回测业绩高于未来业绩,即为数据迁就偏差。
交易成本
没有考虑交易成本的回测业绩是不真实的。
策略改进
对基本策略进行微小调整,来提升收益。策略的改进,最好基于经济学基本原理,或者透彻研究过的市场现象,而不是依据一些主观的试错法则。否则,就有可能产生数据迁就偏差。
import pandas as pd
import numpy as np
xlsx = pd.ExcelFile("datas/example3_4.xls")
df = pd.read_excel(xlsx,"table")
# 计算每日收益率:
df['daily_ret'] = df['Adj Close'].pct_change()
#假设年化收益率为4%,每年252个交易日 计算每日超额收益
df['excess_daily_ret'] = df['daily_ret'] - 0.04/252
sharp_ratio = np.sqrt(252) * df['excess_daily_ret'].mean() / df['excess_daily_ret'].std()
print(sharp_ratio)