3

I am new to Shiny and finished the Shiny tutorial over here: http://shiny.rstudio.com/tutorial/

In lesson 6, the tutorial shows us how to create an App where you input the stock symbol and date range to see its chart on the main panel.

I am trying to go a bit further by changing the App to one that takes 2 stock symbols and draws them on the same chart comparing them over time (overlapping on the same chart).

I've modified server.R to be:

library(quantmod)

shinyServer(function(input, output) {



  dataInput <- reactive({   
        getSymbols(c(input$symb1, input$symb2), src = "yahoo", 
        from = input$dates[1],
        to = input$dates[2],
        auto.assign = TRUE)
  })


  output$plot <- renderPlot({
    chartSeries(dataInput(), theme = chartTheme("white"), 
        type = "line", log.scale = input$log, TA = NULL)
  }) 
})

and my uiR to be:

library(shiny)

shinyUI(fluidPage(
  titlePanel("StockComp"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select two stocks and a time frame to compare. 
        Information will be collected from yahoo finance."),

      textInput("symb1", "1st Stock Symbol", "GOOG"),
      textInput("symb2", "2nd Stock Symbol", "AAPL"),

      dateRangeInput("dates", 
        "Date range",
        start = "2012-01-01", 
        end = as.character(Sys.Date())),

      actionButton("get", "Compare Stocks"),

      br(),
      br(),

      checkboxInput("log", "Plot y axis on log scale", 
        value = FALSE)

    ),

    mainPanel(plotOutput("plot"))
  )
))

I am getting:

Error in try.xts(x, error = "chartSeries requires an xtsible object") : chartSeries requires an xtsible object

I have tried to convert the dataInput to XTS, but XTS and reactive seems to be causing a lot of issues for me with my limited understanding of whats going on.

Dronny
  • 109
  • 1
  • 1
  • 6
  • Thanks for the comment! When I modify the dataInput to only take one input$symb instead of 2 (and set auto-assign to FALSE), the code above works just fine. I'm not sure why adding a second symbol is causing so much issue. – Dronny Jul 26 '15 at 21:31
  • 1
    I think it's because `getSymbols()` is just a wrapper that doesn't directly return an object, so when you try to assign it to `dataInput`, there's not a specific thing to assign. Instead, it creates separate data frames with the names of the called symbols in the global environment. – ulfelder Jul 26 '15 at 21:36
  • 1
    The output of `getSymbols()` is an xts object. Adding another ticker in the display of the same chart is not trivial. It should be possible to achieve this by defining the time series of one ticker as an "indicator" of the other, more specifically by using `newTA()` with the option `on=1`, but this is somewhat tricky. – RHertel Jul 27 '15 at 06:13
  • Thank you! Didn't realize that this was not a trivial matter. – Dronny Jul 27 '15 at 07:37

1 Answers1

0

as @RHertel mentioned, this is not so easy to do. But just to illustrate, I've tweaked your server.R script a little bit so you can see how it would turn out. I've added some comments in the script so you can follow what was changed.

First, I've separated the two data sets because once you set auto.assign=TRUE you cannot call dataInput anymore hoping it would return the two data sets. what auto.assign=TRUE does is it automatically assigns your two data sets separately to the global environment. So there's a different way to call the data sets in that case. In order to avoid the hassle, separate the two data sets so you can set auto.assign=FALSE and call the data sets from `dataInput.

Next, I stored the two data sets in a list so you can call them separately later in the script, in output$plot.

Lastly, I added the TA argument as @RHertel explained above. I had to use the paste function to automate the process.

As you can see the output looks ugly and still needs some cleaning. But I just wanted to give you an idea of how it would look like. I'm not an expert in finance but I would almost never want to see multiple stock prices on one chart because they could vary significantly and you can get weird charts like the one below. In order to standardize the scale, usually the stock returns are plotted on a single chart because even though the prices may be different, the returns have a lower and an upper limit of 0-100%. So they would fall on the same scale.

library(quantmod)

shinyServer(function(input, output) {



  dataInput <- reactive({   
    data1 <- getSymbols(input$symb1, src = "yahoo",      #Seperated the data into two seperate data sets and set auto.assign=FALSE
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)
    data2 <- getSymbols(input$symb2, src = "yahoo",     #Seperated the data into two seperate data sets and set auto.assign=FALSE
                        from = input$dates[1],
                        to = input$dates[2],
                        auto.assign = FALSE)
    return (list(data1,data2))                          #Stored the data sets in a single list 
  })


  output$plot <- renderPlot({
    chartSeries(dataInput()[[1]], TA=paste0("addTA(",input$symb1,",on=1)"),theme = chartTheme("white"),    #added the TA argument with the paste helper function
                type = "line", log.scale = input$log)
    chartSeries(dataInput()[[2]], TA=paste0("addTA(",input$symb2,",on=1)"),theme = chartTheme("white"), 
                type = "line", log.scale = input$log)
  }) 
})
Bahae Omid
  • 554
  • 6
  • 18