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>
quantstrat: A bug in rules.R for stoplimits?
2 messages · Ivan Popivanov, Joshua Ulrich
Hi Ivan, This was fixed in r1430 a few days ago. Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2013: Applied Finance with R | www.RinFinance.com On Sat, Apr 13, 2013 at 11:33 PM, Ivan Popivanov
<ivan.popivanov at gmail.com> wrote:
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))
}
}
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.