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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 22 23:37:35 CEST 2011


Author: gsee
Date: 2011-07-22 23:37:34 +0200 (Fri, 22 Jul 2011)
New Revision: 696

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/rules.R
Log:
some improvements to stoplimit order handling, but still not perfect

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2011-07-22 20:22:03 UTC (rev 695)
+++ pkg/quantstrat/R/orders.R	2011-07-22 21:37:34 UTC (rev 696)
@@ -483,20 +483,40 @@
                         } else if(isBBOmktdata){
                             # check side/qty
                             if(orderQty > 0){ # positive quantity 'buy'
-                                if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
-                                    # price we're willing to pay is higher than the offer price, so execute at the prevailing price
-                                    #txnprice = orderPrice
-                                    txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
-                                    txntime = timestamp
-                                } else next()
+                                if (orderType == 'stoplimit') {
+                                       if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
+                                        # mktprice moved above our stop buy price 
+                                        txnprice = orderPrice #assume we got filled at our stop price
+                                        #txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
+                                        txntime = timestamp
+                                       } else next()
+                                } else {
+                                    if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
+                                        # price we're willing to pay is higher than the offer price, so execute at the prevailing price
+                                        #txnprice = orderPrice
+                                        txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
+                                        txntime = timestamp
+                                    } else next()
+                                }
                             } else { # negative quantity 'sell'
-                                if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
-                                    # we're willing to sell at a better price than the bid, so execute at the prevailing price
-                                    # txnprice = orderPrice
-                                    txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
-                                    txntime = timestamp
-                                } else next()
+                                if (orderType == 'stoplimit') {
+                                    if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
+                                        # mktprice moved below our stop sell price
+                                        txnprice = orderPrice #assumption is that we're filled at our stop price
+                                        #txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
+                                        txntime = timestamp
+                                    } else next()
+                                } else {
+                                    if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
+                                        # we're willing to sell at a better price than the bid, so execute at the prevailing price
+                                        # txnprice = orderPrice
+                                        txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
+                                        txntime = timestamp
+                                    } else next()
+                               } 
                             }
+
+
                             if( orderType == 'iceberg'){
                                 #we've transacted, so the old order was closed, put in a new one
                                 neworder<-addOrder(portfolio=portfolio,

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2011-07-22 20:22:03 UTC (rev 695)
+++ pkg/quantstrat/R/rules.R	2011-07-22 21:37:34 UTC (rev 696)
@@ -334,6 +334,7 @@
         dindex<-get.dindex()
         #message(dindex," in nextIndex(), at ",curIndex)
 
+        hasmktord <- FALSE
         nidx=FALSE
         neworders=NULL
         
@@ -360,14 +361,53 @@
                                 
                     #if any type is market    
                     # set to curIndex+1
-                    curIndex<-curIndex+1
-                    if (is.na(curIndex) || curIndex > length(index(mktdata))) curIndex=FALSE
-                    return(curIndex) # move to next index, a market order in this index would have trumped any other open order
+                    #curIndex<-curIndex+1
+                    if (is.na(curIndex) || (curIndex + 1) > length(index(mktdata))) curIndex=FALSE
+                    hasmktord <- TRUE                    
+                    #return(curIndex) # move to next index, a market order in this index would have trumped any other open order
                 } 
                 if (!length(grep('limit',ordersubset[oo.idx,'Order.Type']))==0){ # process limit orders
                     #else limit
                     #print("limit")
+                    stoplimitorders <- grep('stoplimit',ordersubset[oo.idx,'Order.Type'])
                     limitorders<-grep('limit',ordersubset[oo.idx,'Order.Type'])
+                    limitorders <- limitorders[-stoplimitorders]
+
+                    for (slorder in stoplimitorders) {
+                        dindex <- get.dindex()
+                        tmpqty <- as.numeric(ordersubset[oo.idx[slorder],'Order.Qty'])
+                        tmpprice <- as.numeric(ordersubset[oo.idx[slorder],'Order.Price'])
+                        if (tmpqty > 0) { #buy if mktprice moves above stoplimitorder price
+                            relationship='gte'  #if the Ask or Hi go above threshold our stop will be filled
+                            if(isBBOmktdata) {
+                                col<-first(colnames(mktdata)[has.Ask(mktdata,which=TRUE)])
+                            } else if (isOHLCmktdata) {
+                                col<-first(colnames(mktdata)[has.Hi(mktdata,which=TRUE)])
+                            } else {
+                                # We should never hit this code, but it should help us find any edge cases
+                                # like perhaps we need a has.Price check
+                                stop("no price discernable for stoplimit in applyRules")
+                            }
+                        } else { #sell if mktprice moves below stoplimitorder price
+                            relationship="lte" #if Bid or Lo go below threshold, our stop will be filled
+                            if(isBBOmktdata) {
+                                col<-first(colnames(mktdata)[has.Bid(mktdata,which=TRUE)])
+                            } else if (isOHLCmktdata) {
+                                col<-first(colnames(mktdata)[has.Lo(mktdata,which=TRUE)])
+                            } else {
+                                # We should never hit this code, but it should help us find any edge cases
+                                stop("no price discernable for stoplimit in applyRules")
+                            }
+                        } 
+                        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] 
+                            # insert that into dindex
+                            assign.dindex(c(get.dindex(),newidx))                  
+                        }
+                    }
+
                     for (lorder in limitorders){
                         dindex<-get.dindex()
                         tmpqty<-as.numeric(ordersubset[oo.idx[lorder],'Order.Qty'])
@@ -488,7 +528,7 @@
                 } # end if for trailing orders
             } # end else clause for any open orders in this timespan    
         } # end any open orders closure
-        if(nidx) { #This will never evaluate to TRUE; if it has a purpose, it's a bug. -gsee
+        if(hasmktord) { 
             curIndex <- curIndex+1
             dindex<-get.dindex()
         } else {



More information about the Blotter-commits mailing list