0

I'd like to add (a) symbols and (b) labels to a chart with financial data that was created in Shiny, using quantmod. I'll describe what works and what doesn't work.

The following code works adding symbols, but it is not in Shiny.

library(quantmod)

getSymbols("AAPL", from='2007-07-01',to='2009-09-01')
chartSeries(AAPL, subset='2007-05-01::2009-09-01', theme=chartTheme('white'))
MACD2plot_plus  = which(diff(sign(MACD(Cl(AAPL))[,1])) > 0)
MACD2plot_minus = which(diff(sign(MACD(Cl(AAPL))[,1])) < 0)
range_y         = 0.5
addPoints(MACD2plot_plus,  AAPL[MACD2plot_plus,  4] - range_y, pch=24, col='blue', offset=1.0)
addPoints(MACD2plot_minus, AAPL[MACD2plot_minus, 4] + range_y, pch=25, col='red',  offset=1.0)
addSMA(n=12, on=1, col = "blue")
addSMA(n=26, on=1, col = "red")

The same thing in Shiny, but it doesn't produce the same plot. (This is partly based on xtsible object, looping in quantmod)

library(shiny)
library(quantmod)

ui <- fluidPage(
  textInput("tinput", "Ticker", value = "AAPL"),
  br(),
  plotOutput("qm_plot")
)
server <- function(input, output, session) {
  
  output$qm_plot <- renderPlot({
    stocks = input$tinput
    stockEnv <- new.env()
    
    getSymbols(input$tinput, from='2007-07-01',to='2009-09-01', env=stockEnv)
    
    stock = ls(stockEnv)
    chartSeries(stockEnv[[ls(stockEnv)]], subset='2007-05-01::2009-09-01', 
                name = input$tinput, theme=chartTheme('white'))
    MACD2plot_plus   = which(diff(sign(MACD(Cl(stockEnv[[stock]]))[,1])) > 0)
    MACD2plot_minus  = which(diff(sign(MACD(Cl(stockEnv[[stock]]))[,1])) < 0)
    range_y          = 0.5
    addPoints(MACD2plot_plus,  stockEnv[[stock]][MACD2plot_plus,  4] - range_y, pch=24, col='blue', offset=1.0)
    addPoints(MACD2plot_minus, stockEnv[[stock]][MACD2plot_minus, 4] + range_y, pch=25, col='red',  offset=1.0)
    addSMA(n=12, on=1, col = "blue")
    addSMA(n=26, on=1, col = "red")
  })
}
shinyApp(ui, server)

Questions

  1. Is it possible to do in Shiny the same type of chart as shown in the non-Shiny implementation?
  2. Assuming that point (1) is solved, is it possible to add labels (like, "Buy" or "Sell") in the chart in Shiny, replacing the triangles?

Thanks

N.B.: addTA does not have a procedure to add Points: https://rdrr.io/cran/quantmod/src/R/addTA.R

PLA
  • 89
  • 1
  • 7
  • Your first example doesn't seem to work, and why are you using `new.env` ? Try writing a function for your chart and then calling it from within the `server` instead. – Trusky Mar 25 '21 at 01:12
  • Trusky, I didn't include 'library(quantmod)' at the beginning of the code. Might that be the reason why it didn't work for you? Also, I used 'new.env' to be able to iterate over more than one ticker, if need be, as explained in [link](https://stackoverflow.com/questions/36120961/xtsible-object-looping-in-quantmod). Thanks. – PLA Mar 25 '21 at 14:12

1 Answers1

1

Here's the answer to question 1 :

Wrap addPoints and addSMA function in print for that to reflect on the chart.

library(shiny)
library(quantmod)

ui <- fluidPage(
  textInput("tinput", "Ticker", value = "AAPL"),
  br(),
  plotOutput("qm_plot")
)
server <- function(input, output, session) {
  
  output$qm_plot <- renderPlot({
    stocks = input$tinput
    stockEnv <- new.env()
    
    getSymbols(input$tinput, from='2007-07-01',to='2009-09-01', env=stockEnv)
    
    stock = ls(stockEnv)
    chartSeries(stockEnv[[ls(stockEnv)]], subset='2007-05-01::2009-09-01', 
                name = input$tinput, theme=chartTheme('white'))
    MACD2plot_plus   = which(diff(sign(MACD(Cl(stockEnv[[stock]]))[,1])) > 0)
    MACD2plot_minus  = which(diff(sign(MACD(Cl(stockEnv[[stock]]))[,1])) < 0)
    range_y          = 0.5
    print(addPoints(MACD2plot_plus,  stockEnv[[stock]][MACD2plot_plus,  4] - range_y, pch=24, col='blue', offset=1.0))
    print(addPoints(MACD2plot_minus, stockEnv[[stock]][MACD2plot_minus, 4] + range_y, pch=25, col='red',  offset=1.0))
    print(addSMA(n=12, on=1, col = "blue"))
    print(addSMA(n=26, on=1, col = "red"))
  })
}
shinyApp(ui, server)

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213