1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
# core addTA base functions
#
# written by Jeffrey A. Ryan
# Copyright 2008
# Distributed under the GPL 3 or later
`funToTA` <-
function(x,drop.arg=1) {
drop.arg <- if(any(drop.arg < 1)) {
1:length(formals(x))
} else -drop.arg
fun.args <- paste(names(formals(x))[drop.arg],'=',sapply(formals(x), deparse)[drop.arg],sep='')
fun.args <- paste(gsub('=$','',fun.args),collapse=',')
paste('add',deparse(substitute(x)),'(',fun.args,') {',collapse='',sep='')
}
shading <- function(x)
{
# to be used from addTA when passed a logical object or vector
# also from new addEvents function
#
# ex. rect(shading$start-spacing, par('usr')[3],
# shading$end-spacing, par('usr')[3])
if( !is.logical(x) )
warning('need logical object')
runs <- rle(as.logical(x))
list(
start=cumsum(runs$lengths)[which(runs$values)] - runs$lengths[which(runs$values)]+1,
end=cumsum(runs$lengths)[which(runs$values)]
)
}
# addTA {{{
`addTA` <-
function(ta, order=NULL, on=NA, legend='auto', yrange=NULL, ...) {
if(is.character(ta)) {
if(exists(ta)) {
plot(do.call(paste('add',ta,sep=''),list(...)))
} else stop(paste('no TA method found for',paste('add',ta,sep='')))
} else {
lchob <- get.current.chob()
chobTA <- new("chobTA")
if(any(is.na(on))) {
chobTA@new <- TRUE
} else {
chobTA@new <- FALSE
chobTA@on <- on
}
nrc <- NROW(lchob@xdata)
ta <- try.xts(ta, error=FALSE)
if(is.xts(ta)) {
x <- merge(lchob@xdata, ta, fill=ifelse(is.logical(ta),0,NA),join='left', retside=c(FALSE,TRUE))
} else {
if(NROW(ta) != nrc)
stop('non-xtsible data must match the length of the underlying series')
x <- merge(lchob@xdata, ta, join='left', retside=c(FALSE,TRUE))
}
if(is.logical(ta))
x <- as.logical(x, drop=FALSE) #identical to storage.mode(x)<-"logical"
chobTA@TA.values <- coredata(x)[lchob@xsubset,]
chobTA@name <- "chartTA"
chobTA@call <- match.call()
chobTA@params <- list(xrange=lchob@xrange,
yrange=yrange,
colors=lchob@colors,
spacing=lchob@spacing,
width=lchob@width,
bp=lchob@bp,
isLogical=is.logical(ta),
x.labels=lchob@x.labels,
order=order,legend=legend,
pars=list(list(...)),
time.scale=lchob@time.scale)
# if(is.null(sys.call(-1))) {
# TA <- lchob@passed.args$TA
# lchob@passed.args$TA <- c(TA,chobTA)
# lchob@windows <- lchob@windows + ifelse(chobTA@new,1,0)
# do.call('chartSeries.chob',list(lchob))
# #quantmod:::chartSeries.chob(lchob)
# invisible(chobTA)
# } else {
return(chobTA)
# }
}
}#}}}
# chartTA {{{
`chartTA` <-
function(x) {
spacing <- x@params$spacing
width <- x@params$width
x.range <- x@params$xrange
x.range <- seq(x.range[1],x.range[2]*spacing)
tav <- x@TA.values
if(x@new) {
# draw new sub-window
y.range <- if(is.null(x@params$yrange) || length(x@params$yrange) != 2) {
seq(min(tav * 0.975, na.rm = TRUE), max(tav * 1.05, na.rm = TRUE),
length.out=length(x.range))
} else seq(x@params$yrange[1],x@params$yrange[2],length.out=length(x.range))
plot(x.range,y.range,type='n',axes=FALSE,ann=FALSE)
coords <- par('usr')
rect(coords[1],coords[3],coords[2],coords[4],col=x@params$colors$area)
grid(NA,NULL,col=x@params$colors$grid.col)
}
pars <- x@params$pars[[1]]
pars <- lapply(pars,
function(x) {
len <- NCOL(tav)
if(length(x) < len) {
rep(list(x), length.out=len)
} else rep(list(x),length.out=len)
})
# pars <- x@params$pars#[[1]]
# pars <- lapply(pars, function(x) rep(x, length.out=NCOL(tav)))
col.order <- if(is.null(x@params$order)) {
1:NCOL(tav)
} else x@params$order
if(is.null(x@params$legend)) legend <- function(legend,text.col,...) {}
if(is.character(x@params$legend) && x@params$legend != "auto") {
legend("topleft", legend=x@params$legend, bty='n', y.intersp=0.95)
legend <- function(legend,text.col,...) { }
}
if(!x@new) {
legend <- function(legend,text.col,...) { list(legend=legend,text.col=text.col) }
}
#formals(legend) <- alist(legend=,text.col=,...=) #formals(graphics::legend) # all have the same formals now
legend.text <- list()
# possibly able to handle newTA functionality
if(is.null(x@params$legend.name)) x@params$legend.name <- deparse(x@call[-1][[1]])
x.pos <- 1 + spacing * (1:length(x.range))
if(NCOL(tav) == 1) {
tmp.pars <- lapply(pars,function(x) x[[1]][[1]])
if(x@params$isLogical) {
do.call('rect',c(list(x.pos[shading(tav)$start-1] - spacing/3), list(par('usr')[3]),
list(x.pos[shading(tav)$end-1] + spacing/3), list(par('usr')[4]), tmp.pars))
# do not add a legend name for background shading. probably better to have
# the labels in another routine
} else {
do.call('lines',c(list(seq(1,length(x.range),by=spacing)), list(tav), tmp.pars))
legend.text[[1]] <- legend('topleft',
legend=c(paste(x@params$legend.name,":"),sprintf("%.3f",last(na.omit(tav)))),
text.col=c(x@params$colors$fg.col,last(pars$col[[1]])),bty='n',y.intersp=0.95)
}
} else {
for(cols in col.order) {
tmp.pars <- lapply(pars,function(x) {
p <- try(x[[cols]][[cols]],silent=TRUE)
if(inherits(p, 'try-error')) {
stop("TA parameter length must equal number of columns", call.=FALSE)
} else p
}
)
do.call('lines',c(list(seq(1,length(x.range),by=spacing)), list(tav[,cols]), tmp.pars))
if(cols==1) {
legend.text[[cols]] <- legend('topleft',
legend=c(paste(x@params$legend.name,":")),
text.col=c(x@params$colors$fg.col,last(pars$col[[cols]])),bty='n',y.intersp=0.95)
}
# for each column, add colname: value
Col.title <- colnames(tav)[cols]
legend.text[[cols]] <- legend('topleft',
legend=c(rep('',cols),paste(Col.title,":",
sprintf("%.3f",last(na.omit(tav[,cols]))))),
text.col=pars$col[[cols]][cols],bty='n',y.intersp=0.95)
}
}
axis(2)
box(col=x@params$colors$fg.col)
invisible(legend.text)
} # }}}
# chartSetUp {{{
`chartSetUp` <-
function(x) {
spacing <- x@params$spacing
width <- x@params$width
x.range <- x@params$xrange
x.range <- seq(x.range[1],x.range[2]*spacing)
tav <- x@TA.values
if(x@new) {
y.range <- if(is.null(x@params$yrange) || length(x@params$yrange) != 2) {
seq(min(tav * 0.975, na.rm = TRUE), max(tav * 1.05, na.rm = TRUE),
length.out=length(x.range))
} else seq(x@params$yrange[1],x@params$yrange[2],length.out=length(x.range))
plot(x.range,y.range,type='n',axes=FALSE,ann=FALSE)
coords <- par('usr')
rect(coords[1],coords[3],coords[2],coords[4],col=x@params$colors$area)
grid(NA,NULL,col=x@params$colors$grid.col)
}
pars <- x@params$pars[[1]]
pars <- lapply(pars,
function(x) {
len <- NCOL(tav)
if(length(x) < len) {
rep(list(x), length.out=len)
} else rep(list(x),length.out=len)
})
# pars <- x@params$pars#[[1]]
# pars <- lapply(pars, function(x) rep(x, length.out=NCOL(tav)))
col.order <- if(is.null(x@params$order)) {
1:NCOL(tav)
} else x@params$order
if(is.null(x@params$legend)) legend <- function(legend,text.col,...) {}
if(is.character(x@params$legend) && x@params$legend != "auto") {
legend("topleft", legend=x@params$legend, bty='n', y.intersp=0.95)
legend <- function(legend,text.col,...) { }
}
if(!x@new) {
legend <- function(legend,text.col,...) { list(legend=legend,text.col=text.col) }
}
legend.text <- list()
# possibly able to handle newTA functionality
if(is.null(x@params$legend.name)) x@params$legend.name <- deparse(x@call[-1][[1]])
if(NCOL(tav) == 1) {
tmp.pars <- lapply(pars,function(x) x[[1]][[1]])
# if(x@params$isLogical) {
# do.call('rect',c(list(shading(tav)$start*spacing), list(par('usr')[3]),
# list(shading(tav)$end*spacing), list(par('usr')[4]), tmp.pars))
# } else
# do.call('lines',c(list(seq(1,length(x.range),by=spacing)), list(tav), tmp.pars))
legend.text[[1]] <- legend('topleft',
legend=c(paste(x@params$legend.name,":"),sprintf("%.3f",last(na.omit(tav)))),
text.col=c(x@params$colors$fg.col,last(pars$col[[1]])),bty='n',y.intersp=0.95)
} else {
for(cols in col.order) {
tmp.pars <- lapply(pars,function(x) x[[cols]][[cols]])
# do.call('lines',c(list(seq(1,length(x.range),by=spacing)), list(tav[,cols]), tmp.pars))
if(cols==1) {
legend.text[[cols]] <- legend('topleft',
legend=c(paste(x@params$legend.name,":")),
text.col=c(x@params$colors$fg.col,last(pars$col[[cols]])),bty='n',y.intersp=0.95)
}
# for each column, add colname: value
Col.title <- colnames(tav)[cols]
legend.text[[cols]] <- legend('topleft',
legend=c(rep('',cols),paste(Col.title,":",
sprintf("%.3f",last(na.omit(tav[,cols]))))),
text.col=pars$col[[cols]][cols],bty='n',y.intersp=0.95)
}
}
axis(2)
box(col=x@params$colors$fg.col)
invisible(legend.text)
} # }}}
# setTA {{{
`setTA` <-
function(type=c('chartSeries','barChart','candleChart')) {
if('chartSeries' %in% type) setDefaults(chartSeries,TA=listTA())
if('barChart' %in% type) setDefaults(barChart,TA=listTA())
if('candleChart' %in% type) setDefaults(candleChart,TA=listTA())
}# }}}
# unsetTA {{{
`unsetTA` <-
function(type=c('chartSeries','barChart','candleChart')) {
if('chartSeries' %in% type) setDefaults(chartSeries,TA=NULL)
if('barChart' %in% type) setDefaults(barChart,TA=NULL)
if('candleChart' %in% type) setDefaults(candleChart,TA=NULL)
}# }}}
# listTA {{{
`listTA` <-
function(dev) {
if(missing(dev)) dev <- dev.cur()
sapply(get.chob()[[dev]]@passed.args$TA,function(x) x@call)
} # }}}
chartNULL <- function(...) return(invisible(NULL))
|