如何使用具有 (n-1) 个标准偏差的 BBands 函数?
How to use BBands function with (n-1) standard deviation?
给出了来自 TTR 包的 BBands 函数的捕鸟数据和结果:
d1= 1:20
d1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
BBands(t1)[20,]
dn mavg up pctB
-1.0325626 10.5000000 22.0325626 0.9118772
现在是使用 sd 函数的手动波段。
> c(mean(t1)-sd(t1)*2,mean(t1)+sd(t1)*2)
[1] -1.33216 22.33216
区别是因为sd使用的是标准差的(n-1)法,而BBands使用的是N法
问题是如何让 BBands 函数使用 (n-1) 方法。
文档 here 没有列出这样的选项。
如果 BBands 不可能,谁能帮我做一个克隆 BBands 的函数,但标准差为 (n-1)。
函数
当你写函数BBands
(不带括号):
时,你得到了简单的函数代码
function (HLC, n = 20, maType, sd = 2, ...)
{
HLC <- try.xts(HLC, error = as.matrix)
if (NCOL(HLC) == 3) {
if (is.xts(HLC)) {
xa <- xcoredata(HLC)
HLC <- xts(apply(HLC, 1, mean), index(HLC))
xcoredata(HLC) <- xa
}
else {
HLC <- apply(HLC, 1, mean)
}
}
else if (NCOL(HLC) != 1) {
stop("Price series must be either High-Low-Close, or Close/univariate.")
}
maArgs <- list(n = n, ...)
if (missing(maType)) {
maType <- "SMA"
}
mavg <- do.call(maType, c(list(HLC), maArgs))
sdev <- runSD(HLC, n, sample = FALSE)
up <- mavg + sd * sdev
dn <- mavg - sd * sdev
pctB <- (HLC - dn)/(up - dn)
res <- cbind(dn, mavg, up, pctB)
colnames(res) <- c("dn", "mavg", "up", "pctB")
reclass(res, HLC)
}
我们换个函数
有函数runSD()
的变量sdev
,我把它改成了函数sdev <- as.vector(rollapplyr(HLC, n, sd))`,所以我们得到一个矢量输出。让我们调用这个新函数 BBands_2:
BBands_2 <- function (HLC, n = 20, maType, sd = 2, ...)
{
HLC <- try.xts(HLC, error = as.matrix)
if (NCOL(HLC) == 3) {
if (is.xts(HLC)) {
xa <- xcoredata(HLC)
HLC <- xts(apply(HLC, 1, mean), index(HLC))
xcoredata(HLC) <- xa
}
else {
HLC <- apply(HLC, 1, mean)
}
}
else if (NCOL(HLC) != 1) {
stop("Price series must be either High-Low-Close, or Close/univariate.")
}
maArgs <- list(n = n, ...)
if (missing(maType)) {
maType <- "SMA"
}
mavg <- do.call(maType, c(list(HLC), maArgs))
sdev <- as.vector(rollapplyr(HLC, n, sd))
up <- mavg + sd * sdev
dn <- mavg - sd * sdev
pctB <- (HLC - dn)/(up - dn)
res <- cbind(dn, mavg, up, pctB)
colnames(res) <- c("dn", "mavg", "up", "pctB")
reclass(res, HLC)
}
现在您只需在代码中复制该函数即可。您还需要激活 library(xts)
,当然还有 library(TTR)
结果
结果 BBands_2:
df <- 1:20
BBands_2(df)[20,]
dn mavg up pctB
-1.3321596 10.5000000 22.3321596 0.9014483
给出了来自 TTR 包的 BBands 函数的捕鸟数据和结果:
d1= 1:20
d1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
BBands(t1)[20,]
dn mavg up pctB
-1.0325626 10.5000000 22.0325626 0.9118772
现在是使用 sd 函数的手动波段。
> c(mean(t1)-sd(t1)*2,mean(t1)+sd(t1)*2)
[1] -1.33216 22.33216
区别是因为sd使用的是标准差的(n-1)法,而BBands使用的是N法
问题是如何让 BBands 函数使用 (n-1) 方法。 文档 here 没有列出这样的选项。
如果 BBands 不可能,谁能帮我做一个克隆 BBands 的函数,但标准差为 (n-1)。
函数
当你写函数BBands
(不带括号):
function (HLC, n = 20, maType, sd = 2, ...)
{
HLC <- try.xts(HLC, error = as.matrix)
if (NCOL(HLC) == 3) {
if (is.xts(HLC)) {
xa <- xcoredata(HLC)
HLC <- xts(apply(HLC, 1, mean), index(HLC))
xcoredata(HLC) <- xa
}
else {
HLC <- apply(HLC, 1, mean)
}
}
else if (NCOL(HLC) != 1) {
stop("Price series must be either High-Low-Close, or Close/univariate.")
}
maArgs <- list(n = n, ...)
if (missing(maType)) {
maType <- "SMA"
}
mavg <- do.call(maType, c(list(HLC), maArgs))
sdev <- runSD(HLC, n, sample = FALSE)
up <- mavg + sd * sdev
dn <- mavg - sd * sdev
pctB <- (HLC - dn)/(up - dn)
res <- cbind(dn, mavg, up, pctB)
colnames(res) <- c("dn", "mavg", "up", "pctB")
reclass(res, HLC)
}
我们换个函数
有函数runSD()
的变量sdev
,我把它改成了函数sdev <- as.vector(rollapplyr(HLC, n, sd))`,所以我们得到一个矢量输出。让我们调用这个新函数 BBands_2:
BBands_2 <- function (HLC, n = 20, maType, sd = 2, ...)
{
HLC <- try.xts(HLC, error = as.matrix)
if (NCOL(HLC) == 3) {
if (is.xts(HLC)) {
xa <- xcoredata(HLC)
HLC <- xts(apply(HLC, 1, mean), index(HLC))
xcoredata(HLC) <- xa
}
else {
HLC <- apply(HLC, 1, mean)
}
}
else if (NCOL(HLC) != 1) {
stop("Price series must be either High-Low-Close, or Close/univariate.")
}
maArgs <- list(n = n, ...)
if (missing(maType)) {
maType <- "SMA"
}
mavg <- do.call(maType, c(list(HLC), maArgs))
sdev <- as.vector(rollapplyr(HLC, n, sd))
up <- mavg + sd * sdev
dn <- mavg - sd * sdev
pctB <- (HLC - dn)/(up - dn)
res <- cbind(dn, mavg, up, pctB)
colnames(res) <- c("dn", "mavg", "up", "pctB")
reclass(res, HLC)
}
现在您只需在代码中复制该函数即可。您还需要激活 library(xts)
,当然还有 library(TTR)
结果
结果 BBands_2:
df <- 1:20
BBands_2(df)[20,]
dn mavg up pctB
-1.3321596 10.5000000 22.3321596 0.9014483