欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

期权 Pair trading 策略

程序员文章站 2022-03-21 13:05:48
...

Pair trading 策略 - 期权

0. 引库

import pandas as pd
import numpy as np
import tushare as ts
import seaborn
from matplotlib import pyplot as plt
plt.style.use('seaborn')
%matplotlib inline
stocks_pair = ['cu', 'zn']

1. 数据准备

# 加载数据
data1 = pd.read_csv('cu.csv')[['date','close']]
data2 = pd.read_csv('zn.csv')['close']
# 按行拼接收盘价
data = pd.concat([data1, data2], axis=1)
data.set_index('date',inplace = True)
# 重命名列('cu'、'zn')
data.columns = stocks_pair
data.head()
cu zn
date
2016/10/18 37300 18080
2016/10/19 37260 18200
2016/10/20 37270 18255
2016/10/21 37150 18245
2016/10/24 37320 18175
data.plot(figsize= (8,6));

期权 Pair trading 策略

2. 策略开发思路

data.corr()  # 协方差矩阵
cu zn
cu 1.000000 0.941114
zn 0.941114 1.000000
# 数据可视化,看相关关系
plt.figure(figsize =(8,6))
plt.title('Stock Correlation')
plt.plot(data['cu'], data['zn'], '.');
plt.xlabel('cu')
plt.ylabel('zn')
data.dropna(inplace = True)

期权 Pair trading 策略

# 对两股票价格做线性回归(白噪声项符合正态分布)
[slope, intercept] = np.polyfit(data.iloc[:,0], data.iloc[:,1], 1).round(2)      
slope,intercept
(0.48, 266.73)

(y-266.73-0.48x) 符合Stationary

# 算出 (y-266.73-0.48x) 一列
data['spread'] = data.iloc[:,1] - (data.iloc[:,0]*slope + intercept)
data.head()
cu zn spread
date
2016/10/18 37300 18080 -90.73
2016/10/19 37260 18200 48.47
2016/10/20 37270 18255 98.67
2016/10/21 37150 18245 146.27
2016/10/24 37320 18175 -5.33
data['spread'].plot(figsize = (8,6),title = 'Price Spread');

期权 Pair trading 策略

# 对 spread 进行标准化
data['zscore'] = (data['spread'] - data['spread'].mean())/data['spread'].std()
data.head()
cu zn spread zscore
date
2016/10/18 37300 18080 -90.73 -0.018872
2016/10/19 37260 18200 48.47 0.192004
2016/10/20 37270 18255 98.67 0.268053
2016/10/21 37150 18245 146.27 0.340163
2016/10/24 37320 18175 -5.33 0.110502
# 可视化标准化后的值
data['zscore'].plot(figsize = (10,8),title = 'Z-score')
plt.axhline(1.5)
plt.axhline(0)
plt.axhline(-1.5)

期权 Pair trading 策略

产生交易信号
data['position_1'] = np.where(data['zscore'] > 1.5, 1, np.nan)
data['position_1'] = np.where(data['zscore'] < -1.5, -1, data['position_1'])
data['position_1'] = np.where(abs(data['zscore']) < 0.5, 0, data['position_1'])
data['position_1'] = data['position_1'].fillna(method = 'ffill')
data['position_1'].plot(ylim=[-1.1, 1.1], figsize=(10, 6),title = 'Trading Signal_Uptrade');

期权 Pair trading 策略

data['position_2'] = -np.sign(data['position_1'])
data['position_2'].plot(ylim=[-1.1, 1.1], figsize=(10, 6),title = 'Trading Signal_Downtrade');

期权 Pair trading 策略

3. 计算策略年化收益并可视化

# 算离散收益率
data['returns_1'] = np.log(data['cu'] / data['cu'].shift(1))
data['returns_2'] = np.log(data['zn'] / data['zn'].shift(1))
# 算策略列
data['strategy'] = 0.5*(data['position_1'].shift(1) * data['returns_1']) + 0.5*(data['position_2'].shift(1) * data['returns_2'])
# 计算累积收益率
data[['returns_1','returns_2','strategy']].dropna().cumsum().apply(np.exp).tail(1)
returns_1 returns_2 strategy
date
2018/6/6 1.410724 1.378042 1.35536
# 画出累积收益率
data[['returns_1','returns_2','strategy']].dropna().cumsum().apply(np.exp).plot(figsize=(10, 8),title = 'Strategy_Backtesting');

期权 Pair trading 策略

# 计算年化收益率
data[['returns_1','returns_2','strategy']].dropna().mean() * 252
returns_1    0.216785
returns_2    0.202018
strategy     0.191562
dtype: float64
# 计算年化风险
data[['returns_1','returns_2','strategy']].dropna().std() * 252 ** 0.5
returns_1    0.158409
returns_2    0.210702
strategy     0.054305
dtype: float64
# 策略累积收益率
data['cumret'] = data['strategy'].dropna().cumsum().apply(np.exp)
# 策略累积最大值
data['cummax'] = data['cumret'].cummax()
# 算回撤序列
drawdown = (data['cummax'] - data['cumret'])
# 算最大回撤
drawdown.max()
0.026175344283502433

小结

策略的思考

  1. 对多只ETF进行配对交易,是很多实盘量化基金的交易策略;

策略的风险和问题:

  1. Spread不回归的风险,当市场结构发生重大改变时,用过去历史回归出来的Spread会发生不回归的重大风险;

  2. 中国市场做空受到限制,策略中有部分做空的收益是无法获得的;

  3. 回归系数需要Rebalancing;

  4. 策略没有考虑交易成本和其他成本;

相关标签: 金融量化