joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
798 stars 219 forks source link

Strange issue with add_TA on main panel (on=1) #356

Closed SamoPP closed 2 years ago

SamoPP commented 2 years ago

Description

add_TA for main panel (on=1) does not work and returns error. It works for second panel (on=2). Please see log and code below.

R version 4.1.3 (2022-03-10) -- "One Push-Up"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(quantmod)
Loading required package: xts
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
> 
> VIX <- getSymbols("^VIX", from="1970-01-01", auto.assign=FALSE)
> VIX3M <- getSymbols("^VIX3M", from="1970-01-01", auto.assign=FALSE)
> 
> myTheme = chart_theme()
> myTheme$col$line.col = "darkgreen"
> 
> indicator <- Cl(VIX)/Cl(VIX3M)
> colnames(indicator) <- "VIX.VIX3M.RATIO"
> 
> VIXVIX3MRatioThreshold <- 0.8
> signal <- indicator < VIXVIX3MRatioThreshold
> colnames(signal) <- "signal"
> signalForTA <- indicator[which(signal)]
> signalForChartSeries <- Cl(VIX)[which(signal)]
> 
> subset <- "2018/"
> chart_Series(Cl(VIX), type="line", theme=myTheme, subset=subset)
> add_TA(indicator, col="lightgreen")
> add_TA(signalForChartSeries, type="p", pch=20, col="black", on=1)
Error in plot.window(c(1, 1064), c(NaN, NaN)) : need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> add_TA(signalForTA, type="p", pch=20, col="black", on=2)
Error in plot.window(c(1, 1064), c(NaN, NaN)) : need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

Using add_TA just for second panel (on=1) by commenting out add_TA for panel (on=0) works and produces the graph.

> library(quantmod)
> 
> VIX <- getSymbols("^VIX", from="1970-01-01", auto.assign=FALSE)
> VIX3M <- getSymbols("^VIX3M", from="1970-01-01", auto.assign=FALSE)
> 
> myTheme = chart_theme()
> myTheme$col$line.col = "darkgreen"
> 
> indicator <- Cl(VIX)/Cl(VIX3M)
> colnames(indicator) <- "VIX.VIX3M.RATIO"
> 
> VIXVIX3MRatioThreshold <- 0.8
> signal <- indicator < VIXVIX3MRatioThreshold
> colnames(signal) <- "signal"
> signalForTA <- indicator[which(signal)]
> signalForChartSeries <- Cl(VIX)[which(signal)]
> 
> subset <- "2018/"
> chart_Series(Cl(VIX), type="line", theme=myTheme, subset=subset)
> add_TA(indicator, col="lightgreen")
> #add_TA(signalForChartSeries, type="p", pch=20, col="black", on=1)
> add_TA(signalForTA, type="p", pch=20, col="black", on=2)

Expected behavior

I expected that also black points would be plotted in first panel (on=1). Instead an error is thrown about NAs... See above.

Minimal, reproducible example

library(quantmod)

VIX <- getSymbols("^VIX", from="1970-01-01", auto.assign=FALSE)
VIX3M <- getSymbols("^VIX3M", from="1970-01-01", auto.assign=FALSE)

myTheme = chart_theme()
myTheme$col$line.col = "darkgreen"

indicator <- Cl(VIX)/Cl(VIX3M)
colnames(indicator) <- "VIX.VIX3M.RATIO"

VIXVIX3MRatioThreshold <- 0.8
signal <- indicator < VIXVIX3MRatioThreshold
colnames(signal) <- "signal"
signalForTA <- indicator[which(signal)]
signalForChartSeries <- Cl(VIX)[which(signal)]

subset <- "2018/"
chart_Series(Cl(VIX), type="line", theme=myTheme, subset=subset)
add_TA(indicator, col="lightgreen")
add_TA(signalForChartSeries, type="p", pch=20, col="black", on=1)
add_TA(signalForTA, type="p", pch=20, col="black", on=2)

Session Info

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
 [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8          
 [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantmod_0.4.18.1 TTR_0.24.3        xts_0.12.1.2      zoo_1.8-9        
[5] rj_4.0.5-4       

loaded via a namespace (and not attached):
[1] compiler_4.1.3  tools_4.1.3     rj.gd_4.0.5-1   curl_4.3.2     
[5] grid_4.1.3      lattice_0.20-45

I have also asked about this on StackOverflow: https://stackoverflow.com/questions/71614328/adding-points-to-main-panel-in-chart-series-fails-with-add-ta

pverspeelt commented 2 years ago

As mentioned in a comment on your SO question:

Your signalForChartSeries only has data until 2005. Hence you get this error message; you are trying to plot data outside of your plot area (2018 and further). If you change the subset to 2000/2005 the last line will work, but the other lines will give you errors because of the same reason.

SamoPP commented 2 years ago

Thanks. This is really embarrassing... Of course the code should have been:

signalForTA <- indicator[which(signal)]
signalForChartSeries <- Cl(VIX)[index(signal)[which(signal)]]