繁体   English   中英

在使用 quantmod 创建的 Shiny 中的图表中添加符号和标签

[英]Adding symbols and labels to a chart in Shiny created with quantmod

我想将 (a) 符号和 (b) 标签添加到图表中,其中包含使用 quantmod 在 Shiny 中创建的财务数据。 我将描述什么有效,什么无效。

以下代码可以添加符号,但不在 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")

Shiny 中的相同内容,但不会产生相同的 plot。 (这部分基于xtsible object,在 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)

问题

  1. 是否可以在 Shiny 中执行与非闪亮实现中所示相同类型的图表?
  2. 假设点 (1) 已解决,是否可以在 Shiny 的图表中添加标签(如“买入”或“卖出”),替换三角形?

谢谢

注意:addTA 没有添加积分的程序: https://rdrr.io/cran/quantmod/src/R/addTA.R

以下是问题 1 的答案:

addPointsaddSMA function 包装在print中以反映在图表上。

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)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM