为了上课,赶制了一个异常简单的shiny app。展示二项树期权的定价。
贴在这里,立此存照。等着以后有时间慢慢把它做漂亮。。。
1.二项树期权
用fOptions包的BinomialTreeOption()计算期权价格;用BinomialTreePlot()展示期权价格二项树。
2.shiny
# ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Option types and Parameters"),
sidebarPanel(
selectInput(inputId = "comboBox",
label = "TypeFlag",choices = list(" European call option" = "ce", "European put option" = "pe", "American call option" = "ca","American put option" = "pa")),
numericInput(inputId = "pickNumber",
label = "Time",
min = 0, max = 10, value = 1),
numericInput(inputId = "pickNumber",
label = "Sigma",
min = 0, max = 1, value = .1),
numericInput(inputId = "pickNumber",
label = "r",
min = 0, max = 1, value = .1),
numericInput(inputId = "pickNumber",
label = "S",
min = 0, max = 10000, value = 1),
numericInput(inputId = "pickNumber",
label = "X",
min = 0, max = 10000, value = 1),
sliderInput(inputId = "slider",
label = "n",
min = 1, max = 10, value = 2, step = 1),
),
mainPanel(
h3("CRR Binomial Tree Option"),
plotOutput("plotDisplay")
)
))
#server.r
library(shiny)
library(fOptions)
shinyServer(function(input, output) {
output$plotDisplay <- renderPlot({
crr<-BinomialTreeOption(TypeFlag = input$comboBox,S=input$pickNumber4, X=input$pickNumber5,
Time=input$pickNumber1, r=input$pickNumber3, b=0, sigma=input$pickNumber2,
n=input$slider)
print(BinomialTreePlot(crr, dx = 0.05, dy = 0.2,
cex = 1, digits = 2,ylim=c(-input$slider-1,input$slider+1)))
})
})
把ui.r和serve.r各自保存为文件,放入同一个文件夹中/rresult。设置路径:
setwd('~/rresult')
运行:
runApp()
就可以看到所作的shiny应用了。