-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrial_realtime.py
206 lines (181 loc) · 5.81 KB
/
trial_realtime.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
'''
delta hedging trial
real time hedging framework
author: JIMWWWJIM
'''
#----packages input---
#for the historical data
from WindPy import *
w.start()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from math import exp, sqrt, log
from random import seed, gauss
import datetime
import time
from scipy.stats.distributions import norm
import scipy
# pd setting
pd.set_option('display.width',320)
pd.set_option('display.max_rows',100)
# historical data collection and management
# minutes data based on wsi api function
wsi_data = w.wsi('RU1909.SHF','close','2019-05-24 09:00:00','2019-05-24 10:30:57','')
prices_data = wsi_data.Data
times_data = wsi_data.Times
#print(wsi_data)
#print(wsi_data.Data)
#print(wsi_data.Times)
def MonteCarlo(reTime, rf, S, K, sigma):
siTi = 10000
# siTi times of simulations, could be 1,000,000
#reTime remaining Time, would be given in the class()
list_1 = [] #asian call option value list
list_2 = [] #asian put option value list
dt = 1/3000
for si in range(siTi):
path = []
w = gauss(0,1)
totalNodes = reTime*3000
for node in range(int(totalNodes)):
if node == 0:
path.append(S)
else:
St = path[-1]*exp((rf-0.5*sigma**2)*dt+(sigma*sqrt(dt)*w))
path.append(St)
ave_close = 500
asian_put_value = max(K-ave_close,0)
asian_call_value = max(ave_close-K,0)
list_2.append(asian_put_value)
list_1.append(asian_call_value)
p = sum(list_2)/siTi
c = sum(list_1)/siTi
return {'asianput_MC':p,'asiancall_MC':c}
def asian_delta(reTime, rf, S, K, sigma):
S1=S+0.01*S
S2=S-0.01*S
MC_1 = MonteCarlo(reTime, rf, S1, K, sigma)
MC_2 = MonteCarlo(reTime, rf, S2, K, sigma)
putvalue_1 = MC_1['asianput_MC']
putvalue_2 = MC_2['asianput_MC']
delta = (putvalue_1-putvalue_2)/(0.02*S)
return delta
def asian_gamma(reTime, rf, S, K, sigma):
S1 = S + 0.01*S
S2 = S - 0.01*S
ad_1 = asian_delta(reTime, rf, S1, K, sigma)
ad_2 = asian_delta(reTime, rf, S2, K, sigma)
gamma = (ad_1 - ad_2)/(0.02*S)
return gamma
def asian_theta(reTime,rf,S,K,sigma):
global dt
dt = 1/3000
tau_1 = reTime-dt
MC_1 = MonteCarlo(tau_1, rf, S, K, sigma)
MC_2 = MonteCarlo(reTime, rf, S, K, sigma)
putvalue_1 = MC_1['asianput_MC']
putvalue_2 = MC_2['asianput_MC']
theta = (putvalue_2-putvalue_1)/dt
return theta
class MCAPut(object):
def __init__(self,start,T,K,N):
self.T=T
self.K=K
self.start=start #time to sell option
self.N=N
def calc(self,today,vol,S,rf):
if today<self.start:
return {'asian_delta':0,'asian_put':0,'asian_gamma':0,'asian_theta':0,'theta':0}
if today>self.T:
return {'asian_delta':0,'asian_put':0,'asian_gamma':0,'asian_theta':0,'theta':0}
if today == self.T:
return {'asian_delta':0,'asian_put':0,'asian_gamma':0,'asian_theta':0,'theta':0}
#reTime=(self.T-today)/250.
#print('class MCAPut self.T',self.T)
#print('class MCAPut today',today)
reTime=(self.T-today)/3000
#print('class MCAPut reTime',reTime)
asian_put = MonteCarlo(reTime, rf, S, self.K, vol)['asianput_MC']
delta = asian_delta(reTime, rf, S, self.K, vol)
#print(type(delta))
gamma = asian_gamma(reTime, rf, S, self.K, vol)
#print(type(gamma))
theta = asian_theta(reTime, rf, S, self.K, vol)
#print(type(theta))
return{'asian_delta':self.N*delta,'asian_put':self.N*asian_put,'asian_gamma':self.N*gamma,'asian_theta':self.N*theta}
def price_required():
wsq_data = w.wsq("RU1909.SHF", "rt_last,rt_latest")
print(wsq_data)
price_rt_last = wsq_data.Data[0][0]
price_rt_latest = wsq_data.Data[1][0]
print('price_rt_last',price_rt_last)
print('price_rt_latest',price_rt_latest)
return price_rt_last,price_rt_last
def european_price_required():
wsq_data = w.wsq("RU1909P12000.SHF", "rt_last,rt_imp_volatility,rt_theta,rt_delta,rt_gamma")
print(wsq_data)
price_ = wsq_data.Data[0][0]
IV_ = wsq_data.Data[1][0]
theta_ = wsq_data.Data[2][0]
delta_ = wsq_data.Data[3][0]
gamma_ = wsq_data.Data[4][0]
print('price of european option',price_)
print('implied volatility', IV_)
print('theta',theta_)
print('delta',delta_)
print('gamma',gamma_)
return price_,IV_,theta_,delta_,gamma_
def time_remain():
t_end = '2019-06-30'
def main():
columns = ['标的资产现价','标的资产最新成交价','RU1909P12000 现价','RU1909P12000 隐含波动率','RU1909P12000 theta','RU1909P12000 delta','RU1909P12000 gamma','t']
list_ua_price1 = []
list_ua_price2 = []
list_eo_price = []
list_eo_iv = []
list_eo_theta = []
list_eo_delta = []
list_eo_gamma = []
t_list = []
while True:
now = datetime.datetime.now()
#print(now.month,now.day)
stamp = '2019/0'+str(now.month)+'/'+str(now.day)
print(stamp)
print(now.hour,now.minute)
h_list = [9,10,11,13,14,15,21,23]
if now.hour in h_list:
if now.minute == 0 or now.minute == 30:
print(now)
ua_price_1, ua_price_2 = price_required()
eo_price, eo_iv, eo_theta, eo_delta, eo_gamma = european_price_required()
print('time:',now)
print(u'标的资产现价:',ua_price_1)
print(u'标的资产最新成交价:',ua_price_2)
print(u'RU1909P12000 现价:',eo_price)
print(u'RU1909P12000 隐含波动率:',eo_iv)
print(u'RU1909P12000 theta',eo_theta)
print(u'RU1909P12000 delta',eo_delta)
print(u'RU1909P12000 gamma',eo_gamma)
list_ua_price1.append(ua_price_1)
list_ua_price2.append(ua_price_2)
list_eo_price.append(eo_price)
list_eo_iv.append(eo_iv)
list_eo_theta.append(eo_theta)
list_eo_delta.append(eo_delta)
list_eo_gamma.append(eo_gamma)
t_list.append(now)
time.sleep(60)
if now.hour == 15 and now.minute == 0:
print(list_ua_price1)
print(llist_ua_price2)
print(list_eo_price)
print(list_eo_iv)
print(list_eo_theta)
print(list_eo_delta)
print(list_eo_gamma)
print(t_list)
df = pd.DataFrame([list_ua_price1,list_ua_price2,list_eo_price,list_eo_iv,list_eo_theta,list_eo_delta,list_eo_gamma,t_list],columns=columns)
print(df)
main()