``colnames <-`(`* tmp *`,value = seq(ncol(tmp_val)))中的错误:

时间:2018-12-10 22:48:31

标签: r quantstrat back-testing

我对编码还很陌生,所以我想选r。我正在quantstrat中进行回测。我在网上查看了具体内容,这有助于我清理代码,但我也尝试创建自定义函数,这就是问题的出处。我遵循规则>信号>指标层次结构,并且我知道问题是指标和我要添加到mktdata的一些自定义数据之间的组合。 我要添加一个以xls格式从excel导入的额外列,如下所示:

>PEDATA <- readxl::read_xls()

>headers <- c("Date","PEValues","lowpeindicator","highpeindicator","inpe")

>names(PEDATA) <- c(headers)

>mktdata$PEValues <- PEDATA$PEValues 

>head(mktdata)

当我查看head函数后,我知道数据已正确添加。然后,我继续创建我的自定义指标,在这里我只是在修改add.indicatorSMA函数。

>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*,值= seq(ncol(tmp_val)))   2:applyIndi​​cators(策略=策略,mktdata = mktdata,参数=参数,          ...)   1:applyStrategy(策略= strategy.name,投资组合= Portfolio.name)

要让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

0 个答案:

没有答案