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
- Is it possible to do in Shiny the same type of chart as shown in the non-Shiny implementation?
- 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