期權 Pair trading 策略

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

4

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)

7

# 對兩股票價格做線性迴歸(白噪聲項符合正態分佈)
[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');

11

# 對 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)

13

產生交易信號
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');

16

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

17

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

21

# 計算年化收益率
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. 策略沒有考慮交易成本和其他成本;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章