Optimize moving averages calculation - is it possi

2020-06-27 09:29发布

问题:

Is it possible to optimize (make it much faster) this piece of code:

out <- do.call(rbind,
        lapply(split(Cl(cumulativeBars), "days"), 
                function(x) {
                    previousFullBars <- barsEndptCl[as.Date(index(barsEndptCl), tz=indexTZ(barsEndptCl)) < as.Date(last(index(x)), tz=indexTZ(x)), ]
                    if (NROW(previousFullBars) >= 4) {
                        last(SMA(last(rbind(previousFullBars, x), n=6), n=5))
                    } else {
                        xts(NA, order.by=index(x))
                        }
                    }))

Below you can find my original question with all the code example that runs but a bit to slow for my needs.

ORIGINAL QUESTION:

After I was able to transform xts to lower frequency in cumulative way How to transform xts to lower frequency in a cumulative way thanks to people reading this list.

Now I am trying to calculate "evolution" of moving averages using code below. It is to slow for me. Can tis code (from # TODO: How to compute moving average?, the part starting with out <- do.call(rbind,lapply(split(Cl(cumulativeBars)...) be optimized in any way?

to.weekly.cumulative <- function(xts.obj, name="") {
    out <- do.call(rbind, 
            lapply(split(xts.obj, 'weeks'), 
                    function(x) cbind(rep(first(x[,1]), NROW(x[,1])), 
                                cummax(x[,2]),     cummin(x[,3]), x[,4])))
    colnames(out) <- paste(name, c("Open", "High", "Low", "Close"), sep=".")
    out
}

library(quantmod)
data(sample_matrix)
myxts <- as.xts(sample_matrix)

head(to.weekly.cumulative(myxts), 15)

# TODO: How to compute moving average?

# This SMA(Cl(to.weekly.cumulative(myxts)), n=5) would obviously be wrong 

cumulativeBars <- to.weekly.cumulative(myxts)

barsEndptCl <- Cl(cumulativeBars[endpoints(cumulativeBars, 'weeks')])
barsEndptCl <- Cl(to.weekly(myxts))

#all.equal(cumulativeBars[endpoints(cumulativeBars, 'weeks')], to.weekly(myxts))

out <- do.call(rbind,
        lapply(split(Cl(cumulativeBars), "days"), 
                function(x) {
                    previousFullBars <-     barsEndptCl[as.Date(index(barsEndptCl), tz=indexTZ(barsEndptCl)) < as.Date(last(index(x)),     tz=indexTZ(x)), ]
                    if (NROW(previousFullBars) >= 4) {
                                last(SMA(last(rbind(previousFullBars, x), n=6), n=5))
                    } else {
                        xts(NA, order.by=index(x))
                        }
                    }))

colnames(out) <- "SMA5"

out <- lag.xts(out, k=7)

chart_Series(to.weekly(myxts))
add_TA(SMA(to.weekly(myxts), 5), on=1, col="red")
add_TA(out, on=1, col="green")

回答1:

Moving Averages do not exactly "evolve" as you may be trying to ascertain. Being a Momentum Indicator, it's more about the Cycles than long-term "evolution".

You can definitely hop from Moving Average lines as you see the trend going a certain way. A 200-Day Trend was before a 190-Day Trend and before that, a 180-Day Trend. This may be a more manageable concept to try to build a trading system around.