user2500880
user2500880

Reputation: 125

Changing the legend in chartSeries to display values - Quantmod addTA

Is it possible to change the legend on the plot displayed in Quantmod so that values are displayed rather than the variable name? For example:

library("quantmod")
getSymbols("YHOO")
temp1 <- 6
temp2 <- "SMA"
barChart(YHOO)
addTA(ADX(YHOO, n=temp1, maType=temp2))

The legend that is displayed in the plot is ADX(YHOO, n=temp1, maType=temp2). I would like it to display the specific values instead i.e. ADX(YHOO, n=6, maType='SMA').

Upvotes: 3

Views: 2395

Answers (2)

user2500880
user2500880

Reputation: 125

The following is a partial solution which displays the values rather than variable names in the legend as well as the relevant output values for the TA. However, unlike the default settings of addTA, the text for each output value doesn't match the colour of the line on the addTA plot. Unfortunately I haven't worked out how to get the text of the output values to match the colour of its relevant line on the addTA plot. Any suggestions?

  library("quantmod")
  getSymbols("YHOO")
  barChart(YHOO, subset="last 4 months")
  col <- c("red", "blue", "green", "orange")
  temp1 <- 8
  temp2 <- "SMA"
  temp <- ADX(HLC(YHOO), n=temp1, maType=temp2)
  legend <- rep(NA, NCOL(temp)+1)
  legend[1] <- paste("ADX(HLC(YHOO)", "n=", temp1, "maType=", temp2)
  for(x in 2:(NCOL(temp)+1)){
      legend[x] <- (paste(colnames(temp[,(x-1)]),": ", round(last(temp[,(x-1)]),3), sep=""))
  }
  addTA(temp, legend = legend, col=col)

Upvotes: 0

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

There isn't a way to do this automatically with addTA, because it would need to know which of the the parameters of the TA call it needs to evaluate. But you can do it manually by setting the legend= argument yourself.

One way to do it is to use paste (or paste0).

barChart(YHOO)
Legend <- paste0('ADX(YHOO, n=',temp1,', maType=',temp2,')')
addTA(ADX(YHOO, n=temp1, maType=temp2), legend=Legend)

Or you could create and manipulate the call to get what you want.

barChart(YHOO)
callTA <- call("ADX",quote(YHOO),n=temp1,maType=temp2)
eval(call("addTA", callTA, legend=deparse(callTA)))

Upvotes: 2

Related Questions