简体   繁体   中英

R: Get quantmod's chartSeries and AddTA to not show last value

When using chartSeries, by default it also shows on the top left of the plot the last value. Is there any way to prevent it from doing it?

When adding a new TA with addTA, you can avoid the last value on the plot by setting the argument legend = "", but only if you're making a new plot for the TA. If the TA is on a previously plotted graphic, it'll show the last value regardless of what you put in the legend argument.

getSymbols ("AAPL", src = "google")
chartSeries(AAPL)

What can I use here to prevent it from printing the last value on the plot?

addTA(EMA(Cl(AAPL)), on = 1, legend = "")

This still prints the last value on the top left of the plot. The weird part is that it doesn't do it if you're plotting on a new plot like this:

addTA(EMA(Cl(AAPL)), legend = "")

Is it like this by default, or is there something I can do to get around it?

The last value is shown by default (yes, annoyingly). You'll likely have to modify the source code to remove the last number showing in addTA .

I don't use addTA , but rather add_TA and chart_Series , because I think they look much better (second generation charts for quantmod ). Here is a solution that removes the last number from showing for the add_TA version. But you must be willing to modify the source code.

In add_TA, you'll need to modify approximately lines 56-60 of the source:

Replace the text.exp , which is this:

# this is inside add_TA:
if (is.na(on)) {
    plot_object$add_frame(ylim = c(0, 1), asp = 0.15)
    plot_object$next_frame()
    text.exp <- expression(text(x = c(1, 1 + strwidth(name)), 
                                y = 0.3, labels = c(name, round(last(xdata[xsubset]), 
                                                                5)), col = c(1, col), adj = c(0, 0), cex = 0.9, 
                                offset = 0, pos = 4))
    plot_object$add(text.exp, env = c(lenv, plot_object$Env), 

with these modifications:

if (is.na(on)) {
    plot_object$add_frame(ylim = c(0, 1), asp = 0.15)
    plot_object$next_frame()
    text.exp <- expression(text(x = c(strwidth(name)), # <- affects label on the subchart
                            y = 0.3, labels = name, col = c(col), adj = c(0), cex = 0.9, 
                            offset = 1, pos = 4))
     plot_object$add(text.exp, env = c(lenv, plot_object$Env), 
                expr = TRUE)

... and assign this modified code to a new variable, called say add_TA.mine :

add_TA.mine <- function (x, order = NULL, on = NA, legend = "auto", yaxis = list(NULL, 
                                                                  NULL), col = 1, taType = NULL, ...) 
{
  lenv <- new.env()
  lenv$name <- deparse(substitute(x))
  lenv$plot_ta <- function(x, ta, on, taType, col = col, ...) {
    xdata <- x$Env$xdata 
    ....
     [all the code for the rest of the function with modifications]....

           }
    }
    plot_object
}

Now, just run the code with the modified function

library(quantmod)
getSymbols("AAPL")

environment(add_TA.mine) <- environment(get("add_TA", envir = asNamespace("quantmod")))
assignInNamespace(x = "add_TA", value = add_TA.mine, ns = "quantmod")


chart_Series(AAPL, subset = "2017")
add_TA(RSI(Cl(AAPL)))
quantmod:::add_TA(RSI(Cl(AAPL)))

You can see the last value is no longer printed:

在此输入图像描述

(You could make the same kinds of changes in the old addTA code (perhaps via chartSeries if you really want to stick to the old plots)

If you're happy with the changes, and want to make them permament in add_TA , you can recompile the quantmod source code yourself with your modifications (ie you need to download the quantmod source code and recompile the package) . If you make a mess of things you can always redownload the original quandmod source code again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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