Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created August 2, 2012 16:18
Show Gist options
  • Save timelyportfolio/3238341 to your computer and use it in GitHub Desktop.
Save timelyportfolio/3238341 to your computer and use it in GitHub Desktop.
horizon in quantmod
#code almost entirely from addRSI from quantmod package
# add_RSI {{{
add_RSI.test <- function (n=14, maType="EMA", ..., RSIup=70, RSIdn=30, horizonscale=25) {
require(RColorBrewer)
lenv <- new.env()
lenv$plot_rsi <- function(x, n, maType, ...) {
xdata <- x$Env$xdata
xsubset <- x$Env$xsubset
rsi <- RSI(Cl(xdata),n=n,maType=maType)[xsubset] - 50
x.pos <- 1:NROW(rsi)
theme <- x$Env$theme$rsi
# vertical grid lines
segments(axTicksByTime2(xdata[xsubset]),
par("usr")[3], #min(-10,range(na.omit(macd))[1]),
axTicksByTime2(xdata[xsubset]),
par("usr")[4], #max(10,range(na.omit(macd))[2]), col=x$Env$theme$grid)
col=x$Env$theme$grid)
#lines(x.pos, rep(RSIdn,length(x.pos)), col=theme$col$lines, lwd=1,lty=2,lend=2,...)
#lines(x.pos, rep(RSIup,length(x.pos)), col=theme$col$lines, lwd=1,lty=2,lend=2,...)
#lines(x.pos, rsi[,1], col=x$Env$theme$rsi$col$rsi, lwd=1.5,...)
col.brew <- brewer.pal(name="RdBu",n=10)
rsi.clean <- rsi[,1]
rsi.clean[is.na(rsi.clean[,1])] <- 0
nbands = ceiling(max(abs(rsi.clean))/horizonscale)
for (i in 1:nbands) {
#draw positive
polygon(
c(1,1:length(x.pos),length(x.pos)),
c(0,ifelse(rsi.clean > (i-1) * horizonscale, rsi.clean - (i-1) * horizonscale, 0),0),
col=col.brew[length(col.brew)-nbands+i-1],
border=NA
)
#draw negative
polygon(
c(1,1:length(x.pos),length(x.pos)),
c(0,ifelse(rsi.clean < 0 & abs(rsi.clean) > (i-1) * horizonscale, abs(rsi.clean) - (i-1) * horizonscale, 0),0),
col=col.brew[nbands-i+1],
border=NA
)
}
}
mapply(function(name,value) { assign(name,value,envir=lenv) },
names(list(n=n,maType=maType,...)),
list(n=n,maType=maType,...))
exp <- parse(text=gsub("list","plot_rsi",
as.expression(substitute(list(x=current.chob(),
n=n,maType=maType,...)))),
srcfile=NULL)
plot_object <- current.chob()
if(is.null(plot_object$Env$theme$rsi)) {
plot_object$Env$theme$rsi$col$rsi <- "saddlebrown"
plot_object$Env$theme$rsi$col$lines <- "orange2"
}
xsubset <- plot_object$Env$xsubset
rsi <- RSI(Cl(plot_object$Env$xdata),n=n,maType=maType)
plot_object$add_frame(ylim=c(0,1),asp=0.2)
plot_object$next_frame()
lenv$xdata <- structure(rsi,.Dimnames=list(NULL, "rsi"))
text.exp <- expression(text(c(1,
1+strwidth(paste("RSI(",n,"):",sep=""))),
0.3,
c(paste("RSI(",n,"):",sep=""),
round(last(xdata[xsubset]),5)),
col=c(1,theme$rsi$col$rsi),adj=c(0,0),cex=0.9,offset=0,pos=4))
#plot_object$add(expression(rect(par("usr")[1],0,par("usr")[2],1,col=theme$grid,border="black")),expr=TRUE)
plot_object$add(text.exp, env=c(lenv,plot_object$Env), expr=TRUE)
plot_object$add_frame(ylim=c(0,horizonscale),asp=1,fixed=TRUE)
plot_object$next_frame()
# add grid lines
lenv$grid_lines <- function(xdata,x) { c(RSIdn,RSIup) }
# add grid lines
exp <- c(expression(segments(1, grid_lines(xdata,xsubset),
NROW(xdata[xsubset]), grid_lines(xdata,xsubset), col=theme$grid)),exp,
# add axis labels/boxes
expression(text(1-1/3-max(strwidth(grid_lines(xdata,xsubset))),grid_lines(xdata,xsubset),
noquote(format(grid_lines(xdata,xsubset),justify="right")),
col=theme$labels,offset=0,pos=4,cex=0.9)),
expression(text(NROW(xdata[xsubset])+1/3,grid_lines(xdata,xsubset),
noquote(format(grid_lines(xdata,xsubset),justify="right")),
col=theme$labels,offset=0,pos=4,cex=0.9)))
plot_object$add(exp,env=c(lenv, plot_object$Env),expr=TRUE)
plot_object
} # }}}
require(quantmod)
getSymbols(IYR)
chart_Series(IYR)
add_RSIhorizon(horizonscale=25)
zoom_Chart("2012")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment