-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairs_trade.py
More file actions
237 lines (149 loc) · 7.19 KB
/
pairs_trade.py
File metadata and controls
237 lines (149 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_datareader as web
import statsmodels.tsa.stattools as ts
def create_pairs_dataframe(datadir, symbols):
#Creates a pandas DataFrame containing the closing price
#of a pair of symbols based on CSV files containing a datetime
#stamp and OHLCV data
# Open the individual CSV files and read into pandas DataFrames
print ("Importing CSV data...")
# Fetching Data through Yahoo Finance
sym1 = web.DataReader(symbols[0], data_source='yahoo',start ='2016/1/1',end=time.strftime("%Y/%m/%d"))
sym2 = web.DataReader(symbols[1], data_source='yahoo',start ='2016/1/1',end=time.strftime("%Y/%m/%d"))
print ("Constructing dual matrix for %s and %s..." % symbols)
pairs = pd.DataFrame(index=sym1.index)
pairs['%s_close' % symbols[0]] = sym1['Adj Close']
pairs['%s_close' % symbols[1]] = sym2['Adj Close']
pairs = pairs.dropna()
return pairs
def check_cointegration(pairs, symbols):
print ("Computing Cointegration...")
coin_result=ts.coint(pairs['%s_close' % symbols[0]],pairs['%s_close' % symbols[1]])
#Confidence Level chosen as 0.05 (5%)
return coin_result[1]
def calculate_spread_zscore(pairs, symbols):
pairs['returns'] = np.log(pairs['%s_close' % symbols[0]]/pairs['%s_close' %symbols[1]])
pairs['mean'] = pairs['returns'].rolling(window=30,center=False).mean()
#30天均值
pairs = pairs.dropna()
# Create the spread and then a z-score of the spread
print ("Creating the spread/zscore columns...")
#pairs['spread'] = pairs['spy_close'] - pairs['hedge_ratio']*pairs['iwm_close']
zcore = (pairs.loc[:,'returns'] - pairs.loc[:,'mean']) /pairs.loc[:,'returns'].rolling(window=30,center=False).std()
pairs['zscore'] = zcore
return pairs
def signal_generate(pairs, symbols,
z_entry_threshold=2.0,
z_exit_threshold1=0.5,
z_exit_threshold2=3.5):
"""Create the entry/exit signals based on the exceeding of
z_enter_threshold for entering a position and falling below
z_exit_threshold for exiting a position."""
# Calculate when to be long, short and when to exit
pairs['longs'] = (pairs['zscore'] <= -z_entry_threshold)*1.0
pairs['shorts'] = (pairs['zscore'] >= z_entry_threshold)*1.0
pairs['exits'] = ((np.abs(pairs['zscore']) <= z_exit_threshold1 ) )*1.0
pairs['long_market'] = 0.0
pairs['short_market'] = 0.0
# These variables track whether to be long or short while
# iterating through the bars
long_market = 0
short_market = 0
for i, b in enumerate(pairs.iterrows()):
# Calculate longs
if pairs['longs'][i-1] == 1.0:
long_market = 1
# Calculate shorts
if pairs['shorts'][i-1] == 1.0:
short_market = 1
if pairs['exits'][i-1] == 1.0 or ((np.abs(pairs['zscore'][i]-pairs['zscore'][i-1]) > 1) and (np.abs(pairs['zscore'][i]+pairs['zscore'][i-1]) < 1)) :
pairs['exits'][i-1]=1
long_market = 0
short_market = 0
pairs.ix[i]['long_market'] = long_market
pairs.ix[i]['short_market'] = short_market
return pairs
def portfolio_returns(pairs, symbols):
"""Creates a portfolio pandas DataFrame which keeps track of
the account equity and ultimately generates an equity curve.
This can be used to generate drawdown and risk/reward ratios."""
# Convenience variables for symbols
sym1 = symbols[0]
sym2 = symbols[1]
pairs['ret_%s' % symbols[0]] = 100*((pairs['%s_close' %sym1]/pairs['%s_close' %sym1].shift(1))-1)
pairs['ret_%s' % symbols[1]] = 100*((pairs['%s_close' %sym2]/pairs['%s_close' %sym2].shift(1))-1)
# Construct the portfolio object with positions information
# Note that minuses to keep track of shorts!
print("Constructing a portfolio...")
portfolio = pd.DataFrame(index=pairs.index)
portfolio['positions'] = pairs['long_market'] - pairs['short_market']
pairs['positions'] = pairs['long_market'] - pairs['short_market']
pairs[sym1] = pairs['ret_%s' % symbols[0]] * portfolio['positions']
pairs[sym2] = -1.0*pairs['ret_%s' % symbols[1]] * portfolio['positions']
pairs['total'] = pairs[sym1] + pairs[sym2]
portfolio['total'] = pairs[sym1] + pairs[sym2]
# Construct a percentage returns stream and eliminate all
# of the NaN and -inf/+inf cells
print ("Constructing the equity curve...")
# portfolio['returns'] = portfolio['total'].pct_change()
portfolio['returns'] = portfolio['total']/100 ### wyd
portfolio['returns'].fillna(0.0, inplace=True)
portfolio['returns'].replace([np.inf, -np.inf], 0.0, inplace=True)
portfolio['returns'].replace(-1.0, 0.0, inplace=True)
# Calculate the full equity curve
# portfolio['cum_sum']=portfolio['total'].cumsum().plot()
portfolio['cum_sum'] = (1+portfolio['returns']).cumprod()# wyd
portfolio['cum_sum'].plot()
# (100*np.log(pairs['%s_close' % symbols[0]]/ pairs['%s_close' % symbols[0]].shift(1))).cumsum().plot()
# (100*np.log(pairs['%s_close' % symbols[1]]/ pairs['%s_close' % symbols[1]].shift(1))).cumsum().plot()
plt.xlabel("DateTime")
plt.ylabel("Cumulative Returns ")
plt.grid(True)
plt.show()
return portfolio
def run():
datadir='/Users/wangyaodong/study/qaunt_zhang/Statistical-Arbitrage-using-Pairs-Trading-master'
symbols = ('SBIN.NS', 'ICICIBANK.NS')
# symbols = ('ACC.NS', 'AMBUJACEM.NS')
returns = []
pairs = create_pairs_dataframe(datadir, symbols)
coint_check = check_cointegration(pairs, symbols)
if coint_check < 0.47:
# if pairs == 1:
# exit(1)
print("Pairs are Cointegrated")
print(coint_check)
pairs = calculate_spread_zscore(pairs, symbols)
pairs = signal_generate(pairs, symbols,
z_entry_threshold=2.0,
z_exit_threshold1=0.5,
z_exit_threshold2=3.5)
portfolio = portfolio_returns(pairs, symbols)
plt.plot(portfolio['cum_sum'])
plt.show()
pairs.to_csv("op.csv")
else:
print(coint_check)
print("Pairs are not CoIntegrated, Exiting...")
# sys.exit(0)
if __name__ == "__main__":
datadir = '/Users/wangyaodong/study/qaunt_zhang/Statistical-Arbitrage-using-Pairs-Trading-master'
symbols = ('601398.SS', '601939.SS')
pairs = create_pairs_dataframe(datadir, symbols)
print (pairs.head())
coint_check = check_cointegration(pairs, symbols)
print(coint_check)
pairs = calculate_spread_zscore(pairs, symbols)
print(pairs.tail())
pairs = signal_generate(pairs, symbols,
z_entry_threshold=2.0,
z_exit_threshold1=0.5,
z_exit_threshold2=3.5)
portfolio = portfolio_returns(pairs, symbols)
# returns.append(portfolio.ix[-1]['returns'])
# plt.plot(portfolio['returns'])
# plt.show()