R QUANTSTRAT - 应用信号时出错

时间:2014-11-15 00:27:19

标签: r finance quantstrat

我知道这个问题已被提出,但此处发布的所有答案对我都不起作用。我做了一个简单的指标策略,但最终会出现以下错误:

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 ”而不仅仅是“ runsum ”,如参数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 个答案:

答案 0 :(得分:2)

错误在第二行:

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

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

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