这是一个高级问题。

我将自己的布局用于chartSeriesquantmod函数,甚至可以创建自己的newTA。一切正常。但是...

我想做什么,但不能:

a)处理3个图表中每个图表的图例:
-移至另一个角落,(从“左上”移至“右上”)
-更改内容
-如果需要,将其完全去除...

b)我的指标生成2个图例:
值1
值2
与上述相同...我该如何修改它们?如何删除它们?

c)控制yaxis的位置和范围(将其放在左侧/右侧
甚至删除它们
当图中有副轴时相同

d)修改主图例(右上角的一个)
日期范围写在哪里

一个有效的示例代码:

# Load Library
library(quantmod)

# Get Data
getSymbols("SPY", src="yahoo", from = "2010-01-01")

# Create my indicator (30 values)
value1 <- rnorm(30, mean = 50, sd = 25)
value2 <- rnorm(30, mean = 50, sd = 25)

# merge with the first 30 rows of SPY
dataset <- merge(first(SPY, n = 30),
                 value1,
                 value2)
# **** data has now 8 columns:
# - Open
# - High
# - Low
# - Close
# - Volume
# - Adjusted
# - a       (my indicator value 1)
# - b       (my indicator value 2)
#

# create my TA function - This could also be achieve using the preFUN option of newTA
myTAfun <- function(a){
   # input: a: function will receive whole dataset
   a[,7:8]  # just return my indicator values
}

# create my indicator to add to chartSeries
newMyTA <- newTA(FUN   = myTAfun, # chartSeries will pass whole dataset,
                                  # I just want to process the last 2 columns
              lty   = c("solid", "dotted"),
              legend.name = "My_TA",
              col   = c("red", "blue")
              )

# define my layout
layout(matrix(c(1, 2, 3), 3, 1),
       heights = c(2.5, 1, 1.5)
       )

# create the chart
chartSeries(dataset,
            type        = "candlesticks",
            main        = "",
            show.grid   = FALSE,
            name        = "My_Indicator_Name",
            layout      = NULL,     # bypass internal layout
            up.col      = "blue",
            dn.col      = "red",
            TA          = c(newMyTA(),
                            addVo()
                            ),
            plot        = TRUE,
            theme       = chartTheme("wsj")
            )

我尝试使用图例命令以及选项legend.name(对输出的控制非常有限)。
我看过chartSeries返回的chob对象,但是我不知道下一步该怎么做...

下图:

最佳答案

经过一段时间的学习之后,我找到了解决方案。我对R内部,S3和S4对象以及Quantum包有了更多的了解。它可用于更改图形中的任何内容。

A)如果图例属于二级指示器窗口:

  • 不要打印chartSeries(类型选项plot = FALSE)并获取返回的“chob”对象。
  • 在“chob”对象的插槽之一中,有一个“chobTA”对象,带有与图例相关的2个参数。将它们设置为NULL。
  • 最后,调用隐藏函数chartSeries.chob

  • 就我而言:
    #get the chob object
    my.chob <- chartSeries(dataset,
                           type        = "candlesticks",
                           main        = "",
                           show.grid   = FALSE,
                           name        = "My_Indicator_Name",
                           layout      = NULL,     # bypass internal layout
                           up.col      = "blue",
                           dn.col      = "red",
                           TA          = c(newMyTA(),
                                           addVo()
                                           ),
                           plot        = FALSE,          # do not plot, just get the chob
                           #plot        = TRUE,
                           theme       = chartTheme("wsj")
                           )
    
    #if the legend is in a secundary window, and represents
    #an indicator created with newTA(), this will work:
    [email protected]$TA[[1]]@params$legend <- NULL
    [email protected]$TA[[1]]@params$legend.name <- NULL
    quantmod:::chartSeries.chob(my.chob)
    

    B)在任何其他情况下,都可以修改“chartSeries.chob”,“chartTA”,“chartBBands”等,然后调用chartSeries.chob

    就我而言:
    fixInNamespace("chartSeries.chob", ns = "quantmod")
    quantmod:::chartSeries.chob(my.chob)
    

    在与legend()相关的行的开头添加“#”就足够了。

    而已。

    10-08 00:29