[R-SIG-Finance] quantstrat: A bug in rules.R for stoplimits?
Ivan Popivanov
ivan.popivanov at gmail.com
Sun Apr 14 06:33:54 CEST 2013
The code which decides the next index for stoplimit orders looks like:
cross<-sigThreshold(label='tmpstop',column=col,threshold=tmpprice,relationship=relationship)
if(any(cross[timespan])){
# find first index that would cross after this index
newidx <- curIndex + which(cross[timespan])[1] - 1
# insert that into dindex
assign.dindex(c(get.dindex(),newidx))
}
To me, what the above says is: if we have a cross, set the new index to the
first cross. However, the first cross could be the timestamp corresponding
to the current index (easiest to repro by hacking the Low price for a long
stoplimit). If that's the case, newidx is set to the current index, in
other words, the stoplimit is not respected until the next date we decide
to stop (from the vectorization of the math) for another reason.
In the attached file, I have provided a possible fix and the original file,
for easy diff.
Regards,
Ivan
My fix:
cross<-sigThreshold(label='tmpstop',column=col,threshold=tmpprice,relationship=relationship)
if(any(cross[timespan])) {
# find first index that would cross after this index
crosses = which(cross[timespan])
if(crosses[1] != 1) {
# The first cross is not on the current index
newidx <- curIndex + crosses[1] - 1
} else if(length(crosses) > 1) {
# The first cross is on the current index, but
there is another index
newidx <- curIndex + crosses[2] - 1
} else {
# Only one cross and it's on the current index
newidx <- NA
}
if(!is.na(newidx)) {
# insert that into dindex
assign.dindex(c(get.dindex(),newidx))
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20130414/1902a195/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fix.tar.gz
Type: application/x-gzip
Size: 20164 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20130414/1902a195/attachment.gz>
More information about the R-SIG-Finance
mailing list