Stock Charting Platform in Shiny

Shiny has been available for some time now, but I've only recently had the opportunity to play around with it. For us R users who are constantly purporting the benefits of the language, Shiny is another point to add to the list.

Below I've created a simple stock charting application that pulls data from Yahoo's server, then presents the historical prices and returns distribution. Some additional features have been added, such as technical indicators, chart type, and the price range. I've also added an extra tab that displays financial statements of the company.

To run the application in your local browser, save the following piece of code as ui.R:


library(shiny)

shinyUI(navbarPage("Quant Qonsulting: Stock Charting Platform",
  
  tabPanel("Charts",
  fluidRow(
    
    ##################### SIDEPANEL
    column(4,

             #INFO TEXT
             helpText("Select a stock from a US Exchange to view chart.") ,
             
             #TICKER
             textInput("ticker", 
                       label=h5("Stock Symbol:"),
                       "GOOG"),
             
             #DATE PERIOD
             dateRangeInput("dates", 
                            label=h5("Date Range:"),
                            format = "dd-mm-yy",
                            start=Sys.Date()-90,
                            end=Sys.Date()),
             
             
             #CHART TYPE
             selectInput("chart_type",
                         label =h5("Chart Type:"),
                         choices=list("candlesticks", "matchsticks", "bars", "line")),
             
             #TA OPTIONS
             checkboxGroupInput("TA_check",
                                label=h5("Technical Indicators:"),
                                choices=list(
                                  "Directional Movement Index"="addADX()",
                                  "Bollinger Bands"="addBBands()",
                                  "Commodity Channel Index"="addCCI()",
                                  "Exponential Moving Average"="addEMA()",
                                  "Smooth Moving Average"="addSMA()",
                                  "Moving Average Convergence Divergence"="addMACD()",
                                  "Rate of Change"="addROC()",
                                  "Relative Strength Index"="addRSI()",
                                  "Parabolic Stop and Reversal"="addSAR()",
                                  "Stochastic Momentum Indicator"="addSMI()",
                                  "Volume"="addVo()",
                                  "William's %"="addWPR()"))      
             
             
           ),
           
    ###################### CHARTS
    column(8,plotOutput("distPlot"), plotOutput("returnsPlot"))
                       

          
  ) #END fluidRow
  ), #END tab
                   
                   tabPanel("Financials",     
                   fluidRow(             
                     #column(6,plotOutput("returnsPlot")), 
                     column(4, 
                            
                            #INFO TEXT
                            helpText("Select a stock from a US Exchange to view financials.") ,
                            
                            #TICKER
                            textInput("ticker", 
                                      label=h5("Stock Symbol:"),
                                      "GOOG")
                     ),
                                       
                     column(8,tableOutput("financialsText"))))
         
))

Save the following code as server.R:


library(shiny)
library(quantmod)


shinyServer(function(input, output) {


  output$distPlot <- renderPlot({
    
    getSymbols(as.character(input$ticker), src="yahoo", from = input$dates[1], to = input$dates[2])

    if (length(input$TA_check)!=0){
      chartSeries(eval(parse(text=input$ticker)), TA=paste(input$TA_check, collapse=";"), name=paste("Symbol:", input$ticker), type=input$chart_type, up.col='green', dn.col='red', theme="white")
    }
    if (length(input$TA_check)==0){
      chartSeries(eval(parse(text=input$ticker)), TA=NULL, name=paste("Symbol:", input$ticker), type=input$chart_type, up.col='green', dn.col='red', theme="white")
    }
    
  })
  
  output$returnsPlot <- renderPlot({ 
    getSymbols(as.character(input$ticker), src="yahoo", from = input$dates[1], to = input$dates[2])
    hist(dailyReturn(eval(parse(text=input$ticker))), prob=T, col='orange', xlab="Log Returns", main="Daily Returns")    
    lines(density(dailyReturn(eval(parse(text=input$ticker)))), col='blue')
    })
  
  
  output$financialsText <- renderTable({
    
    x <- getFinancials(input$ticker)
    cat(x, class(x), "\n")
    as.table(viewFinancials(eval(parse(text=x))))
    
    })
  
})

And to deploy the code, run the following script (after you change the directory to the location of your ui.R and server.R code):


library(shiny)
setwd("U:\\SHINY\\QQ")
runApp()

I apologize in advance for the cluttered format and lack of commenting in the code. I intend to set up an Ubuntu server and host this (and other Shiny applications) on there, but in the meantime I wanted to get this code up.

For those who want to learn how to deploy R applications themselves, visit: http://shiny.rstudio.com/.