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
|
## adjRatiosR <- function(split, div, close){
## split[is.na(split)] <- 1
## #backpopulate the split ratio vector from the tail
## split <- rev(cumprod(rev(split)))
## coredata(split) <- c(coredata(split)[-1],1)
## div.index <- !is.na(div)
## stopifnot(is.na(div[1]))
## if(any(div.index)){
## div[div.index] <- 1-div[div.index]/coredata(close[which(div.index)-1])
## div[!div.index] <- 1
## div <- rev(cumprod(rev(div)))
## coredata(div) <- c(coredata(div)[-1],1)
## }else{
## coredata(div) <- rep(1,length(div))
## }
## return(try.xts(merge(split,div),error="bad things are going to happen"))
## }
adjRatios <- function (splits, dividends, close)
{
if (!missing(dividends) && missing(close))
stop("\"close\" must be specified to adjust dividends")
if (missing(close) || all(is.na(close)) || NROW(close) ==
0) {
close <- NA
}
else {
close <- try.xts(close, error = stop("\"as.xts(close)\" failed"))
}
if (missing(splits) || all(is.na(splits)) || NROW(splits) == 0) {
splits <- NA
}
else {
splits <- try.xts(splits, error = stop("\"as.xts(splits)\" failed"))
}
if (missing(dividends) || all(is.na(dividends))|| NROW(dividends) == 0) {
dividends <- NA
}
else {
dividends <- try.xts(dividends, error = stop("\"as.xts(dividends)\" failed"))
}
obj <- merge.xts(close, splits, dividends)
# for a non NA dividend value, close from the previous day must be non NA
#otherwise we can't compute the dividend ratio
if(any(is.na(obj[-NROW(obj),1][!is.na(obj[,3])[-1]]))){
for(i in which(!is.na(obj[,3]))){
if( is.na(obj[i-1,1]) ){
obj[i-1,1] <- last(na.omit(obj[1:(i-1),1]))
}
}
}
# if close is NA, then we can't compute the dividend ratio anway
#if (!isTRUE(is.na(close))) {
# obj <- obj[!is.na(obj[, 1]), ]
#}
adj <- .Call("adjRatios", obj[, 2], obj[, 3], obj[, 1], PACKAGE = "TTR")
adj <- xts(cbind(adj[[1]], adj[[2]]), index(obj))
colnames(adj) <- c("Split", "Div")
return(adj)
}
|