如何解决在 R 中计算隐含波动率时出错
我已经能够成功地绘制股票的波动率,现在我已经使用 quantmod 使用历史收盘价计算股票历史隐含波动率。下面是我的代码,但我得到的错误让我失望。我是这门语言的新手,肯定有一点学习曲线,非常感谢任何输入:
library(quantmod)
library(stringr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(RND)
source("helpers.R")
ui <- fluidPage(
titlePanel("Realized Voltility"),helpText("Select a stock to examine. information will be collected from Yahoo finance."),textInput("symb","Symbol",value="SPY"),daterangeInput("dates","Date range",start = "2020-09-01",end = as.character(Sys.Date())),plotOutput("plot")
)
# Define server logic required to draw a histogram
server <- function(input,output) {
output$plot <- renderPlot({
##Get stock price data
price <- getSymbols(req(input$symb),src = "yahoo",from = input$dates[1],to = input$dates[2],auto.assign = FALSE)
##plot volitility based on price dataframe
vol <- volatility(price,n=25,N=252,calc="close")
##set values for BS computation of Implied Vol
r = 0.05
y = 0.02
te = 60/365
s0 = 400
##run through function to set option price range
sigma.range = seq(from = 0.1,to = 0.8,by = 0.05)
callPrice.range = floor(seq(from = 300,to = 500,length.out = length(sigma.range)))
bsm.calls = numeric(length(sigma.range))
for (i in 1:length(sigma.range))
{
bsm.calls[i] = price.bsm.option(r = r,te = te,s0 = s0,k = callPrice.range[i],sigma = sigma.range[i],y = y)$call
}
bsm.calls
##set call price range
callPrice.range
##loop through dataframe 'price' and compute IV for each closing day value based on variables r,te,s0,k,y,callPrice.range,and set lower/upper range
iVol <- for (i in price) {
impliedVol = compute.implied.volatility(r = r,k = i,y = y,callPrice.range = bsm.calls,lower = 0.001,upper = 0.999)
##for each computr value of IV,paste it in the console to start
print(paste("CLosing Price = ",impliedVol))
}
##chart it all
chartSeries(vol)
})
}
# Run the application
shinyApp(ui = ui,server = server)
收到的错误信息如下:
Listening on http://127.0.0.1:5727
Warning: Error in compute.implied.volatility: unused argument (callPrice.range = bsm.calls)
167: renderPlot [/Users/nobility/DevProjects/ShinyOptionspractice/app.R#63]
165: func
125: drawPlot
111: <reactive:plotObj>
95: drawReactive
82: origRenderFunc
81: output$plot
1: runApp
我的预期结果是打印到控制台的每个执行价格(收盘价)的各种计算隐含波动率如下:
[1] "Implied Vol = 339.390015"
[1] "Implied Vol = 338.220001"
[1] "Implied Vol = 326.660004"
[1] "Implied Vol = 329.980011"
[1] "Implied Vol = 326.540009"
[1] "Implied Vol = 330.200012"
[1] "Implied Vol = 336.029999"
[1] "Implied Vol = 343.540009"
解决方法
错误是因为 compute.implied.volatility()
没有 callPrice.range
参数。您可能打算使用 call.price
。
函数定义为:
compute.implied.volatility(r,te,s0,k,y,call.price,lower,upper)
因此您需要将您的电话更新为:
impliedVol = compute.implied.volatility(r = r,te = te,s0 = s0,k = i,y = y,call.price = bsm.calls,lower = 0.001,upper = 0.999)
,
您在 output$plot
中的打印语句将不起作用,因为它呈现的最终输出只是一个情节。因此,您需要将其打印在 output$plot
之外,但在观察者内打印,因为您正在使用用户输入变量 input$symb
和日期。控制台输出显示在下图的底部。
试试这个
ui <- fluidPage(
titlePanel("Realized Voltility"),helpText("Select a stock to examine. Information will be collected from Yahoo finance."),textInput("symb","Symbol",value="SPY"),dateRangeInput("dates","Date range",start = "2020-09-01",end = as.character(Sys.Date())),plotOutput("plot")
)
# Define server logic required to draw a histogram
server <- function(input,output) {
observe({
##Get stock price data
price <- getSymbols(req(input$symb),src = "yahoo",from = input$dates[1],to = input$dates[2],auto.assign = FALSE)
##plot volitility based on price dataframe
vol <- volatility(price,n=25,N=252,calc="close")
##set values for BS computation of Implied Vol
r = 0.05
y = 0.02
te = 60/365
s0 = 400
##run through function to set option price range
sigma.range = seq(from = 0.1,to = 0.8,by = 0.05)
callPrice.range = floor(seq(from = 300,to = 500,length.out = length(sigma.range)))
bsm.calls = numeric(length(sigma.range))
for (i in 1:length(sigma.range))
{
bsm.calls[i] = price.bsm.option(r = r,k = callPrice.range[i],sigma = sigma.range[i],y = y)$call
}
bsm.calls
##set call price range
callPrice.range
impliedVol = compute.implied.volatility(r = r,k = callPrice.range,upper = 0.999)
sigma.range
print(paste("CLosing Price = ",impliedVol))
output$plot <- renderPlot({
##chart it all
chartSeries(vol)
})
})
}
# Run the application
shinyApp(ui = ui,server = server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。