R quantmod chartSeries newTA chob - modify legend and axis (primary and secondary)

This is an extended question.

I use my own layout for the quantmod chartSeries function, and I can even create my own newTA. Everything is working fine. But...

What I want to do, but I can’t:

a) Manipulate the legend about each of the three diagrams: - go to another corner (from "topleft" to "topright") - change the content - delete it completely if necessary ...

b) My indicator generates 2 legends: value1 value2 is the same as above ... how can I change them? how can i delete them?

c) control position and yaxis range (put it left / right or even delete them the same when there is a time axis on the graph

d) Change the main legend (one in the upper right corner where the date range is written

Working code example:

# 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")
            )

I tried using the legend command as well as the legend.name option (with very limited output control). I looked at the chob object returned by chartSeries, but I cannot figure out what to do next ...

Image below:

Graph with indicators

+3
source share
1 answer

After some time, having learned a little about the insides of R, the objects S3 and S4 and the quantmod package, I came up with a solution. It can be used to change something on the chart.

A) If the legend refers to the window of the following indicator:

  • Do not print chartSeries (enter option plot = FALSE) and get the returned "chob" object.
  • "chob" "chobTA" , . 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:
my.chob@passed.args$TA[[1]]@params$legend <- NULL
my.chob@passed.args$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)

, "#" , ().

.

graph with modified legends on chartSeries graph (quantmod package)

+2

Source: https://habr.com/ru/post/1598085/


All Articles