[英]plotting stock volatility in R using Shiny
希望有一些方向。 我是“R”和 Shiny 的新手,並且有一些編程背景,我想在這里申請,但無濟於事。 本質上,我使用quantmod
作為 Shiny 的擴展,因此我可以使用 plot 股票波動率。 使用以下命令在終端中成功:
getSymbols(‘SPY’,from=’2007/01/01′)
vol=volatility(SPY,n=25,N=252,calc=’close’)
chartSeries(vol)
但是,當我嘗試將其轉換為 Shiny 應用程序以便在 web 瀏覽器中呈現它時,我遇到了幾個錯誤。 可能使用了錯誤的功能? 不確定......這是我的代碼:
library(quantmod)
library(stringr)
library(tidyr)
library(dplyr)
library(ggplot2)
source("helpers.R")
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Theoretical Vol"),
helpText("Select a stock to examine. Information will be collected from Yahoo finance."),
textInput("symb", "Symbol", "SPY"),
dateRangeInput("dates","Date range",start = "2019-01-01",end = as.character(Sys.Date())),
plotOutput("plot")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
dataInput <- reactive({
getSymbols(input$symb, src = "yahoo",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE)
})
output$plot <- reactive(
vol <- volatility(input$symb,n=25,N=252,calc="close"))
}
# Run the application
shinyApp(ui = ui, server = server)
錯誤信息:
Listening on http://127.0.0.1:5727
Warning: Error in log: non-numeric argument to mathematical function
99: diff
98: ROC
97: volatility
96: <reactive>
80: output$plot
1: runApp
嘗試這個
ui <- fluidPage(
titlePanel("Theoretical Vol"),
helpText("Select a stock to examine. Information will be collected from Yahoo finance."),
textInput("symb", "Symbol", value="SPY"),
dateRangeInput("dates","Date range",start = "2019-01-01",end = as.character(Sys.Date())),
plotOutput("plot")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot <- renderPlot({
price <- getSymbols(req(input$symb), src = "yahoo",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE)
vol <- volatility(price,n=25,N=252,calc="close")
chartSeries(vol)
})
}
# Run the application
shinyApp(ui = ui, server = server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.