Quantmod Oscillators - r

Utilizing the chartSeries function in the quantmod package, I want to modify the RSI oscillator. Given an xts object containing OHLC price data, here is the call that I am using:
chartSeries(plot_report[, 1:4],
name = substr(ticker, 1, nchar(ticker) - 4),
theme = chartTheme('white.mono', grid.col = NA),
TA = c(addRSI(n = 14, maType = "SMA")),
type = "line",
bar.type = 'ohlc',
major.ticks = 'months',
show.grid = FALSE,
log.scale = TRUE)
Generating this chart:
I have four questions:
How can I change the default color of blue to something else? I have tried: c(addRSI(n = 14, maType = "SMA", col = "black")). However, I get the "unused argument" error.
Can I draw horizontal lines in the oscillator panel? Traditional RSI's have a horizontal red line at a y-axis value of 70 and a horizontal green line at a y-axis value of 30 to indicate overbought/oversold levels.
Is it possible to plot another calculation as an oscillator line below the chart? I have some proprietary oscillators that I want to visualize instead of the RSI or any of the indicators in the TTR package.
How can I get involved in improving the quantmod charting functionality; is this project being actively maintained?

You can't. You would need to add ... to the arguments for addRSI and modify the body of the function to use ... appropriately. A work-around is to calculate RSI manually, then call addTA as done in the answer to Change line colors of technical indicators made by R quantmod TTR?.
Use addLines:
getSymbols("SPY"); chartSeries(SPY, TA="addRSI();addLines(h=c(30,70), on=2)")
Use addTA with the object containing your proprietary data.
See quantmod's GitHub page. Yes, it's actively maintained. The last update was pushed to CRAN a couple months ago.

Related

Suppress graph output of a function [duplicate]

I am trying to turn off the display of plot in R.
I read Disable GUI, graphics devices in R but the only solution given is to write the plot to a file.
What if I don't want to pollute the workspace and what if I don't have write permission ?
I tried options(device=NULL) but it didn't work.
The context is the package NbClust : I want what NbClust() returns but I do not want to display the plot it does.
Thanks in advance !
edit : Here is a reproducible example using data from the rattle package :)
data(wine, package="rattle")
df <- scale (wine[-1])
library(NbClust)
# This produces a graph output which I don't want
nc <- NbClust(df, min.nc=2, max.nc=15, method="kmeans")
# This is the plot I want ;)
barplot(table(nc$Best.n[1,]),
xlab="Numer of Clusters", ylab="Number of Criteria",
main="Number of Clusters Chosen by 26 Criteria")
You can wrap the call in
pdf(file = NULL)
and
dev.off()
This sends all the output to a null file which effectively hides it.
Luckily it seems that NbClust is one giant messy function with some other functions in it and lots of icky looking code. The plotting is done in one of two places.
Create a copy of NbClust:
> MyNbClust = NbClust
and then edit this function. Change the header to:
MyNbClust <-
function (data, diss = "NULL", distance = "euclidean", min.nc = 2,
max.nc = 15, method = "ward", index = "all", alphaBeale = 0.1, plotetc=FALSE)
{
and then wrap the plotting code in if blocks. Around line 1588:
if(plotetc){
par(mfrow = c(1, 2))
[etc]
cat(paste(...
}
and similarly around line 1610. Save. Now use:
nc = MyNbClust(...etc....)
and you see no plots unless you add plotetc=TRUE.
Then ask the devs to include your patch.

Avoid plotmo truncating predictor labels on plot axes

I'm plotting nice plots with the R language plotmo library, but he's truncating the main labels.
How can I avoid this?
The real name of the variable is tempMaxima_Maxima not tmpMxm_M.
plotmo(mars1, pt.col = 'steelblue1', do.par = F, smooth.col = 2, trace = -1)
Plotmo version 3.5.7 has a new argument to take care of this issue. Use prednames.abbreviate=FALSE for full (non-abbreviated) names in the graphs.
(Plotmo version 3.5.7 will be released in mid-April 2020.)

Quantmod addMACD() remove line plot

I was currently using Quantmod to visualize technical analysis of equity data. When I came across the addMACD() functions for adding MACD graph and it works fine, except when I need to visualize the histogram only instead of the line graph.
addMACD(fast = display$macdFast, slow = display$macdSlow, signal = display$macdSignal, histogram = TRUE)
After reading through the documentation, I cannot figure out a way to remove the line plot of MACD graph. Is is possible to remove line plot while retaining histogram plot for MACD?
I always seem have problems with creating complicated newTA objects with quantmod, but here is an SO example. I find it easier to use rtsplot. That is a plotting package for xts objects but based on the base plots.
Since the macd histogram is nothing but the difference between the macd and the macd signal you can create your own histogram values:
library(quantmod)
goog <- getSymbols("GOOGL", from = "2019-01-01", auto.assign = F)
goog_macd <- MACD(goog$GOOGL.Close)
goog_macd_his <- goog_macd$macd - goog_macd$signal
library(rtsplot)
layout(c(1,1,1,2))
rtsplot(goog, type = "candle")
rtsplot(goog_macd_his , type = "h", lwd = 2)

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.
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)))

How to overlay multiple TA in new plot using quantmod?

We can plot candle stick chart using chart series function chartSeries(Cl(PSEC)) I have created some custom values (I1,I2 and I3) which I want to plot together(overlay) outside the candle stick pattern. I have used addTA() for this purpose
chartSeries(Cl(PSEC)), TA="addTA(I1,col=2);addTA(I2,col=3);addTA(I3,col=4)")
The problem is that it plots four plots for Cl(PSEC),I1,I2 and I3 separately instead of two plots which I want Cl(PSEC) and (I1,I2,I3)
EDITED
For clarity I am giving a sample code with I1, I2 and I3 variable created for this purpose
library(quantmod)
PSEC=getSymbols("PSEC",auto.assign=F)
price=Cl(PSEC)
I1=SMA(price,3)
I2=SMA(price,10)
I3=SMA(price,15)
chartSeries(price, TA="addTA(I1,col=2);addTA(I2,col=3);addTA(I3,col=4)")
Here is an option which preserves largely your original code.
You can obtain the desired result using the option on=2 for each TA after the first:
library(quantmod)
getSymbols("PSEC")
price <- Cl(PSEC)
I1 <- SMA(price,3)
I2 <- SMA(price,10)
I3 <- SMA(price,15)
chartSeries(price, TA=list("addTA(I1, col=2)", "addTA(I2, col=4, on=2)",
"addTA(I3, col=5, on=2)"), subset = "last 6 months")
If you want to overlay the price and the SMAs in one chart, you can use the option on=1 for each TA.
Thanks to #hvollmeier who made me realize with his answer that I had misunderstood your question in the previous version of my answer.
PS: Note that several options are described in ?addSMA(), including with.col which can be used to select a specific column of the time series (Cl is the default column).
If I understand you correctly you want the 3 SMAs in a SUBPLOT and NOT in your main chart window.You can do the following using newTA.
Using your data:
PSEC=getSymbols("PSEC",auto.assign=F)
price=Cl(PSEC)
Now plotting a 10,30,50 day SMA in a window below the main window:
chartSeries(price['2016'])
newSMA <- newTA(SMA, Cl, on=NA)
newSMA(10)
newSMA(30,on=2)
newSMA(50,on=2)
The key is the argument on. Use on = NA in defining your new TA function, because the default value foron is 1, which is the main window. on = NA plots in a new window. Then plot the remaining SMAs to the same window as the first SMA. Style the colours etc.to your liking :-).
You may want to consider solving this task using plotting with the newer quantmod charts in the quantmod package (chart_Series as opposed to chartSeries).
Pros:
-The plots look cleaner and better (?)
-have more flexibility via editing the pars and themes options to chart_Series (see other examples here on SO for the basics of things you can do with pars and themes)
Cons:
-Not well documented.
PSEC=getSymbols("PSEC",auto.assign=F)
price=Cl(PSEC)
chart_Series(price, subset = '2016')
add_TA(SMA(price, 10))
add_TA(SMA(price, 30), on = 2, col = "green")
add_TA(SMA(price, 50), on = 2, col = "red")
# Make plot all at once (this approach is useful in shiny applications):
print(chart_Series(price, subset = '2016', TA = 'add_TA(SMA(price, 10), yaxis = list(0, 10));
add_TA(SMA(price, 30), on = 2, col = "purple"); add_TA(SMA(price, 50), on = 2, col = "red")'))

Resources