#' @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')
# make x in Sys.Date()-x a big number in case the market is down
# more than 3 weeks
prices <- suppressMessages(
rameritrade::td_priceHistory(symbols, startDate = Sys.Date()-70))
rets <- data.table::data.table(prices) %>%
data.table::dcast(date ~ ticker, value.var = 'close') %$%
{xts::xts(.[,-1], date)} %>%
.[,symbols] %>%
.[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>%
{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() %>%
xts::last()
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.