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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 8 21:22:26 CEST 2011


Author: gsee
Date: 2011-06-08 21:22:24 +0200 (Wed, 08 Jun 2011)
New Revision: 617

Modified:
   pkg/quantstrat/R/orders.R
Log:
BBO market orders are now filled at BBO instead of mid/close/trade/last price.

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2011-06-08 17:30:21 UTC (rev 616)
+++ pkg/quantstrat/R/orders.R	2011-06-08 19:22:24 UTC (rev 617)
@@ -407,7 +407,7 @@
     # check for open orders
     if (length(procorders)>=1){
         # get previous bar
-        prevtime  <- time(mktdata[last(mktdata[timespan, which.i = TRUE])-1])
+        prevtime  <- time(mktdata[last(mktdata[timespan, which.i = TRUE])-1]) #not used
         timestamp <- time(last(mktdata[timespan]))
         #switch on frequency
         freq = periodicity(mktdata)
@@ -439,11 +439,20 @@
                                     txntime=as.character(index(ordersubset[ii,])) # transacts on this bar, e.g. in the intraday cross, or leading into the end of month, quarter, etc.
                                     # txntime=as.character(timestamp) # use this if you wanted to transact on the close of the next bar
                                     txnprice=as.numeric(getPrice(last(mktdata[txntime]), ...=...))
-                                }, #end daily and lower frequency processing
-                                {
-                                    txnprice = as.numeric(getPrice(mktdataTimestamp))
-                                    #TODO extend this to figure out which side to prefer
+                                }, #end daily
+                                { 
+                                    #txnprice = as.numeric(getPrice(mktdataTimestamp)) #filled at 'price'
                                     txntime = timestamp
+                                    #An ordertype of market will *almost* trump pricemethod here. orderPrice was determined using pricemethod.
+                                    #but, for buy orders you'll be filled at either orderPrice or the current mkt ask -- whichever is worse.
+                                    #and, for sell orders you'll be filled at either orderPrice or the current mkt bid -- whichever is worse.
+                                    if(orderQty > 0){ # positive quantity 'buy'
+                                        #fill at max(orderPrice,newMktAsk price) 
+                                        txnprice = max(orderPrice, as.numeric(getPrice(mktdataTimestamp,prefer='ask')))
+                                    } else { # negative quantity 'sell'
+                                        txnprice = min(orderPrice, as.numeric(getPrice(mktdataTimestamp,prefer='bid'))) #presumes unique timestamp
+                                    }
+                                    #e.g. if pricemethod was opside, it sent a buy order at mktAsk. fill at greater of that ask, and current ask
                                 }) # end switch on frequency
                     },
                     limit= ,
@@ -454,8 +463,10 @@
                                 stop("iceberg orders not supported for OHLC data")
                             } 
                             # check to see if price moved through the limit
-                            if( orderPrice > as.numeric(Lo(mktdataTimestamp)) &
-                                orderPrice < as.numeric(Hi(mktdataTimestamp)) ) 
+                            # FIXME: should this be the same for buys and sells?
+                            if( orderPrice > as.numeric(Lo(mktdataTimestamp)) & #what about an offer entered way above the market?
+                                orderPrice < as.numeric(Hi(mktdataTimestamp)) ) #what about a bid entered way below the market? 
+                                #should be: if buy order price > Lo || sell order price < Hi
                             {
                                 txnprice = orderPrice
                                 txntime = timestamp
@@ -623,7 +634,8 @@
 # R (http://r-project.org/) Quantitative Strategy Model Framework
 #
 # Copyright (c) 2009-2011
-# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich 
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, 
+# Jeffrey Ryan, Joshua Ulrich, and Garrett See 
 #
 # This library is distributed under the terms of the GNU Public License (GPL)
 # for full details see the file COPYING



More information about the Blotter-commits mailing list