-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotGreeks.R
198 lines (140 loc) · 4.43 KB
/
plotGreeks.R
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
# plot gamma of option when stock price is being varied #######################
plotGamma <- function(n, S0, K, T, r, sigma, type){
# input:
# n: percentage of initial stock price, used to control the width
# of the interval to calculate Gamma
#
# S0: initial stock
#
# K : strike price
#
# T : maturity date (in year fractions)
#
# r : constant risk-free short rate
#
# sigma : volatility
#
# type: call or put
#
#
# output: data frame with 2 columns, the 1st is stock prices, the second
# values of Vega for a corresponding stock price
#
# calculate gamma for a given stock price
gammaValue <- function(stock){
out <- BSgamma(BSworld(stock, K, T, r, sigma, type))$gamma
out
}
# sequence of stock values used for calculation of Gamma
S0 <- seq(S0 - n*(S0/100), S0 + n*(S0/100), length = 10000)
# calculated Gammas
gammaValues <- sapply(S0, gammaValue)
out <- data.frame(S0, gammaValues)
colnames(out) <- c("underlying", "Gamma")
out
}
# plot delta when stock price is being varied ##################################
plotDelta <- function(n, S0, K, T, r, sigma, type){
# input:
# n: percentage of initial stock price, used to control the width
# of the interval to calculate Delta
#
# S0: initial stock
#
# K : strike price
#
# T : maturity date (in year fractions)
#
# r : constant risk-free short rate
#
# sigma : volatility
#
# type: call or put
#
#
# output: data frame with 2 columns, the 1st is stock prices, the second
# values of Delta for a corresponding stock price
#
# calculate Delta for a given stock price
deltaValue <- function(stock){
out <- BSdelta(BSworld(stock, K, T, r, sigma, type))$delta
out
}
# sequence of stock values used for calculation of Gamma
S0 <- seq(S0 - n*(S0/100), S0 + n*(S0/100), length = 1000)
# calculated Deltas
deltaValues <- sapply(S0, deltaValue)
out <- data.frame(S0, deltaValues)
colnames(out) <- c("underlying", "Delta")
out
}
# plot rho when stock price is being varied ##################################
plotRho <- function(n, S0, K, T, r, sigma, type){
# input:
# n: percentage of initial stock price, used to control the width
# of the interval to calculate Rho
#
# S0: initial stock
#
# K : strike price
#
# T : maturity date (in year fractions)
#
# r : constant risk-free short rate
#
# sigma : volatility
#
# type: call or put
#
#
# output: data frame with 2 columns, the 1st is stock prices, the second
# values of Rho for a corresponding stock price
#
# calculate Rho for a given stock price
rhoValue <- function(stock){
out <- BSrho(BSworld(stock, K, T, r, sigma, type))$rho
out
}
# sequence of stock values used for calculation of Rho
S0 <- seq(S0 - n*(S0/100), S0 + n*(S0/100), length = 1000)
# calculated Rho
rhoValues <- sapply(S0, rhoValue)
out <- data.frame(S0, rhoValues)
colnames(out) <- c("underlying", "Rho")
out
}
# plot Vega when stock price is being varied ##################################
plotVega <- function(n, S0, K, T, r, sigma, type){
# input:
# n: percentage of initial stock price, used to control the width
# of the interval to calculate Vega
#
# S0: initial stock
#
# K : strike price
#
# T : maturity date (in year fractions)
#
# r : constant risk-free short rate
#
# sigma : volatility
#
# type: call or put
#
#
# output: data frame with 2 columns, the 1st is stock prices, the second
# values of Vega for a corresponding stock price
#
# calculate Vega for a given stock price
vegaValue <- function(stock){
out <- BSvega(BSworld(stock, K, T, r, sigma, type))$vega
out
}
# sequence of stock values used for calculation of Vega
S0 <- seq(S0 - n*(S0/100), S0 + n*(S0/100), length = 1000)
# calculated Vega
vegaValues <- sapply(S0, vegaValue)
out <- data.frame(S0, vegaValues)
colnames(out) <- c("underlying", "Vega")
out
}