#' @importFrom magrittr '%$%' '%T>%'
.datatable.aware = TRUE
if (getRversion() >= '2.15.1')
utils::globalVariables(c('.', 'Ret', 'Sig', 'Lag.3'), utils::packageName())
make_gann_sigs <- function()
{
symbols <- c('SPY', 'DIA', 'QQQ')
# just show EOW dates to avoid the scenario where you need to remake
# model data mid-week and gann sigs = 0 for FRIDAY but it's still WEDNESDAY
# or something
dates <- readRDS('data/dates.rds')
# make x in Sys.Date()-x a big number in case the market is down
# more than 3 weeks
prices <- lapply(symbols, function(x) {
quantmod::getSymbols(x, env = NULL, from = Sys.Date()-70, to = Sys.Date()+1)[,4]
}) %>%
do.call(what = xts::merge.xts) %>%
`names<-`(symbols)
rets <- prices %>%
.[xts::endpoints(., 'week')] %>%
TTR::ROC(type = 'discrete', na.pad = F)
gann_sigs <- parallel::mclapply(names(rets), function(x) {
rets[,x] %>%
`names<-`('Ret') %$%
cbind(., quantmod::Lag(Ret, 1:2)) %>%
cbind(., rowSums(sign(.)) == -3) %>%
.[,c(1,4)] %T>%
{.[is.na(.)] <- 0} %T>%
{names(.)[2] <- 'Sig'} %$%
cbind(Ret, diff(Sig) > 0) %T>% {.[!.] <- NA} %$%
cbind(Ret, Sig, quantmod::Lag(Sig, 3) %T>% {.[.==1] <- 0}) %$%
cbind(Ret, ifelse(is.na(Sig), Lag.3, Sig)) %>%
zoo::na.locf() %T>%
{names(.)[2] <- 'Sig'} %>%
.[,2] %>%
`names<-`(x)
}) %>%
do.call(what = cbind) %>%
PerformanceAnalytics::zerofill()
# in case you make gann_sigs the first week of the year, in that case
# dates$EOW would yield an xts with length 0
if (length(gann_sigs[dates$EOW]) == 0) {
gann_sigs <- xts::last(gann_sigs)
} else {
gann_sigs <- xts::last(gann_sigs[dates$EOW])
}
saveRDS(gann_sigs, file = 'data/gann_sigs.rds')
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.