【问题标题】:Adding symbols and labels to a chart in Shiny created with quantmod在使用 quantmod 创建的 Shiny 中向图表添加符号和标签
【发布时间】:2021-03-25 01:01:28
【问题描述】:

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

以下代码可以添加符号,但在 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 中的内容相同,但情节不同。 (这部分基于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)

问题

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

谢谢

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

【问题讨论】:

  • 您的第一个示例似乎不起作用,您为什么使用 new.env ?尝试为您的图表编写一个函数,然后从 server 中调用它。
  • Trusky,我没有在代码开头包含“library(quantmod)”。这可能是它对你不起作用的原因吗?此外,如果需要,我使用“new.env”来迭代多个代码,如link 中所述。谢谢。

标签: r shiny quantmod


【解决方案1】:

这是问题 1 的答案:

addPointsaddSMA 函数包装在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)

【讨论】:

  • 在第 1 点上效果很好,Ronak!感谢您的反馈。
猜你喜欢
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
相关资源
最近更新 更多