Skip to content

Instantly share code, notes, and snippets.

@romunov
Created June 5, 2017 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romunov/54e8ed40c9aab3b1688705a742e143e7 to your computer and use it in GitHub Desktop.
Save romunov/54e8ed40c9aab3b1688705a742e143e7 to your computer and use it in GitHub Desktop.
felix's problem with code
require(xts)
dailyip=readRDS(gzcon(url("https://dl.dropboxusercontent.com/s/kfxg1vi1s6hv6cg/dailyip.rds")))
dailyrx=readRDS(gzcon(url("https://dl.dropboxusercontent.com/s/of47enf79ugkq8c/dailyrx.rds")))
populate = function(x) {
if(unique(is.na(x))=='TRUE') return(x)
naseries=cbind(x,NA)[,2]
where=which(x<(yy))[1]
if(!is.na(where)) return(na.locf(cbind(x[1:where],naseries)[,1]))
if(is.na(where)) return(x)
}
populate2 = function(zz) {
do.call(cbind, lapply(as.list(zz), populate))
}
# variable part
yy <- -0.02
long=ifelse(dailyip==1,1,NA)
short=ifelse(dailyip==-1,-1,NA)
dailyrx2=do.call(rbind, lapply(split(dailyrx, 'months'), cumsum))
long_split <- split(long*dailyrx2, 'months') # check dimensions of the input, mispatched
short_split <- split(short*dailyrx2, 'months')
# split into month, and then into columns in populate2
stoploss_l <- do.call(rbind, lapply(long_split, populate2))
stoploss_s <- do.call(rbind, lapply(short_split, populate2))
rx4b <- xts(rowMeans(apply.monthly(stoploss_l, last), na.rm=T)+
rowMeans(apply.monthly(stoploss_s, last), na.rm=T),
index(apply.monthly(stoploss_l, last)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment