【发布时间】:2021-01-10 17:58:08
【问题描述】:
我已经能够成功地绘制股票的波动率,现在我开始使用 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, te = te, s0 = s0,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"
【问题讨论】: