R QUANTSTRAT - error when applying signal

526 views Asked by At

I know this question has already been asked, but all answers posted here did not work for me. I do backtest one simple one indicator strategy but which ends up with following error:

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

Data I am working with are showing bid / ask volume, total volume, bid / ask quote, quote, and number of bid / ask trades in one second:

> 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

with following structure:

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

Strategy is really simple: there should be a signal when the running sum over moving window of given interval is higher or equal to some threshold.

Indicator with running sum works well:

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

gives

                       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

but what I don't get is why the newc olumns is called "X1.runsum" and not only "runsum" as stated in parametr label = "runsum". And this may cause the naming problem when calling signal:

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

which ends up with an error as decribed at the beginning.

I tried this:

  • changing the name in add.signal from column = X1.runsum to column = runsum - did not help
  • downloading newer version of RRT package - did not help
  • also tricks in indicator function like arguments = list(x = quote(data$ask.vol[,1]) - did not help

I cannot use standard OHLC functions as my data contains more information than just OHLC.

Can you please help?

1

There are 1 answers

0
Steef Gregor On

Mistake was in the second line:

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

as it calls original unchanged data which do not contain any indicators. But use this instead:

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