-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
129 lines (108 loc) · 3.71 KB
/
main.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
from datetime import datetime
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from model.vix import build_vix_sig
from TVIX.data.data_util import read_data
__author__ = 'junyan'
holding_period = 1000
locking_period = 40
stoploss = 0.6
start_date = pd.datetime(2011, 12, 1)
tvix_data = read_data().set_index('Date')
sp500_data = pd.read_csv(
"/Users/junyan/Desktop/tvix/data/sp500.csv",
parse_dates=True).sort_values(['Date']).set_index('Date')
tvix_data.columns = ["tvix_" + i for i in tvix_data.columns]
sp500_data.columns = ["sp500_" + i for i in sp500_data.columns]
sp500_data["sp500_60 return"] = (sp500_data["sp500_Adj Close"].diff(60) /
sp500_data["sp500_Adj Close"])
sp500_data["sp500_120 return"] = (sp500_data["sp500_Adj Close"].diff(120) /
sp500_data["sp500_Adj Close"])
data = pd.concat(
[tvix_data, sp500_data],
axis=1, join='outer')
data["tvix_40_return"] = (
data["tvix_Adj Close"].diff(40).shift(-40) /
data["tvix_Adj Close"]
)
data["tvix_last_return"] = (
data["tvix_Adj Close"].diff(1)/data["tvix_Adj Close"]
)
data["tvix_return"] = data["tvix_last_return"].shift(-1)
data = data.dropna()
data = pd.merge(data, build_vix_sig(2, show=False),
how='inner', left_index=True, right_index=True)
def compute_max_drawn_down(PNL):
current_max = PNL[0]
current_max_drawn_down = 0
for i in list(PNL)[1:]:
current_max = max(i, current_max)
current_max_drawn_down = max(
current_max_drawn_down,
(current_max-i) / current_max
)
return current_max_drawn_down
def rolling_backtest(data):
dates = data.index
position = [0]
holding_count = [0]
lock_count = [0]
current_max = [1]
max_drowndown = [0]
enter = [0]
left = [0]
for i in dates[1:]:
background = data.loc[i, :]
if background['vix_sell_sig'] == 1 and position[-1] == 0:
position.append(-1)
holding_count.append(1)
enter.append(1)
left.append(0)
elif background["tvix_last_return"] > 0 and position[-1] == -1:
position.append(0)
holding_count.append(0)
enter.append(0)
left.append(1)
elif holding_count[-1] == holding_period and position[-1] == -1:
position.append(0)
holding_count.append(0)
enter.append(0)
left.append(1)
else:
position.append(position[-1])
holding_count.append(holding_count[-1]+1)
enter.append(0)
left.append(0)
data["position"] = position
data["enter"] = enter
data["left"] = left
data["cum_pnl_single"] = (data['position'] * data["tvix_return"]).cumsum()
data["cum_pnl_compound"] = (
1 + data['position'] * data["tvix_return"]).cumprod()
plot, axe = plt.subplots(2, 2)
data[
[
"cum_pnl_single",
"sp500_Adj Close"
]
].plot(ax=axe[0][0], secondary_y="sp500_Adj Close")
data[
[
"cum_pnl_compound",
"sp500_Adj Close"
]
].plot(ax=axe[1][0], secondary_y="sp500_Adj Close")
axe[0][1].hist(data['tvix_return'][data['position'] == -1], bins=30)
monthly_pnl = data['cum_pnl_compound'].resample('M', how='last')
(monthly_pnl.diff()/monthly_pnl.shift(1)).plot(ax=axe[1][1])
print(data[["position", "tvix_return",
"vix_Adj Close",
"vix_sell_sig",
"left"]])
data.to_excel("result.xlsx")
plt.show()
if __name__ == '__main__':
rolling_backtest(data)
print("max drawndown:", compute_max_drawn_down(data.cum_pnl_compound))
plt.show()