-
Notifications
You must be signed in to change notification settings - Fork 35
/
BackTesting.py
328 lines (269 loc) · 13.3 KB
/
BackTesting.py
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
class: BackTestingSystem
author: Jerry Xia
email: [email protected]
date: 20/Apr/2017
modules:
- data input
- preprocessing
- PnL relative computing
- data output
"""
import numpy as np
from datetime import datetime
import pandas as pd
class Positions:
def __init__(self, positions, startIdx, endIdx):
self.positions = np.array(positions)
self.startIdx = startIdx
self.endIdx = endIdx
def duration(self):
return self.endIdx - self.startIdx
class PortPositions:
def __init__(self, timeSize, numEquities):
# print(timeSize)
self.startIdx = 0
self.endIdx = timeSize - 1
self.cumPositions = np.zeros((timeSize, numEquities))
self.numPositions = np.zeros(timeSize)
# todo: i in range number to number
# i in df.index[]
def addPositions(self, positionsChange):
if (positionsChange.startIdx >= positionsChange.endIdx):
return
for i in range(positionsChange.startIdx, positionsChange.endIdx):
# print("##################################################")
# print(type(positionsChange.positions))
# print(type(self.cumPositions[i,:]))
self.cumPositions[i, :] = self.cumPositions[i, :] + positionsChange.positions
self.numPositions[i] = self.numPositions[i] + 1
def maxPosJudge(self, positionsChange, maxPos):
startIdx = positionsChange.startIdx
# print(startIdx)
# print(self.numPositions[startIdx])
return self.numPositions[startIdx] < maxPos
def get_cumPositions():
return self.cumPositions
def get_numPositions():
return self.numPositions
class BackTestingSystem:
def __init__(self, numEquities, pointPrices, tickSizePrices, margins, transactionCostCoeff):
self.numEquities = numEquities
if (len(pointPrices) == numEquities):
self.pointPrices = np.array(pointPrices)
else:
print("number of equities unmatch: point prices")
if (len(tickSizePrices) == numEquities):
self.tickSizePrices = np.array(tickSizePrices)
else:
print("number of equities unmatch: tickSizes")
if (len(margins) == numEquities):
self.margins = np.array(margins)
else:
print("number of equities unmatch: margins")
self.transactionCostCoeff = transactionCostCoeff
# others
self.PnL = None
self.transactionCost = None
self.netPnL = None
def set_rollDate(self, rollDate):
self.rollDate = rollDate
def get_rollDate(self):
return self.rollDate
def set_exitUpLevel(self, exitUpLevel):
self.exitUpLevel = exitUpLevel
def set_exitDownLevel(self, exitDownLevel):
self.exitDownLevel
def set_triggerS(self, triggerS):
self.triggerS = triggerS
def set_triggerT(self, triggerT):
self.triggerT = triggerT
def get_marginPrices(self):
return self.margins / self.pointPrices
def get_tickSizes(self):
return self.pointPrices * tickSizePrices
def set_AUM(self, AUM):
self.AUM = AUM
def set_rollingStats(self, dfRollingStats):
self.dfRollingStats = dfRollingStats
self.df = pd.concat([self.df, self.rollingStats], axis=1)
def set_maxPoistions(self, maxPositions):
self.maxPositions = 30
def set_percentageInvested(self, pctInvest):
self.percentageInvested = pctInvest
def set_maxPositions(self, maxPositions):
self.maxPositions = maxPositions
def set_exitUpLevel(self, exitUpLevel):
self.exitUpLevel = exitUpLevel
def set_exitDownLevel(self, exitDownLevel):
self.exitDownLevel = exitDownLevel
def input_data(self, dfPrices, dfDurations, dfOptWeights, dfRollingStats):
self.dfPrices = dfPrices
self.dfDurations = dfDurations
self.dfOptWeights = dfOptWeights
self.df = pd.concat([self.dfPrices, self.dfDurations, self.dfOptWeights, dfRollingStats], axis=1)
# todo: delete
# def input_whole_data(self,df):
# self.df = df
def get_df(self):
return self.df
def time_delta_365(self, timeDelta):
if (timeDelta.days > 0):
return timeDelta.days / 365
else:
return 0
def preprocessing(self):
print("****************************************************************")
print("Start preprocessing...")
# basic setting
self.marginPrices = self.margins / self.pointPrices
self.maxInitMargin = self.AUM * self.percentageInvested
self.positionInitMargin = self.maxInitMargin / self.maxPositions
self.tickSizes = self.pointPrices * self.tickSizePrices
self.marginPrices = self.margins / self.pointPrices
# time to maturity
timeDeltas = self.rollDate - self.df.index
self.df['TimeToMaturity'] = timeDeltas
self.df.TimeToMaturity = self.df.TimeToMaturity.apply(self.time_delta_365)
self.timeToMaturity = self.df.TimeToMaturity
print(self.df.head())
# future duration
futureDurationsColumns = ["dfFutureDuration" + dur_str[8:] for dur_str in self.dfDurations.columns]
self.dfFutureDurations = pd.DataFrame(index=self.df.index, columns=futureDurationsColumns)
for index, row in self.dfDurations.iterrows():
self.dfFutureDurations.loc[index, :] = (row - self.df.TimeToMaturity[index]).values
# margin unit
# self.marginUnit = pd.Series(index = self.df.index, name="MarginUnit")
# for index, row in self.dfOptWeights.iterrows():
# self.marginUnit[index] = np.inner(np.abs(row.values), self.marginPrices)
self.marginUnit = self.dfOptWeights.apply(lambda x: np.inner(np.abs(x), self.marginPrices), axis=1)
self.marginUnit.rename("MarginUnit")
# national
self.portNotional = self.positionInitMargin / self.marginUnit
self.portNotional.rename("PortNotional", inplace=True)
# positions
positionsColumns = ["dfPosition" + dur_str[8:] for dur_str in self.dfDurations.columns]
self.dfPositions = pd.DataFrame(index=self.df.index, columns=positionsColumns)
for index, row in self.dfOptWeights.iterrows():
self.dfPositions.loc[index, :] = row.values * self.portNotional[index] / self.pointPrices
# tick size
self.portTickSize = self.dfPositions.apply(lambda x: np.inner(np.abs(x), self.tickSizes), axis=1)
self.portTickSize.rename("PortTickSize", inplace=True)
# current price
self.portPrice = pd.Series(index=self.df.index, name="PortPrice")
for idx in self.df.index:
self.portPrice[idx] = np.inner(self.dfPrices.loc[idx, :], self.dfOptWeights.loc[idx, :])
# tick size price
self.portTickSizePrice = pd.Series(index=self.df.index, name="PortTickSizePrice")
for idx in self.df.index:
self.portTickSizePrice[idx] = self.portTickSize[idx] / self.portNotional[idx]
# z-score
self.ZScore = pd.Series(index=self.df.index, name="ZScore")
for idx in self.df.index:
self.ZScore[idx] = (self.portPrice[idx] - self.df.RollingAvg[idx]) / self.df.RollingStd[idx]
# t-score
self.TScore = pd.Series(index=self.df.index, name="TScore")
for idx in self.df.index:
self.TScore[idx] = (self.portPrice[idx] - self.df.RollingAvg[idx]) / self.portTickSizePrice[idx]
# concat all results
self.df = pd.concat([self.df, self.dfFutureDurations, self.marginUnit, self.portNotional,
self.dfPositions, self.portTickSize, self.portPrice, self.portTickSizePrice,
self.ZScore, self.TScore], axis=1)
print("Preprocessing finished!")
print("****************************************************************")
return self.df
def _enterSignal(self, time):
return self.ZScore[time] <= -self.triggerS and self.TScore[time] <= -self.triggerT and time < self.rollDate
def _exitTime(self, startTime, rollTime=None):
# print("rollTimehere",rollTime)
positions = self.dfPositions.loc[startTime, :]
p0 = np.sum(positions.values * self.dfPrices.loc[startTime, :].values * self.pointPrices)
exitUp = self.exitUpLevel * self.portTickSize[startTime]
exitDown = self.exitDownLevel * self.portTickSize[startTime]
startIdx = self.df.index.get_loc(startTime)
for time in self.df.index[startIdx:]:
price = np.sum(positions.values * self.dfPrices.loc[time, :].values * self.pointPrices)
# print(price)
# print(p0)
if (price - p0 >= exitUp):
break
if (price - p0 <= -exitDown):
break
# print(time)
if (rollTime and time > rollTime):
# print("############$$$$$$$$$$$$$$$$$$$$#################")
# print("time",time)
# print("rolltime",rollTime)
time = rollTime
# print("after changing, time",time)
# print("############$$$$$$$$$$$$$$$$$$$$#################")
return time
# todo: change misleading name upwards, "port" is the term for portfolio, if not execute, call "df"
def calculateCumPositions(self):
print("**************************************************")
print("start calculate strategy positions")
self.portPositions = PortPositions(len(self.df.index), self.numEquities)
for idx, time in enumerate(self.df.index):
positions = self.dfPositions.iloc[idx, :]
# print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
# print("endtime",self._exitTime(time,self.rollDate))
# print("rolltime",self.rollDate)
# print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
endTimeIdx = self.df.index.get_loc(self._exitTime(time, self.rollDate))
# print("roll date",self.rollDate)
positionsChange = Positions(positions, idx, endTimeIdx)
# print("positionsChange.positions",positionsChange.positions)
if (self._enterSignal(time) and time < self.rollDate
and self.portPositions.maxPosJudge(positionsChange,
self.maxPositions)):
self.portPositions.addPositions(positionsChange)
print("##########################################")
print("add positions:", positionsChange.positions)
print("time:", time)
print("number of positions:", self.portPositions.numPositions[idx])
print("startTime:", self.df.index[positionsChange.startIdx])
print("endTime:", self.df.index[positionsChange.endIdx])
print("cumPositions:", self.portPositions.cumPositions[idx])
print("##########################################")
print("complete calculation")
print("**************************************************")
return self.portPositions
def calculateInitMargin(self):
portInitMargin = np.inner(np.abs(self.portPositions.cumPositions), self.margins)
self.portInitMargin = pd.Series(index=self.df.index, data=portInitMargin, name="InitMargin")
return self.portInitMargin
def calculateDailyPnL(self):
self.dailyPnL = pd.Series(index=self.df.index, name="DailyPnL")
self.dailyPnL[0] = 0
for idx, time in enumerate(self.df.index[1:]):
self.dailyPnL[time] = np.sum(self.pointPrices * self.portPositions.cumPositions[idx]
* (self.dfPrices.iloc[idx + 1] - self.dfPrices.iloc[idx]))
return self.dailyPnL
def calculateTransactionCost(self):
self.transactionCost = pd.Series(index=self.df.index, name="TransactionCost")
self.transactionCost[0] = 0
for idx, time in enumerate(self.df.index[1:]):
self.transactionCost[time] = (np.inner(
np.abs(self.portPositions.cumPositions[idx + 1] - self.portPositions.cumPositions[idx]),
self.tickSizes) * self.transactionCostCoeff)
return self.transactionCost
def calculateDailyNetPnL(self):
self.netDailyPnL = self.dailyPnL - self.transactionCost
self.netDailyPnL.name = "DailyNetPnL"
return self.netDailyPnL
def calculateCumNetPnL(self):
self.cumNetPnL = pd.Series(data=np.cumsum(self.netDailyPnL), index=self.df.index, name="CumNetPnL")
return self.cumNetPnL
def output_data(self):
self.preprocessing()
self.calculateCumPositions()
self.calculateInitMargin()
self.calculateDailyPnL()
self.calculateTransactionCost()
self.calculateDailyNetPnL()
self.calculateCumNetPnL()
dfOutput = pd.concat([self.dfPrices, self.dfOptWeights, self.portNotional,
self.dfPositions, self.portPrice, self.portInitMargin,
self.dailyPnL, self.cumNetPnL],
axis=1)
return dfOutput