Skip to content

Instantly share code, notes, and snippets.

@yakneens
Created May 16, 2018 13:23
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 yakneens/85cd50a4f0860ea95e468678e87ca4ef to your computer and use it in GitHub Desktop.
Save yakneens/85cd50a4f0860ea95e468678e87ca4ef to your computer and use it in GitHub Desktop.
count_outcomes_by_condition <- function(my_data, cond){
ret = apply.weekly(na.omit(my_data), function(r){sum(eval(parse(text=cond)))})
names(ret)[1] = cond
return(ret)
}
xts2df <- function(x) {
res_qqq_df = xts2df(res_qqq)
data.frame(date=index(x), coredata(x))
}
conds = list("r < -0.01", "r>=-0.01 & r <= -0.002", "r>-0.002 & r < 0.002", "r>=0.002 & r<=0.01", "r>0.01")
res_qqq = do.call(cbind,lapply(conds,count_outcomes_by_condition,my_data=qqq_xts$Returns))
names(res_qqq) = c("red", "slightly_red", "flat", "slightly_green", "green")
summary(res_qqq)
res_qqq_df = xts2df(res_qqq)
res_qqq_long = melt(res_qqq_df, measure.vars=names(res_qqq))
ggplot(res_qqq_long, aes(value)) +
geom_histogram(bins = 5) +
facet_wrap(~variable, scales = 'free_x')
sum(apply(res_qqq,1,function(x){all(x > 0)})) / dim(res_qqq)[1]
week_types_qqq = table(as.factor(apply(res_qqq, 1, paste0, collapse="")))
barplot(week_types_qqq[week_types_qqq>10],las=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment