R/BAV.R

Defines functions BAV

BAV <- function(ret_list, leverageFactor)
{
  parallel::mclapply(ret_list, function(rets) {

    ep <- xts::endpoints(rets)
    ep[ep < 1] <- 1
    ep[ep > nrow(rets)] <- nrow(rets)
    ep <- unique(ep)
    epDiff <- diff(ep)

    if(xts::last(epDiff)==1)
      ep <- ep[-length(ep)]

    allSMA <- list()
    for(i in 1:(length(ep)-12)) {
      smaSubset <- rets[c((ep[i]+1):ep[(i+12)]),]

      sharpes <- apply(smaSubset, 2, function(x) {
        mean(x, na.rm=TRUE)/stats::sd(x, na.rm=TRUE)}) %>%
        stats::na.omit() %>%
        t

      # 'last' to favor SMAs longer in length
      sma <- xts::xts(max.col(sharpes, ties.method = 'last'),
                      xts::last(zoo::index(smaSubset)))
      allSMA[[i]] <- sma
    }

    do.call(rbind, allSMA)

  })
}
causality-loop/mnmt documentation built on June 17, 2022, 5:14 a.m.