首页 文章

R QUANTSTRAT - 应用信号时出错

提问于
浏览
0

我知道这个问题已经被问到了,但是这里发布的所有答案都不适用于我 . 我会回测一个简单的指标策略,但最终会出现以下错误:

Error in .xts(e, .index(e1), .indexCLASS = indexClass(e1), .indexFORMAT = indexFormat(e1),  : 
index length must match number of observations
In addition: Warning messages:
1: In match.names(column, colnames(data)) :
all columns not located in X1.runsum for bid.vol ask.vol vol bid.freq ask.freq freq bid.price ask.price price              
2: In min(j, na.rm = TRUE) :
no non-missing arguments to min; returning Inf
3: In max(j, na.rm = TRUE) :
no non-missing arguments to max; returning -Inf

我正在使用的数据显示买/卖量,总交易量,买/卖报价,报价和一秒钟的买卖交易数量:

> head(data)
                      bid.vol   ask.vol       vol bid.freq ask.freq freq bid.price ask.price   price 
2014-09-25 00:00:01 0.0000000 0.0722401 0.0722401        0        1    1        NA   408.110 408.110
2014-09-25 00:00:02 0.0423759 0.0430572 0.0854331        1        2    3   408.110   408.111 408.111
2014-09-25 00:00:03 0.0299792 0.1648549 0.1948341        1        4    5   408.106   408.112 408.112
2014-09-25 00:00:04 0.0000000 2.9369966 2.9369966        0        9    9   408.106   407.500 407.500
2014-09-25 00:00:05 0.0000000 0.0000000 0.0000000        0        0    0   408.106   407.500 407.500
2014-09-25 00:00:06 0.0000000 0.0000000 0.0000000        0        0    0   408.106   407.500 407.500

具有以下结构:

> str(data)
An ‘xts’ object on 2014-09-25 00:00:01/2014-10-01 23:59:50 containing:
  Data: num [1:603994, 1:9] 0 0.0424 0.03 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:9] "bid.vol" "ask.vol" "vol" "bid.freq" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: CET
  xts Attributes:  
 NULL
>

策略非常简单:当给定间隔的移动窗口上的运行总和高于或等于某个阈值时,应该有一个信号 .

运行总和的指标运作良好:

add.indicator(strategy.st, name = "runSum", arguments = list(x = quote(data$ask.vol), n = lookBackVol), label = "runsum")

bid.vol   ask.vol       vol bid.freq ask.freq freq bid.price ask.price   price X1.runsum
 2014-09-25 00:00:01 0.0000000 0.0722401 0.0722401        0        1    1        NA   408.110 408.110        NA
 2014-09-25 00:00:02 0.0423759 0.0430572 0.0854331        1        2    3   408.110   408.111 408.111        NA
 2014-09-25 00:00:03 0.0299792 0.1648549 0.1948341        1        4    5   408.106   408.112 408.112        NA
 2014-09-25 00:00:04 0.0000000 2.9369966 2.9369966        0        9    9   408.106   407.500 407.500        NA
 2014-09-25 00:00:05 0.0000000 0.0000000 0.0000000        0        0    0   408.106   407.500 407.500  3.217149
 2014-09-25 00:00:06 0.0000000 0.0000000 0.0000000        0        0    0   408.106   407.500 407.500  3.144909

但我没有得到的是为什么newc olumns被称为“ X1.runsum " and not only " runsum " as stated in parametr label = " runsum” . 这可能会在调用signal时引起命名问题:

> add.signal(strategy.st, name = "sigThreshold", arguments = list(column = "X1.runsum", threshold    = thresholdVol, relationship = "gte", cross = TRUE), label = "longEntry")

最终会出现一个错误,如开头所述 .

我试过这个:

  • 将add.signal中的名称从 column = X1.runsum 更改为 column = runsum - 没有帮助

  • 下载更新版本的RRT包 - 没有帮助

  • 也像 arguments = list(x = quote(data$ask.vol[,1]) 这样的指标功能 - 没有帮助

我不能使用标准的OHLC功能,因为我的数据包含的信息不仅仅是OHLC .

你能帮忙吗?

1 回答

  • 2

    错误在第二行:

    test <- applyIndicators(strategy.st, data)
    test <- applySignals(strategy.st, data)
    

    因为它称原始未更改的数据不包含任何指标 . 但请改用:

    test <- applyIndicators(strategy.st, data)
    test <- applySignals(strategy.st, test)
    

相关问题