Search code examples
rquantmod

When I plot a chart using the quantmod package, I get multiple plots, instead of 1. How do I get only the last chart?


candleChart(NIFTY, multi.col = TRUE, theme = "white")
addEMA(n = 50, col = "red")
addEMA(n = 200, col = "green")

As I run this code, I get 3 charts. The first one simply plots it, then I get another one with the 50 EMA and then I get a third one with the the 200 EMA. How do I get simply the last chart? The charts I get are here.

The tail of the dataset I use is below:

      Date      Open     High      Low    Close Shares Traded Turnover (Rs. Cr)
  2018-10-03 10982.70 10989.05 10843.75 10858.25     398756507    21225.59
  2018-10-04 10754.70 10754.70 10547.25 10599.25     438202008    23711.57
  2018-10-05 10514.10 10540.65 10261.90 10316.45     625153832    25254.21
  2018-10-08 10310.15 10398.35 10198.40 10348.05     470279031    22130.94
  2018-10-09 10390.30 10397.60 10279.35 10301.05     443795275    18285.41
  2018-10-10 10331.85 10482.35 10318.25 10460.10     373844130    19592.59

then I use the code I mention above.


Solution

  • When you want to add an EMA or another TA indicator and you use addEMA (or addXXX) you basically tell the function to take the current chart and add the EMA to it. This will create a new chart. If you are using Rmarkdown or a notebook you will indeed a new plot appearing for every addXXX you use. If you just want 1 plot you need to add all the TA's to the candleChart call like this:

    candleChart(NIFTY, multi.col = TRUE, theme = "white", TA = c(addEMA(n = 50), addEMA(n = 200)))