[Blotter-commits] r1442 - pkg/quantstrat/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Apr 20 13:45:09 CEST 2013


Author: bodanker
Date: 2013-04-20 13:45:09 +0200 (Sat, 20 Apr 2013)
New Revision: 1442

Modified:
   pkg/quantstrat/R/rules.R
Log:
- fix to update dindex if stoplimit crosses but any(move_order) is FALSE


Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2013-04-19 14:04:05 UTC (rev 1441)
+++ pkg/quantstrat/R/rules.R	2013-04-20 11:45:09 UTC (rev 1442)
@@ -500,6 +500,7 @@
                     mkt_price_series <-getPrice(mktdata[timespan],prefer=prefer)[-1]  # don't look for crosses on curIndex
                     col<-first(colnames(mkt_price_series))
 
+                    # check if order needs to be moved
                     if(tmpqty > 0){ # positive quantity 'buy'
                         move_order <- tmpprice - abs(orderThreshold) > mkt_price_series
                         relationship="gte"
@@ -507,28 +508,14 @@
                         move_order <- tmpprice + abs(orderThreshold) < mkt_price_series
                         relationship="lte"
                     }
+                    # check if order will be filled
+                    cross <- sigThreshold(data=mkt_price_series, label='tmptrail',column=col,threshold=tmpprice,relationship=relationship)
                     tmpidx<-NULL
-                    if(any(move_order)){
-                        #print(firsttime)
-                        # find first index where we would move an order
-                        orderidx<-first(which(move_order)) 
-                        tmpidx <- format(index(move_order[orderidx,]), "%Y-%m-%d %H:%M:%OS6")
-                        trailspan <- paste(format(firsttime, "%Y-%m-%d %H:%M:%OS6"),"::",tmpidx,sep='')
-                        #make sure we don't cross before then 
-                        # use sigThreshold
-                        cross<-sigThreshold(data=mkt_price_series, label='tmptrail',column=col,threshold=tmpprice,relationship=relationship)
-                        cross <- cross[trailspan]  # don't look for crosses on curIndex (curIndex is removed from mkt_price_series)
-                        # find first index that would cross after this index
-                        if (any(cross)){
-                            newidx <- curIndex + index(mktdata[index(which(cross)[1]),which.i=TRUE])
-                            # insert that into dindex
-                            assign.dindex(c(get.dindex(),newidx))
-                        } else {
-                            #if we don't cross, do this
-                            moveidx <- curIndex + orderidx
-                            assign.dindex(c(get.dindex(),moveidx))
-                        }    
-                    } # end any(move_order) check                            
+                    # update dindex if order is moved or filled
+                    if(any(move_order) || any(cross)){
+                        moveidx <- curIndex + min(which(move_order)[1], which(cross)[1], na.rm=TRUE)
+                        assign.dindex(c(get.dindex(), moveidx))
+                    }
                 } # end loop over open trailing orders
             } # end else clause for any open orders in this timespan    
         } # end any open orders closure



More information about the Blotter-commits mailing list