R / quantmod - BBands()和rund(EMA)计算之间的差异

时间:2015-08-14 23:12:19

标签: r quantmod

当我的点差计算大于或小于上/下布林带时,我正试图创建一个信号状态,但是我的计算是:

pair <- c("qqq","iwm")
start <-  "2014-08-08"
finish <- "2015-08-13"
stckY <- suppressWarnings(getSymbols(pair[1], from = start, to = finish, auto.assign = FALSE))
stckX <- suppressWarnings(getSymbols(pair[2], from = start, to = finish, auto.assign = FALSE))

adY <- Ad(stckY)
adX <- Ad(stckX)

logY <- log(adY)
logX <- log(adX)

spread <- cbind(logY, logX)
spread <- spread[complete.cases(spread),] #remove NAs
spread$dailyDiff <- spread[,1] - spread[,2]

ema <- EMA(spread[,1] - spread[,2], n=20)
spread$UpBand <- (runSD(ema, n=20) * 2) + ema
spread$LwBand <- ema - (runSD(ema, n=20) * 2)

chartSeries(spread$dailyDiff, up.col = "white", 
            theme = chartTheme("black"), line.type = "l")
addEMA(n = 20, col = "orange")
addBBands(n = 20, sd = 2, maType = "EMA")

与chartSeries显示屏上显示的波段值不匹配,我无法弄清楚原因?帮助文件指出不使用SMA可能会导致“不一致”,所以这可能是问题的根源? chartSeries也在使用EMA。

也许还有更好的方法可以解决这个问题?我不确定如何仅使用BBands()来引用上/下频段......

1 个答案:

答案 0 :(得分:1)

有两个问题:

  1. 当您采用原始系列的标准偏差时,您将采用移动平均线的标准差。
  2. BBands函数在sample = FALSE调用中使用runSD
  3. 这会复制图表中BBands函数的输出:

    ema <- EMA(spread$dailyDiff, n=20)
    spread$UpBand <- runSD(spread$dailyDiff, n=20, sample=FALSE) * 2 + ema
    spread$LwBand <- ema - runSD(spread$dailyDiff, n=20, sample=FALSE) * 2
    tail(spread)
    #            QQQ.Adjusted IWM.Adjusted   dailyDiff      UpBand     LwBand
    # 2015-08-06     4.704563     4.793060 -0.08849663 -0.06442705 -0.1381200
    # 2015-08-07     4.703295     4.786575 -0.08328008 -0.06687188 -0.1322478
    # 2015-08-10     4.714652     4.798267 -0.08361464 -0.06938022 -0.1267023
    # 2015-08-11     4.701752     4.789323 -0.08757113 -0.07421110 -0.1198771
    # 2015-08-12     4.705196     4.787408 -0.08221192 -0.07856667 -0.1126964
    # 2015-08-13     4.703566     4.784320 -0.08075361 -0.08283161 -0.1055975
    tail(BBands(spread$dailyDiff, n=20, maType="EMA"))
    #                    dn        mavg          up      pctB
    # 2015-08-06 -0.1381200 -0.10127351 -0.06442705 0.6733800
    # 2015-08-07 -0.1322478 -0.09955985 -0.06687188 0.7490178
    # 2015-08-10 -0.1267023 -0.09804126 -0.06938022 0.7516764
    # 2015-08-11 -0.1198771 -0.09704411 -0.07421110 0.7074403
    # 2015-08-12 -0.1126964 -0.09563152 -0.07856667 0.8931942
    # 2015-08-13 -0.1055975 -0.09421457 -0.08283161 1.0912769