forked from fdallac/autocallable-simu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapp.py
144 lines (115 loc) · 4.54 KB
/
myapp.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
### IMPORT :
# python packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import time
# mypackages
import myfinutils as fin
import myblackscholes as bs
import myautocallable as acl
import mygreeks as grk
### INPUTS :
print('\nINPUT AND PARAMETERS ESTIMATION...\n')
# parameters
n_simu = int(1e6) # 10^6 simu
# data
N = 1 # NOMINAL
S_0 = 3042 # SPOT PRICE
kickout = 1.10 # KICKOUT BARRIER PERCENTAGE
protection = 0.95 # PROTECTION_BARRIER
I = 0.04 # INTEREST
T = 2020 # MATURITY
t_0 = 2015
# other variables
S_p = S_0 * protection # PROTECTION BARRIER
S_k = S_0 * kickout # KICKOUT BARRIER
t_steps = range(t_0+1, T+1)
# input from excel file
xls_data = pd.ExcelFile('res\\Data_Pricing.xlsx') # read pathfile
maturity_yrs = np.arange(2016, 2025+1, 1)
# strike prices
Strk = pd.read_excel(xls_data, sheet_name='Strike_Price', header=None, names=maturity_yrs)
# time to maturities
TtM = pd.read_excel(xls_data, sheet_name='Maturity', header=None, names=maturity_yrs)
# call options
Call = pd.read_excel(xls_data, sheet_name='Call_Price', header=None, names=maturity_yrs)
# put options
Put = pd.read_excel(xls_data, sheet_name='Put_Price', header=None, names=maturity_yrs)
# spot prices
Spot = pd.read_excel(xls_data, sheet_name='Underlying_Price', header=None, names=maturity_yrs)
# (daily) risk-free rate
d_RfR = pd.read_excel(xls_data, sheet_name='Risk_Free_Rate_EONIA', header=None, names=maturity_yrs)
### VARS ESTIMATION :
starting_time = time.time()
# interest rate
InR = fin.interestRate(Call, Put, Spot, Strk, TtM)
# implied volatility
flag = 'C' # call option is used for implied vol
ImV = fin.impliedVolatilitySurface(Call, flag, Spot, Strk, TtM, InR)
# risk-free rate -> discount rate
DsR = d_RfR.iloc[0] * np.sqrt(365)
# debug
elapsed_time = time.time() - starting_time
print('Interest rates, implied volatility, discount rates estimated in', elapsed_time, 's\n')
# find key-values to interpolate from dataframes
S = Strk[t_steps[0]]
i = 0
while (S_p > S[i]):
i += 1
# interpolation: dataframes -> series
k_Drift = (InR.iloc[i] * (S_p - S[i-1]) + InR.iloc[i-1] * (S[i] - S_p)) / (S[i] - S[i-1])
k_Vol = (ImV.iloc[i] * (S_p - S[i-1]) + ImV.iloc[i-1] * (S[i] - S_p)) / (S[i] - S[i-1])
k_TtM = TtM.iloc[0] # constant over strike value
### PLOT GREEKS BY MONTECARLO SIMULATION :
# initialize spark
sc = acl.startDistribuitedEnvironment()
## VEGA :
print('\n\nSIMULATING VEGA...\n')
starting_time = time.time()
vol_param = np.arange(0, 5, 0.5)
# simulate range of prices
priceForVega, vol = grk.computePricesForGreek('V', t_steps, k_TtM, k_Drift, k_Vol, DsR, S_0, S_k, S_p, N, I, vol_param, n_simu, sc)
# compute discrete derivatives
vega, vol_mid = grk.derivateGreek(priceForVega, vol)
# debug
print('\nElapsed time =', time.time() - starting_time, 's')
# plot vega
fig1, ax11, ax12 = grk.plot(vega, vol_mid, priceForVega, vol, 'VEGA', 'PRICE', 'VOLATILITY')
## DELTA :
print('\n\nSIMULATING DELTA...\n')
starting_time = time.time()
vol_param = np.arange(0.5, 1.5, 0.1)
# simulate range of prices
priceForDelta, spot = grk.computePricesForGreek('D', t_steps, k_TtM, k_Drift, k_Vol, DsR, S_0, S_k, S_p, N, I, vol_param, n_simu, sc)
# compute discrete derivatives
delta, spot_mid = grk.derivateGreek(priceForDelta, spot)
# debug
print('\nElapsed time =', time.time() - starting_time, 's')
# plot delta
fig2, ax21, ax22 = grk.plot(delta, spot_mid, priceForDelta, spot, 'DELTA', 'PRICE', 'SPOT PRICE')
## RHO :
print('\n\nSIMULATING RHO...\n')
starting_time = time.time()
vol_param = np.arange(-5, 5, 1)
# simulate range of prices
priceForRho, inR = grk.computePricesForGreek('R', t_steps, k_TtM, k_Drift, k_Vol, DsR, S_0, S_k, S_p, N, I, vol_param, n_simu, sc)
# compute discrete derivatives
rho, inR_mid = grk.derivateGreek(priceForRho, inR)
# debug
print('\nElapsed time =', time.time() - starting_time, 's')
# plot delta
fig3, ax31, ax32 = grk.plot(rho, inR_mid, priceForRho, inR, 'RHO', 'PRICE', 'INTEREST RATE')
# ax32.set_ylim([0.2, 0.8]) # correction for the 'rho' ax
## GAMMA :
print('\n\nSIMULATING GAMMA...\n')
starting_time = time.time()
# compute discrete derivatives
# (gamma is the second derivative of price over the spot price, i.e. the first derivative of delta)
gamma, spot_midmid = grk.derivateGreek(delta, spot_mid)
# debug
print('Elapsed time =', time.time() - starting_time, 's')
# plot gamma
fig4, ax41, ax42 = grk.plot(gamma, spot_midmid, priceForDelta, spot, 'GAMMA', 'PRICE', 'SPOT PRICE')
# show plots
plt.show()