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

StackOverflow https://stackoverflow.com/questions/23090963

  •  04-07-2023
  •  | 
  •  

Question

This is an advanced question.

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

What I want to do but I can't:

a) Manipulate the legend of each of the 3 charts: - move to other corner, (from "topleft" to "topright") - change the content - remove completely if needed ...

b) My indicator generates 2 legends: value1 value2 same as above ... how could I modify them? how could I delete them?

c) control position and range of yaxis (place it on the left / right or even remove them same when there is a secundary axis on the graph

d) Modify main legend (the one in the top right where is written the range of dates

A working sample code:

# 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 have tried using legend command, and also the option legend.name (with very limited control of the output). I have had a look at the chob object returned by chartSeries, but I can't figure out what to do next ...

Image below:

Graph with indicators

Was it helpful?

Solution

After some time learning a little bit more about R internals, S3 and S4 objects, and quantmod package, I've come up with the solution. It can be used to change anything in the graph.

A) If the legend belongs to a secundary indicator window:

  1. Do not print the chartSeries (type option plot = FALSE) and get the returned "chob" object.
  2. In one of the slots of the "chob" object there is a "chobTA" object with 2 params related to legend. Set them to NULL.
  3. Finally, call the hidden function chartSeries.chob

In my case:

#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) In any other case, it is possible to modify "chartSeries.chob", "chartTA", "chartBBands", etc and then call chartSeries.chob

In my case:

fixInNamespace("chartSeries.chob", ns = "quantmod")
quantmod:::chartSeries.chob(my.chob)

It is just enough with adding "#" at the beginning of the lines related to legend().

That's it.

graph with modified legends on chartSeries graph (quantmod package)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top