必威体育Betway必威体育官网
当前位置:首页 > IT技术

ETF50 & ETF500 Pair trading 策略

时间:2019-11-01 02:45:41来源:IT技术作者:seo实验室小编阅读:59次「手机版」
 

etf50

Pair trading 策略

ETF50 & ETF500 小demo

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 = ['50ETF', '500ETF']

1. 数据准备

# 加载数据
data1 = ts.get_k_data('510050', start='2018-04-01', end='2019-04-01')[['date','close']]
data2 = ts.get_k_data('510500', start='2018-04-01', end='2019-04-01')['close']
# 按行拼接收盘价
data = pd.concat([data1, data2], axis=1)
data.set_index('date',inplace = True)
# 重命名列('50ETF'、'500ETF')
data.columns = stocks_pair
data.head()
50ETF 500ETF
date
2018-04-02 2.702 6.424
2018-04-03 2.693 6.373
2018-04-04 2.694 6.321
2018-04-09 2.711 6.331
2018-04-10 2.775 6.380

画图

data.plot(figsize= (8,6));

在这里插入图片描述

2. 策略开发思路

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

在这里插入图片描述

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

(y+4.27-3.75x) 符合Stationary

# 算出 (y+4.27-3.75x) 一列
data['spread'] = data.iloc[:,1] - (data.iloc[:,0]*slope + intercept)
data.head()
50ETF 500ETF spread
date
2018-04-02 2.702 6.424 0.56150
2018-04-03 2.693 6.373 0.54425
2018-04-04 2.694 6.321 0.48850
2018-04-09 2.711 6.331 0.43475
2018-04-10 2.775 6.380 0.24375
data['spread'].plot(figsize = (8,6),title = 'Price Spread');

在这里插入图片描述

# 对 spread 进行标准化
data['zscore'] = (data['spread'] - data['spread'].mean())/data['spread'].std()
data.head()
50ETF 500ETF spread zscore
date
2018-04-02 2.702 6.424 0.56150 1.523889
2018-04-03 2.693 6.373 0.54425 1.477487
2018-04-04 2.694 6.321 0.48850 1.327522
2018-04-09 2.711 6.331 0.43475 1.182938
2018-04-10 2.775 6.380 0.24375 0.669158
# 可视化标准化后的值
data['zscore'].plot(figsize = (10,8),title = 'Z-score');
plt.axhline(1.5)
plt.axhline(0)
plt.axhline(-1.5)
<matplotlib.lines.Line2D at 0x2e8d8383c8>

在这里插入图片描述

产生交易信号

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');

在这里插入图片描述

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

在这里插入图片描述

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

# 算离散收益率
data['returns_1'] = np.log(data['50ETF'] / data['50ETF'].shift(1))
data['returns_2'] = np.log(data['500ETF'] / data['500ETF'].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
2019-04-01 1.063657 0.96155 1.114828
# 画出累积收益率
data[['returns_1','returns_2','strategy']].dropna().cumsum().apply(np.exp).plot(figsize=(10, 8),title = 'Strategy_Backtesting');

在这里插入图片描述

# 计算年化收益率
data[['returns_1','returns_2','strategy']].dropna().mean() * 252
returns_1    0.064263
returns_2   -0.040828
strategy     0.113192
dtype: float64
# 计算年化风险
data[['returns_1','returns_2','strategy']].dropna().std() * 252 ** 0.5
returns_1    0.236365
returns_2    0.264628
strategy     0.057670
dtype: float64
# 策略累积收益率
data['cumret'] = data['strategy'].dropna().cumsum().apply(np.exp)
# 策略累积最大值
data['cummax'] = data['cumret'].cummax()
# 算回撤序列
drawdown = (data['cummax'] - data['cumret'])
# 算最大回撤
drawdown.max()
0.053458095761447444

小结

策略的思考

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

策略的风险和问题:

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

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

  3. 回归系数需要Rebalancing;

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

文章最后发布于: 2019-05-11 12:21:29

相关阅读

分享到:

栏目导航

推荐阅读

热门阅读