【问题标题】:Error in calculating Implied Volatility in R计算 R 中的隐含波动率时出错
【发布时间】: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"

【问题讨论】:

    标签: r shiny quantmod


    【解决方案1】:

    错误是因为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)
    

    【讨论】:

    • 谢谢,这似乎已经解决了一个错误......现在我得到了另一个:警告:uniroot 中的错误:端点处的 f() 值不是相反的符号
    【解决方案2】:

    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, te = te, s0 = s0, 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, te = te, s0 = s0, k = callPrice.range, y = y, 
                                   call.price = bsm.calls, lower = 0.001, 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)
    

    【讨论】:

    • 我很伤心 - 因为这确实有效(应用程序运行无错误)但目标是针对“价格”中的每个数据元素运行“compute.implied.volatility()”数据框并将其计算值打印到控制台。提供的答案打印出一组永远不会改变的计算,这实际上应该根据用户作为“符号”输入的内容而有所不同
    • 对不起,我对compute.implied.volatility()这个函数不熟悉。我没有看到 input$symbcompute.implied.volatility() 之间的依赖关系。您可以将观察者更改为observeEvent,但每次输入新股票代码时,控制台都会显示相同的输出。
    • 谢谢,它是从 rstudio 中的 RND 包派生的函数 - cran.r-project.org/web/packages/RND/RND.pdf 让控制台为每个输入符号打印股票特定数据是我所追求的。
    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多