joshuaulrich / quantmod

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

issue with addMACD when "col" is specified #321

Closed nvalueanalytics closed 3 years ago

nvalueanalytics commented 3 years ago

Description

When explicitly passing colors to addMACD an error occurs (see examples below). After a bit of research I think the issue is with the vectorized function "ifelse" which evaluates the difference between the MACD line and the signal line. cols <- ifelse((macd[,1]-macd[,2]) > 0, col[1],col[2])

What is not clear to me is why, if I specify histogram = F no lines are generated either

Expected behavior

I should be able to specify custom colors (or other parameters) without affecting the function

Minimal, reproducible example

This

MACD = addMACD(col = c("#FF0000", "#008000", "#BBBBBB","#FF0000"))
plot(MACD)

or this

MACD = addMACD(histogram = T, col = c("#FF0000", "#008000", "#BBBBBB","#FF0000"))
plot(MACD)

cause the following

Error in ans[ypos] <- rep(yes, length.out = len)[ypos] : replacement has length zero In addition: Warning message: In rep(yes, length.out = len) : 'x' is NULL so the result will be NULL

However when using

MACD = addMACD(histogram = F, col = c("#FF0000", "#008000", "#BBBBBB","#FF0000"))
plot(MACD)

No error is generated. But also no lines in the chart

Session Info

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

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

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

other attached packages:
 [1] rmarkdown_2.3         googledrive_1.0.1     RobinHood_1.4         base64enc_0.1-3       httr_1.4.2
 [6] forecast_8.13         BatchGetSymbols_2.6.1 dplyr_1.0.1           rvest_0.3.6           xml2_1.3.2
[11] curl_4.3              jsonlite_1.7.0        stringi_1.4.6         quantmod_0.4.18       TTR_0.24.2
[16] xts_0.12-0            zoo_1.8-8             pacman_0.5.1

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  xfun_0.16         listenv_0.8.0     purrr_0.3.4       urca_1.3-0        lattice_0.20-41
 [7] colorspace_1.4-1  vctrs_0.3.2       generics_0.0.2    htmltools_0.5.0   utf8_1.1.4        rlang_0.4.7
[13] pillar_1.4.6      glue_1.4.1        lifecycle_0.2.0   timeDate_3043.102 munsell_0.5.0     gtable_0.3.0
[19] future_1.21.0     codetools_0.2-16  evaluate_0.14     knitr_1.29        tseries_0.10-48   lmtest_0.9-38
[25] parallel_4.0.2    fansi_0.4.1       Rcpp_1.0.5        scales_1.1.1      parallelly_1.22.0 fracdiff_1.5-1
[31] ggplot2_3.3.2     digest_0.6.25     grid_4.0.2        cli_2.0.2         quadprog_1.5-8    tools_4.0.2
[37] magrittr_1.5      tibble_3.0.3      crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.1    lubridate_1.7.9
[43] assertthat_0.2.1  rstudioapi_0.11   R6_2.4.1          globals_0.14.0    nnet_7.3-14       nlme_3.1-147
[49] compiler_4.0.2
joshuaulrich commented 3 years ago

Thanks for the report! This is fixed now. Please let me know if you run into any other issue(s).

nvalueanalytics commented 3 years ago

@joshuaulrich Thanks for addressing this, it no longer throws an error but the visualization is still broken (see attached screenshots) Basically when specifying col, and empty chart is created but no axis, lines, or tick-marks are added

addMACD()

no_param

addMACD(col = c("#FF0000", "#008000", "#BBBBBB","#FF0000"))

col_specified

Do you want me to open a new issue?

joshuaulrich commented 3 years ago

Hmm, it works for me...

library(quantmod)
getSymbols("JAN")
jan <- to.weekly(JAN)
chartSeries(jan, subset="2018/", theme=chartTheme("white"))
addBBands()
addMACD(col = c("#FF0000", "#008000", "#BBBBBB","#FF0000"))
packageVersion("quantmod")
# [1] '0.4.18.1'

Screenshot from 2021-01-02 13-01-01

Are you using the latest development version? Maybe there's something else in your environment that conflicts with the change I made? Try starting R with the --vanilla flag so none of your potential customizations are used.

nvalueanalytics commented 3 years ago

Weird... I'm also on v0.4.18.1. I'll try to figure it out on my end :)

Thanks for the quick response and for all the great work you've put into this

nvalueanalytics commented 3 years ago

Restarting R did the trick, now it works perfectly when plotting on standard output device but still not when putting the output to PDF... Will do some digging...Thanks again!

joshuaulrich commented 3 years ago

Glad that you got it working!

The PDF issue is probably because you need to plot/print the last call to a plotting function (like you do for lattice or ggplot). There should be some examples on stack overflow.