我对编码相当陌生,我正在尝试选择 r。我正在quantstrat
. 我在网上查看了具体细节,这帮助我清理了我的代码,但我也在尝试创建自定义函数,这就是问题开始的地方。我遵循规则>信号>指标层次结构,我知道问题是指标和我试图添加的一些自定义数据之间的组合mktdata
。我正在添加一个从 excel 中导入的 xls 格式的额外列,如下所示:
>PEDATA <- readxl::read_xls()
>headers <- c("Date","PEValues","lowpeindicator","highpeindicator","inpe")
>names(PEDATA) <- c(headers)
>mktdata$PEValues <- PEDATA$PEValues
>head(mktdata)
一旦我查看head
函数,我就知道数据已正确添加。然后我继续创建我的自定义指标,我只是在其中调整add.indicator
和SMA
函数。
>add.indicator(strategy.name, name = "SMA", arguments =
list(x=quote(PEValues(mktdata)), n = PEs), label = SMA1)
从这里开始,这一切一直持续到最后。一旦我尝试应用该策略,我会得到以下信息:
> applyStrategy(strategy = strategy.name, portfolios = portfolio.name)
Error in `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) :
attempt to set 'colnames' on an object with less than two dimensions
我已经运行traceback
并得到以下信息:
4: stop("尝试在小于二维的对象上设置 'colnames'") 3:
colnames<-
(*tmp*
, value = seq(ncol(tmp_val))) 2: applyIndicators(strategy = strategy, mktdata = mktdata, parameters = parameters , ...) 1: 应用策略(策略 = 策略名称,投资组合 = 投资组合名称)
为了得到PEValues
一些东西,我这样做了:
>has.PEValues<- function (x, which = FALSE)
>{
> colAttr <- attr(x, "PEValues")
> if (!is.null(colAttr))
> return(if (which) colAttr else TRUE)
> loc <- grep("PEValues", colnames(x), ignore.case = TRUE)
> if (!identical(loc, integer(0))) {
> return(if (which) loc else TRUE)
> }
> else FALSE
> }
>
>
>
> PEValues<- function (x)
> {
> if (has.inpe(x))
> return(x[, grep("PEValues", colnames(x), ignore.case = TRUE)])
> stop("subscript out of bounds: no column name containing
>\"PEValues\"")
> }
在这个阶段,老实说,我不知道该做什么,而且很多论坛仍然超出我的能力范围。任何帮助将不胜感激,我将非常乐意回答任何问题。
根据要求,使用样本数据dput(head(mktdata)
> dput(head(mktdata))
>structure(c(44.2187, 44.4062, 44.9687, 44.9687, 44.9687, 44.8125,
>44.375, 44.8437, 45.0937, 45.0625, 45.125, 44.8125, 44.125, 44.375,
>44.4687, 44.7187, 44.9062, 44.5625, 44.3437, 44.8125, 45, 44.9687,
>44.9687, 44.6562, 201300, 529400, 531500, 492100, 596100, 122100,
>27.362904, 27.652185, 27.767893, 27.748577, 27.748577, 27.555738,
>NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
>NA, NA, NA, NA, NA, NA, NA, NA, 22.55, 22.55, 22.55, 22.55, 22.55,
>22.55, 1, 1, 1, 1, 1, 1), class = c("xts", "zoo"), .indexCLASS = "Date",
>.indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated =
>structure(1544551441.04075, class = c("POSIXct",
>"POSIXt")), index = structure(c(728611200, 728697600, 728784000,
>728870400, 729129600, 729216000), tzone = "UTC", tclass = "Date"), .Dim =
>c(6L,12L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low",
>"SPY.Close", "SPY.Volume", "SPY.Adjusted", "SMA.SMA150", "SMA.SMA50",
>"opensma", "closesma", "PEValues", "inpe")))
head(mktdata)
> head(mktdata)
> SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
>SMA.SMA150 SMA.SMA50 opensma closesma PEValues inpe
>1993-02-02 44.2187 44.3750 44.1250 44.3437 201300 27.36290
>NA NA NA NA 22.55 1
>1993-02-03 44.4062 44.8437 44.3750 44.8125 529400 27.65218
>NA NA NA NA 22.55 1
>1993-02-04 44.9687 45.0937 44.4687 45.0000 531500 27.76789
>NA NA NA NA 22.55 1
>1993-02-05 44.9687 45.0625 44.7187 44.9687 492100 27.74858
>NA NA NA NA 22.55 1
>1993-02-08 44.9687 45.1250 44.9062 44.9687 596100 27.74858
>NA NA NA NA 22.55 1
>1993-02-09 44.8125 44.8125 44.5625 44.6562 122100 27.55574
>NA NA NA NA 22.55 1