-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.R
461 lines (401 loc) · 16.2 KB
/
app.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
########################################################################
# PACKAGES and FUNCTIONS #
########################################################################
# if necessary install the following packages with:
# pkgs <- c("data.table", "DT", "ggplot2", "knitr", "magrittr", "rmarkdown",
# "RQuantLib", "shiny")
# install.packages(pkgs)
library(data.table)
library(DT)
library(ggplot2)
library(knitr)
library(magrittr)
library(rmarkdown)
library(shiny)
library(fOptions)
source("R/functions.R")
# uncomment this if the text-files should be recompiled
# rmdfiles <- c("files/about.Rmd", "files/text_intro_options.Rmd", "files/text_intro_valuation.Rmd")
# sapply(rmdfiles, knit, quiet = T)
# sapply(rmdfiles, render, quiet = T)
########################################################################
# UI #
########################################################################
# payoff_ui for the calculation-tab
payoff_ui <- function() {
sidebarLayout(
sidebarPanel = sidebarPanel(
h3("Option Basket Input"),
radioButtons("I_basket_dir", "Position",
list("Long" = 1, "Short" = 2),
selected = 1, inline = T),
radioButtons("I_basket_type", "Option Type",
list("Call" = 1, "Put" = 2, "Underlying" = 3),
selected = 1, inline = T),
conditionalPanel("input.I_basket_type < 3",
sliderInput("I_basket_strike", "Strike Price", value = 100,
min = 0, max = 250)
),
actionButton("BT_basket_new_option", "Insert", icon = icon("check")),
actionButton("BT_basket_clear", "Clear", icon = icon("remove")),
textOutput("text_new_option"),
br(),
helpText("The premium is automatically calculated for a given volatility\n",
"and for an underlying currently worth $100.")
),
mainPanel = mainPanel(
tabsetPanel(
tabPanel("Theory",
htmlOutput("payoff_theory")),
tabPanel("Figure",
plotOutput("p_payoffs")
),
tabPanel("Data",
DT::dataTableOutput("t_option_basket")
)
)
)
)
}
# greek_ui for the display of the greeks
greek_ui <- function() {
sidebarLayout(
sidebarPanel = sidebarPanel(
h3("Financial Greek Input"),
radioButtons("I_greek_dir", "Position",
list("Long" = 1, "Short" = 2),
selected = 1, inline = T),
radioButtons("I_greek_type", "Option",
list("Call" = 1, "Put" = 2),
selected = 1, inline = T),
radioButtons("I_greek_eu_am", "Option Type",
list("European" = 1, "American" = 2), inline = T),
sliderInput("I_greek_strike", "Strike Price (in $)", value = 100,
min = 1, max = 250),
sliderInput("I_greek_value_underlying", "Current Value Underlying (in $)",
value = 100,
min = 1, max = 250),
sliderInput("I_greek_maturity", "Maturity (in years)", value = 1,
min = 0, max = 5, step = 0.1),
sliderInput("I_greek_dvd_yield", "Dividend Yield (in %)", value = 5,
min = 0, max = 20),
sliderInput("I_greek_rf", "Risk-Free Rate (in %)", value = 1,
min = 0, max = 20),
sliderInput("I_greek_vola", "Volatility (in %)", value = 1,
min = 0, max = 20)
),
mainPanel = mainPanel(
tabsetPanel(
tabPanel("Theory",
DT::dataTableOutput("greek_theory")
),
tabPanel("Text Result",
verbatimTextOutput("text_single_result")
),
tabPanel("The Greeks ",
radioButtons("I_greek_var", "Variable",
list("Underlying" = "underlying",
"Strike" = "strike",
"Dividend Yield" = "dvd_yield",
"Risk-Free Rate" = "rf",
"Maturity" = "maturity",
"Volatility" = "vola"),
inline = T),
plotOutput("plot_greeks"),
"Note: the greeks refer mostly to the cases where the variable is the underlying."
)
)
)
)
}
# price_ui for the display of the greeks
price_ui <- function() {
fluidPage(tabsetPanel(
tabPanel("Theory",
htmlOutput("price_theory")
),
tabPanel("Binomial Model",
sidebarLayout(
sidebarPanel = sidebarPanel(
h3("Binomial Input"),
numericInput("I_price_n_steps", "Number of Steps",
min = 1, max = 10, value = 3),
radioButtons("I_price_type", "Option",
list("Call" = 1, "Put" = 2),
selected = 1, inline = T),
sliderInput("I_price_strike", "Strike Price (in $)", value = 100,
min = 1, max = 250),
sliderInput("I_price_value_underlying", "Current Value Underlying (in $)",
value = 100,
min = 1, max = 250),
sliderInput("I_price_tick", "Tick (in $)", value = 10,
min = 0, max = 50),
sliderInput("I_price_rf", "Risk-Free Rate (in %)", value = 1,
min = 0, max = 20)
),
mainPanel = mainPanel(
plotOutput("price_binomial")
)
)
),
tabPanel("Black-Scholes Model",
sidebarLayout(
sidebarPanel = sidebarPanel(
h3("Black-Scholes Input"),
radioButtons("I_bs_type", "Option",
list("Call" = 1, "Put" = 2),
selected = 1, inline = T),
sliderInput("I_bs_strike", "Strike Price (in $)", value = 100,
min = 1, max = 250),
sliderInput("I_bs_value_underlying", "Current Value Underlying (in $)",
value = 100,
min = 1, max = 250),
sliderInput("I_bs_maturity", "Maturity (in years)", value = 1,
min = 0, max = 5, step = 0.1),
sliderInput("I_bs_rf", "Risk-Free Rate (in %)", value = 1,
min = 0, max = 20, step = 0.1),
sliderInput("I_bs_vola", "Volatility (in %)", value = 1,
min = 0, max = 20, step = 0.1)
),
mainPanel = mainPanel(
uiOutput("price_bs")
)
)
)
)
)
}
# main_file for the main-tabs
main_ui <- shinyUI(
navbarPage("Options 101",
tabPanel("Payoff",
payoff_ui()
),
tabPanel("Valuation",
price_ui()
),
tabPanel("Price and the Greeks",
greek_ui()
),
tabPanel("About",
helpText("v 0.1 @ 2017", br(),
"David Zimmermann", br(),
"david_j_zimmermann[at]hotmail.com", br(),
"All feedback welcome!"))
)
)
########################################################################
# SERVER #
########################################################################
server_fun <- function(input, output, session) {
# init ----
output$payoff_theory <- renderUI({includeHTML("files/text_intro_options.html")})
output$price_theory <- renderUI({includeHTML("files/text_intro_valuation.html")})
empty_dt_positions()
render_plot1(output = output)
render_positions_table(output)
output$text_new_option <- renderText("")
# Observe Events Basket ----
observeEvent(input$BT_basket_new_option, {
cat("New Option!\n")
opt.dir <- input$I_basket_dir
opt.type <- input$I_basket_type
opt.strike <- input$I_basket_strike
opt.dir <- ifelse(opt.dir == 1, "Long", "Short")
if (opt.type == 1) {
opt.type <- "Call"
} else if (opt.type == 2) {
opt.type <- "Put"
} else if (opt.type == 3) {
opt.type <- "Underlying"
opt.strike <- 100
}
if (opt.type != "Underlying") {
cat(tolower(opt.type))
opt.premium <- fEuropean(type = tolower(opt.type),
underlying = 100,
strike = opt.strike,
dividendYield = 0,
riskFreeRate = 0,
maturity = 1,
volatility = 0.2)[["value"]] %>% round(4)
} else {
opt.premium <- 0
}
tmp <- data.table(name = paste0(opt.dir, " ",
opt.type, " ",
opt.strike),
type = opt.type,
dir = opt.dir,
strike = opt.strike,
premium = opt.premium)
dt.positions <<- rbindlist(list(dt.positions,
tmp))
cat("New Option registered!\n")
plotPF(output)
text_option <- paste("New", opt.type, "inserted, with a premium of",
opt.premium)
output$text_new_option <- renderText(text_option)
})
observeEvent(input$BT_basket_clear, {
cat("clear!\n")
# reset inputs!
output$text_new_option <- renderText("files/text_intro_options.html")
updateSliderInput(session, "I_basket_strike", value = 100)
updateRadioButtons(session, "I_basket_dir", selected = 1)
updateRadioButtons(session, "I_basket_type", selected = "Call")
# reset data!
empty_dt_positions()
render_plot1(NA, output = output)
render_positions_table(output)
})
# Single Options ----
option <- reactive(data.table(type = ifelse(input$I_greek_type == 1, "call",
"put"),
dir = ifelse(input$I_greek_dir == 1, "long",
"short"),
strike = input$I_greek_strike,
underlying = input$I_greek_value_underlying,
maturity = input$I_greek_maturity,
dvd_yield = input$I_greek_dvd_yield / 100,
rf = input$I_greek_rf / 100,
vola = input$I_greek_vola / 100,
eu_am = ifelse(input$I_greek_eu_am == 1,
"European", "American")))
option_result <- reactive({
dat <- option()
if (dat$eu_am == "European") {
fEuropean(type = dat$type,
underlying = dat$underlying,
strike = dat$strike,
dividendYield = dat$dvd_yield,
riskFreeRate = dat$rf,
maturity = dat$maturity,
volatility = dat$vola) %>% round(4)
} else {
fAmerican(type = dat$type,
underlying = dat$underlying,
strike = dat$strike,
dividendYield = dat$dvd_yield,
riskFreeRate = dat$rf,
maturity = dat$maturity,
volatility = dat$vola) %>% round(4)
}
})
greek_plot_data <- reactive({
dat <- option()
var <- input$I_greek_var
val <- dat[, var, with = F] %>% as.numeric()
input_parameters <- data.table(var = var,
val_min = round(val * 0.75, 4),
val_max = round(val * 1.25, 4))
# no values for the american option.. return emtpy dt
if (input$I_greek_eu_am == 2) {
n0 <- numeric(0)
pdat <- data.table(xvar = n0, xval = n0, yvar = n0, yval = n0)
} else {
pdat <- sensitivityWrapper(input_parameters, dat)
}
return(pdat)
})
output$plot_greeks <- renderPlot({
pdat <- greek_plot_data()
cat(nrow(pdat))
if (nrow(pdat) == 0) {
empty_df <- data.frame(x = 0, y = 0,
label = "Greeks currently not available for American Options")
ggplot(empty_df, aes(x = x, y = y, label = label)) +
geom_text() + theme_void()
} else {
ggplot(pdat[is.finite(yval)], aes(x = xval, y = yval)) + geom_line() +
theme_bw() + xlab(fUpper(pdat[1, xvar])) +
facet_wrap(~yvar, scales = "free") + ggtitle("The Greeks") +
ylab("Value of the Greek")
}
})
interpretation <- c("Value of the option",
"Sensitivity of the option value to a change in the underlying",
"Sensitivity of the option delta to a change in the underlying",
"Sensitivity of the option value to a change in the underlying's volatility",
"Sensitivity of the option value to a change in t, the remaining time to maturity",
"Sensitivity of the option value to a change in the risk-free interest rate",
"Sensitivity of the option value to a change in the dividend yield")
df <- data.frame(greek = c("value", "delta", "gamma", "vega", "theta", "rho", "dividendRho"),
interpretation = interpretation)
df_dt <- datatable(df, rownames = NA, colnames = c("Greek", "Interpretation"))
output$greek_theory <- DT::renderDataTable(df_dt)
output$text_single_result <- renderPrint(option_result())
# Prices & Valuation
output$price_binomial <- renderPlot({
create_tree(n_steps = input$I_price_n_steps,
type = ifelse(input$I_price_type == 1, "call", "put"),
s_0 = input$I_price_value_underlying,
tick = input$I_price_tick,
k = input$I_price_strike,
rf = input$I_price_rf/100)
}, height = 700)
bs_data <- reactive({
list(
type = input$I_bs_type,
s0 = input$I_bs_value_underlying,
k = input$I_bs_strike,
r = input$I_bs_rf/100,
ti = input$I_bs_maturity,
s = input$I_bs_vola/100
)
})
output$price_bs <- renderUI({
text1 <- "The Black-Scholes formula predicts an option price for a European call option to be equal to
$$ C = S_0 N(d_1) - Ke^{-rt} N(d_2) $$
with
$$ d_1 = \\frac{ln(S_o/Ke^{-rT})}{\\sigma \\sqrt{T}} $$
and
$$ d_2 = d_1 - \\sigma \\sqrt{T}. $$
The put option is priced using the put-call parity and is expected to be worth
$$ P = Ke^{-rT} N(-d_2) - S_0N(-d_1). $$
The result of the inputs from the left side are:
"
text2_call <- "$$ C = %.0f*N(%.03f) - %.0f e^{-%.03f * %.01f} * N(%.03f) = %.03f $$
with
$$ d_1 = \\frac{ln(%.0f/%.0fe^{-%.03f*%.01f})}{%.03f\\sqrt{%.01f}} = %.03f, $$
$$ d_2 = %.03f - %.03f \\sqrt{%.01f} = %0.3f, $$
$$ N(d_1) = N(%.03f) = %.03f, $$
and
$$ N(d_2) = N(%.03f) = %.03f, $$
"
text2_put <- "$$ P = %.0fe^{%.03f*%.01f} * N(-%.03f) - %.0f * N(-%.03f) = %.03f $$
with
$$ d_1 = \\frac{ln(%.0f/%.0fe^{%.03f*%.01f})}{%.03f\\sqrt{%.01f}} = %.03f $$
$$ d_2 = %.03f - %.03f \\sqrt{%.01f} = %0.3f, $$
$$ N(-d1) = N(%.03f) = %.03f, $$
and
$$ N(-d2) = N(%.03f) = %.03f, $$
"
s0 <- bs_data()$s0
k <- bs_data()$k
r <- bs_data()$r
ti <- bs_data()$ti
s <- bs_data()$s
d1 <- (log(s0/(k*exp(-r*ti)))) / (s*sqrt(ti))
d2 <- d1 - s*sqrt(ti)
n_d1 <- pnorm(d1)
n_d2 <- pnorm(d2)
n_d1_minus <- pnorm(-d1)
n_d2_minus <- pnorm(-d2)
C <- s0*n_d1 - k * exp(-r*ti)*n_d2
P <- k*exp(-r*ti)*n_d2_minus - s0*n_d1_minus
if (bs_data()$type == 1) {
withMathJax(
sprintf(paste0(text1, text2_call), s0, d1, k, r, ti, d2, C, s0, k, r, ti,
s, ti, d1, d1, s, ti, d2, d1, n_d1, d2, n_d2))
} else {
withMathJax(
sprintf(paste0(text1, text2_put), k, -r, ti, d2, s0, d1, P, s0, k, -r, ti,
s, ti, d1, d1, s, ti, d2, -d1, n_d1_minus, -d2, n_d2_minus))
}
})
}
########################################################################
# RUN APP #
########################################################################
shinyApp(main_ui, server_fun)