我试图用quantmod在图表上绘制指标。我想对bbands函数进行一些调整,但即使我完全复制函数,我仍然会收到错误。这是我的步骤:
尝试使用此功能时会发生这种情况
Error in addBBands2() : could not find function "get.current.chob"
这是代码
addBBands2<- function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)
function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)
{
stopifnot("package:TTR" %in% search() || require("TTR", quietly = TRUE))
draw.options <- c("bands", "percent", "width")
draw <- draw.options[pmatch(draw, draw.options)]
lchob <- get.current.chob()
x <- as.matrix(lchob@xdata)
chobTA <- new("chobTA")
if (draw == "bands") {
chobTA@new <- FALSE
}
else {
chobTA@new <- TRUE
on <- NULL
}
xx <- if (is.OHLC(x)) {
cbind(Hi(x), Lo(x), Cl(x))
}
else x
bb <- BBands(xx, n = n, maType = maType, sd = sd)
chobTA@TA.values <- bb[lchob@xsubset, ]
chobTA@name <- "chartBBands"
chobTA@call <- match.call()
chobTA@on <- on
chobTA@params <- list(xrange = lchob@xrange, colors = lchob@colors,
color.vol = lchob@color.vol, multi.col = lchob@multi.col,
spacing = lchob@spacing, width = lchob@width, bp = lchob@bp,
x.labels = lchob@x.labels, time.scale = lchob@time.scale,
n = n, ma = maType, sd = sd, draw = draw)
return(chobTA)
}
虽然不知怎的,但它们并不相同,
> str(addBBands)
function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)
> str(addBBands2)
function (n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)
- attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 14 31 1 14 1 1 31
.. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x25eb9bf4>
答案 0 :(得分:2)
我不知道quantmod,但可能这是因为get.current.chob
未从包名称空间导出。您可以尝试使用quantmod:::get.current.chob
引用它。其他非导出函数可能会有很多这样的错误。
答案 1 :(得分:1)
您需要确保该函数具有正确的environment
集,以便它可以看到未导出的函数。
复制功能(保留环境):
addBBands2 <- addBBands
然后edit
那个。
或者在创建新功能后直接更改environment
。考虑:
f <- function() get.current.chob()
f()
Error in f() : could not find function "get.current.chob"
environment(f) <- environment(addBBands)
f()
An object of class "chob"
Slot "device":
...