Search code examples
rquantmoddynamic-chart-series

chartSeries: Logarithmic scale for indicator


I am new to R and quantmod. I created a new indicator which I like to add to a plot with addTA. But the scale of the indicator should be logarithmic. First I tried (with RSI as an example)

> chartSeries ...
> rsi <- RSI(Cl(...))
> addTA(rsi,log.scale=T)
Warning message:
In plot.xy(xy.coords(x, y), type = type, ...) :
  "log.scale" is not a graphical parameter

Then I copied addTA from source (packageVersion('quantmod') 0.4.12) and tried some dirty modifications:

--- addTA       
+++ addTA.test  
@@ -1 +1,2 @@
+addTA.test <-
 function (ta, order = NULL, on = NA, legend = "auto", yrange = NULL, 
@@ -11,3 +12,3 @@
     else {
-        lchob <- get.current.chob()
+        lchob <- quantmod:::get.current.chob()
         chobTA <- new("chobTA")
@@ -41,3 +42,4 @@
             order = order, legend = legend, pars = list(list(...)), 
-            time.scale = [email protected])
+            time.scale = [email protected], log.scale = T)
+       [email protected] <- T
         return(chobTA)

Which also results in an error

> source ...
> addTA.test(rsi)
Error in (function (cl, name, valueClass)  : 
  'log.scale' is not a slot in class "chobTA"

How can I draw an indicator with logarithmic scale?


Solution

  • You could simply use the log function before passing into addTA

    library(quantmod)
    x <- getSymbols("AAPL", auto.assign=FALSE)
    rsi <- RSI(Cl(x))
    
    chartSeries(x, subset = "2017")
    addTA(log(rsi))
    
    # Newer, cleaner charts alternative:
    chart_Series(x, subset = "2017")
    add_TA(log(rsi))
    add_TA(rsi)