<div dir="ltr">The code which decides the next index for stoplimit orders looks like:<div><br><div>cross<-sigThreshold(label='tmpstop',column=col,threshold=tmpprice,relationship=relationship)<br></div><div><div>
if(any(cross[timespan])){</div><div> # find first index that would cross after this index</div><div> newidx <- curIndex + which(cross[timespan])[1] - 1</div><div> # insert that into dindex</div>
<div> assign.dindex(c(get.dindex(),newidx))</div><div> }</div></div><div><br></div><div style>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.</div>
</div><div style><br></div><div style>In the attached file, I have provided a possible fix and the original file, for easy diff.</div><div style><br></div><div style>Regards,</div><div style>Ivan</div><div style><br></div>
<div style>My fix:</div><div style><br></div><div style><div> cross<-sigThreshold(label='tmpstop',column=col,threshold=tmpprice,relationship=relationship)</div><div> if(any(cross[timespan])) {</div>
<div> # find first index that would cross after this index</div><div> crosses = which(cross[timespan])</div><div> if(crosses[1] != 1) {</div><div> # The first cross is not on the current index</div>
<div> newidx <- curIndex + crosses[1] - 1 </div><div> } else if(length(crosses) > 1) {</div><div> # The first cross is on the current index, but there is another index</div>
<div> newidx <- curIndex + crosses[2] - 1 </div><div> } else {</div><div> # Only one cross and it's on the current index</div><div> newidx <- NA</div>
<div> } </div><div> if(!<a href="http://is.na">is.na</a>(newidx)) {</div><div> # insert that into dindex</div><div> assign.dindex(c(get.dindex(),newidx))</div>
<div> } </div><div> } </div></div></div>