优化移动平均线计算-可以吗? [英] Optimize moving averages calculation - is it possible?

查看:90
本文介绍了优化移动平均线计算-可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以优化(使其更快)这段代码:

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.

原始问题:

在我能够以累积方式将xts转换为较低频率后

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.

现在,我正在尝试使用下面的代码来计算移动平均线的演变".这对我来说太慢了.可以以任何方式优化代码(从#TODO:如何计算移动平均吗?,以<-do.call(rbind,lapply(split(Cl(cum(ativeBars)...) >

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")

推荐答案

移动平均数并不像您尝试确定的那样完全演变".作为动量指标,与其说是周期,不如说是长期的演变".

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".

当趋势出现一定趋势时,您绝对可以从移动平均线跳来跳去. 200天趋势先于190天趋势,然后才是180天趋势.尝试建立一个交易系统可能是一个更易于管理的概念.

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.

这篇关于优化移动平均线计算-可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆