【问题标题】:R QUANTSTRAT - error when applying signalR QUANTSTRAT - 应用信号时出错
【发布时间】:2014-11-15 00:27:19
【问题描述】:

我知道已经有人问过这个问题,但是这里发布的所有答案都对我不起作用。我回测了一个简单的单指标策略,但最终出现以下错误:

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”,如参数标签 =“runsum”中所述”。这可能会导致调用信号时的命名问题:

> 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。

你能帮忙吗?

【问题讨论】:

  • 你确定你安装了latest TTR from R-forge
  • 是的,我有。不过,现在发现了错误。如果有人犯了和我一样的错误,我会发布解决方案。

标签: r finance quantstrat


【解决方案1】:

错误在第二行:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多