[Blotter-commits] r634 - in pkg/quantstrat: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 24 05:45:45 CEST 2011


Author: gsee
Date: 2011-06-24 05:45:45 +0200 (Fri, 24 Jun 2011)
New Revision: 634

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/man/getOrders.Rd
   pkg/quantstrat/man/updateOrders.Rd
Log:
-check sign of orderqty before replacing orders

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2011-06-23 20:12:56 UTC (rev 633)
+++ pkg/quantstrat/R/orders.R	2011-06-24 03:45:45 UTC (rev 634)
@@ -70,7 +70,7 @@
 #' @param side one of NULL, "long" or "short", default NULL 
 #' @param which.i if TRUE, return the row index numbers rather than the order rows matching the criteria, default FALSE
 #' @export
-getOrders <- function(portfolio,symbol,status="open",timespan=NULL,ordertype=NULL, side=NULL, which.i=FALSE)
+getOrders <- function(portfolio,symbol,status="open",timespan=NULL,ordertype=NULL, side=NULL, qtysign=NULL, which.i=FALSE)
 {
     #if(is.null(timespan)) stop("timespan must be an xts style timestring")
     # get order book
@@ -89,7 +89,8 @@
     indices <- which(#if(!is.null(timespan)) ordersubset[timespan,which.i=TRUE] else TRUE &
                      (if(!is.null(status)) ordersubset[,"Order.Status"]==status else TRUE) &
                      (if(!is.null(ordertype)) ordersubset[,"Order.Type"]==ordertype else TRUE) &
-                     (if(!is.null(side)) ordersubset[,"Order.Side"]==side else TRUE)
+                     (if(!is.null(side)) ordersubset[,"Order.Side"]==side else TRUE) &
+                     (if(!is.null(qtysign)) sign(as.numeric(ordersubset[,"Order.Qty"]))==qtysign else TRUE)
                     )
 
     if(isTRUE(which.i)){
@@ -98,7 +99,7 @@
         # extract
         ordersubset<-orderbook[[portfolio]][[symbol]][indices,]
         #subset by time
-        if(nrow(ordersubset)>1) ordersubset<-ordersubset[timespan]
+        if(nrow(ordersubset)>1 && !is.null(timespan)) ordersubset<-ordersubset[timespan]
         return(ordersubset)
     }
 }
@@ -260,10 +261,10 @@
     #handle order sets
     #get the order set if length(price)>1
     if(length(price)>1) {
-        order.set<-max(getOrders(portfolio=portfolio, symbol=symbol, status='open', timespan=timespan, ordertype=NULL, side=NULL,which.i=FALSE)$Order.Set)
+        order.set<-max(na.omit(getOrders(portfolio=portfolio, symbol=symbol, status='open', timespan=timespan, ordertype=NULL, side=NULL,which.i=FALSE)$Order.Set))
         if(is.na(order.set)) order.set<-1
     } else {    
-        order.set=NA
+        order.set=1
     }
 
     #set up the other parameters
@@ -289,8 +290,10 @@
         next()
     }
 
+    qtysign <- sign(qty)    
+    
     if(!isTRUE(return)){
-        if(isTRUE(replace)) updateOrders(portfolio=portfolio, symbol=symbol,timespan=timespan, ordertype=ordertype, side=side, oldstatus="open", newstatus="replaced", statustimestamp=timestamp)
+        if(isTRUE(replace)) updateOrders(portfolio=portfolio, symbol=symbol,timespan=timespan, ordertype=ordertype, side=side, qtysign=qtysign, oldstatus="open", newstatus="replaced", statustimestamp=timestamp)
         # get order book
         orderbook <- getOrderBook(portfolio)
         orderbook[[portfolio]][[symbol]]<-rbind(orderbook[[portfolio]][[symbol]],order)
@@ -325,7 +328,7 @@
 #' @param newstatus one of "open", "closed", "canceled", or "replaced"
 #' @param statustimestamp timestamp of a status update, will be blank when order is initiated 
 #' @export
-updateOrders <- function(portfolio, symbol, timespan, ordertype=NULL, side=NULL, oldstatus="open", newstatus, statustimestamp) 
+updateOrders <- function(portfolio, symbol, timespan, ordertype=NULL, side=NULL, qtysign=NULL, oldstatus="open", newstatus, statustimestamp) 
 { 
     #data quality checks
     if(!is.null(oldstatus) & !length(grep(oldstatus,c("open", "closed", "canceled","replaced")))==1) 
@@ -334,12 +337,14 @@
         stop(paste("new order status:",newstatus,' must be one of "open", "closed", "canceled", or "replaced"'))
     if(!is.null(side) & !length(grep(side,c('long','short')))==1) 
         stop(paste("side:",side," must be one of 'long' or 'short'"))
+    if(!is.null(qtysign) && (qtysign != -1 && qtysign != 1 && qtysign != 0))
+        stop(paste("qtysign:",qtysign," must be one of -1, 0, or 1"))
     #if(is.null(side)) side<-NA
     if(!is.null(ordertype) & is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","iceberg")))) 
         stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit","stoptrailing", or "iceberg"'))
     
     # need the ability to pass a range like we do in blotter
-    updatedorders<-getOrders(portfolio=portfolio, symbol=symbol, status=oldstatus, timespan=timespan, ordertype=ordertype, side=side,which.i=TRUE) 
+    updatedorders<-getOrders(portfolio=portfolio, symbol=symbol, status=oldstatus, timespan=timespan, ordertype=ordertype, side=side, qtysign=qtysign, which.i=TRUE) 
     if(length(updatedorders)>=1){
         # get order book 
         #TODO this gets the order book again after it was already retrieved by getOrdersByStatus.  
@@ -403,7 +408,7 @@
     
     # get open orders
     procorders=NULL
-    procorders<-getOrders(portfolio=portfolio, symbol=symbol, status="open", timespan=timespan, ordertype=ordertype,which.i=TRUE)
+    procorders<-getOrders(portfolio=portfolio, symbol=symbol, status="open", timespan=timespan, ordertype=ordertype, which.i=TRUE)
     # check for open orders
     if (length(procorders)>=1){
         # get previous bar

Modified: pkg/quantstrat/man/getOrders.Rd
===================================================================
--- pkg/quantstrat/man/getOrders.Rd	2011-06-23 20:12:56 UTC (rev 633)
+++ pkg/quantstrat/man/getOrders.Rd	2011-06-24 03:45:45 UTC (rev 634)
@@ -1,8 +1,10 @@
 \name{getOrders}
 \alias{getOrders}
 \title{get orders by time span, status, type, and side...}
-\usage{getOrders(portfolio, symbol, status="open", timespan, ordertype, side,
-    which.i=FALSE)}
+\usage{getOrders(portfolio, symbol, status = "open", timespan = NULL,
+                 ordertype = NULL, side = NULL, qtysign = NULL, 
+                which.i = FALSE)
+}
 \description{get orders by time span, status, type, and side}
 \details{This function exists so that other code can find open orders, potentially to update or cancel them.
 
@@ -15,4 +17,5 @@
 \item{timespan}{xts-style character timespan to be the period to find orders of the given status and ordertype}
 \item{ordertype}{one of NULL, "market","limit","stoplimit", "stoptrailing", or "iceberg" default NULL}
 \item{side}{one of NULL, "long" or "short", default NULL}
+\item{qtysign}{sign of orderqty: -1, 0, 1}
 \item{which.i}{if TRUE, return the row index numbers rather than the order rows matching the criteria, default FALSE}}

Modified: pkg/quantstrat/man/updateOrders.Rd
===================================================================
--- pkg/quantstrat/man/updateOrders.Rd	2011-06-23 20:12:56 UTC (rev 633)
+++ pkg/quantstrat/man/updateOrders.Rd	2011-06-24 03:45:45 UTC (rev 634)
@@ -1,8 +1,9 @@
 \name{updateOrders}
 \alias{updateOrders}
 \title{update an order or orders...}
-\usage{updateOrders(portfolio, symbol, timespan, ordertype, side,
-    oldstatus="open", newstatus, statustimestamp)}
+\usage{updateOrders(portfolio, symbol, timespan, ordertype = NULL, side =
+                 NULL, qtysign = NULL, oldstatus = "open", newstatus,
+                 statustimestamp)}
 \description{update an order or orders}
 \details{When an order gets filled, it should have its status moved to 'closed'.
 
@@ -19,6 +20,7 @@
 \item{timespan}{xts-style character timespan to be the period to find orders of the given status and ordertype}
 \item{ordertype}{one of NULL, "market","limit","stoplimit", or "stoptrailing" default NULL}
 \item{side}{one of NULL, "long" or "short", default NULL}
+\item{qtysign}{sign of orderqty: -1, 0, or 1}
 \item{oldstatus}{one of NULL, "open", "closed", "canceled", or "replaced", default "open"}
 \item{newstatus}{one of "open", "closed", "canceled", or "replaced"}
 \item{statustimestamp}{timestamp of a status update, will be blank when order is initiated}}



More information about the Blotter-commits mailing list