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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Feb 8 19:49:06 CET 2010


Author: braverock
Date: 2010-02-08 19:49:06 +0100 (Mon, 08 Feb 2010)
New Revision: 238

Added:
   pkg/quantstrat/man/updateOrder.Rd
Modified:
   pkg/quantstrat/NAMESPACE
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/man/addOrder.Rd
   pkg/quantstrat/man/getOrderBook.Rd
   pkg/quantstrat/man/getOrdersByStatus.Rd
Log:
- implement updateOrder fn
- minor revisions of other order documentation

Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE	2010-02-08 14:55:19 UTC (rev 237)
+++ pkg/quantstrat/NAMESPACE	2010-02-08 18:49:06 UTC (rev 238)
@@ -2,9 +2,9 @@
 export(applyIndicators)
 export(match.names)
 export(getOrderBook)
-export(initOrders)
 export(getOrdersByStatus)
 export(addOrder)
+export(updateOrder)
 export(add.rule)
 export(applyRules)
 export(add.signal)

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2010-02-08 14:55:19 UTC (rev 237)
+++ pkg/quantstrat/R/orders.R	2010-02-08 18:49:06 UTC (rev 238)
@@ -1,6 +1,7 @@
 #' get the order book object
 #' 
-#' I don't think this should be exported. 
+#' I don't think this should be exported, but it is for now while we're in test mode.
+#' 
 #' @param portfolio text name of the portfolio the order book is associated with
 #' @export
 getOrderBook <- function(portfolio) #should symbol subsets be supported too?  probably not.
@@ -23,7 +24,6 @@
 #' @param portfolio text name of the portfolio to associate the order book with
 #' @param symbols a list of identfiers of the instruments to be contained in the Portfolio.  The name of any associated price objects (xts prices, usually OHLC) should match these
 #' @param initDate date (ISO8601) prior to the first close price given in mktdata, used to initialize the order book with a dummy order
-#' @export
 initOrders <- function(portfolio=NULL, symbols=NULL, initDate = '1999-12-31')
 {
     # NOTE we could stor all of these in one object, but I think that might get big
@@ -51,14 +51,14 @@
     assign(paste("order_book",portfolio,sep='.'),orders,envir=.strategy)
 }
 
-#TODO getOrdersByStatus
-#' get orders by status
+#' get orders by time span, status, and type
 #' 
 #' This function exists so that other code can find open orders, potentially to update or cancel them.
 #' 
 #' It has some use as a reporting or post-hoc analytics tool, but it may not always be exported.
 #' 
-#' should this be symbols in stead of symbol?
+#' should this be symbols instead of symbol?
+#' 
 #' @param portfolio text name of the portfolio to associate the order book with
 #' @param symbol identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC) should match these
 #' @param status one of "open", "closed", "canceled", or "replaced"
@@ -116,10 +116,10 @@
 #' @param ordertype one of "market","limit",or "stop"
 #' @param side one of either "long" or "short" 
 #' @param status one of "open", "closed", "canceled", or "replaced"
-#' @param statustime timestamp of a status update, will be blank when order is initiated 
+#' @param statustimestamp timestamp of a status update, will be blank when order is initiated 
 #' @param delay what delay to add to timestamp when inserting the order into the order book, in seconds
 #' @export
-addOrder <- function(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", statustime='' , delay=.00001)
+addOrder <- function(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", statustimestamp='' , delay=.00001)
 {
     # get order book
     orderbook <- getOrderBook(portfolio)
@@ -134,7 +134,7 @@
     # TODO do we need to check for collision, and increment timestamp?  or alternately update?
     
     # insert new order
-    order<-xts(c(qty, price, ordertype, side, status, statustime),order.by=(as.POSIXct(timestamp)+delay))
+    order<-xts(c(qty, price, ordertype, side, status, statustimestamp),order.by=(as.POSIXct(timestamp)+delay))
     colnames(order) <- c("Order.Qty","Order.Price","Order.Type","Order.Side","Order.Status","Order.StatusTime")
     orderbook[[symbol]]<-rbind(orderbook[[symbol]],order)
     
@@ -142,12 +142,50 @@
     assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
 }
 
-# TODO update an order
+#' update an order
+#' 
+#' When an order gets filled, it should have its status moved to 'closed'.
+#' 
+#' When an order is updated with a new order, the order status should change to 'replaced' with a StatusTime that is the same as the one for the new order.
+#' 
+#' When a risk event or over-limit event happens, typically open orders will be 'canceled'.  Possibly new orders will be added to close open positions.  
+#' Many models will also want to run a process at the close of market that will cancel all open orders. 
+#' 
+#' @param portfolio text name of the portfolio to associate the order book with
+#' @param symbol identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC) should match these
+#' @param timestamp timestamp coercible to POSIXct that will be the time the order will be inserted on 
+#' @param oldstatus one of NULL, "open", "closed", "canceled", or "replaced"
+#' @param newstatus one of "open", "closed", "canceled", or "replaced"
+#' @param statustimestamp timestamp of a status update, will be blank when order is initiated 
+#' @export
+updateOrder <- function(portfolio, symbol, timestamp, oldstatus, newstatus, statustimestamp) 
+{ 
+    #data quality checks
+    if(!is.null(oldstatus) & !length(grep(oldstatus,c("open", "closed", "canceled","replaced")))==1) stop(paste("old order status:",oldstatus,' must be one of "open", "closed", "canceled", or "replaced"'))
+    if(!length(grep(newstatus,c("open", "closed", "canceled","replaced")))==1) stop(paste("new order status:",newstatus,' must be one of "open", "closed", "canceled", or "replaced"'))
 
+    # need the ability to pass a range like we do in blotter
+    updatedorders<-getOrdersByStatus(portfolio, symbol, timestamp, oldstatus,starttime=-.00001)  
+    
+    
+    # get order book 
+    #TODO this gets the order book again after it was already retrieved by getOrdersByStatus.  
+    # at some point, we should eliminate the double get
+    orderbook <- getOrderBook(portfolio)
+    
+    updatedorders[,"Order.Status"]<-newstatus
+    updatedorders[,"Order.StatusTime"]<-statustimestamp
+    
+    #orderbook<-merge.xts(orderbook,updatedorders,join='left')
+    orderbook[index(updatedorders)]<-updatedorders
+
+    # assign order book back into place (do we need a non-exported "put" function?)
+    assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
+}
+
 # TODO ruleOrderProc
 # process orders at time t, generating transactions
 
-
 ###############################################################################
 # R (http://r-project.org/) Quantitative Strategy Model Framework
 #

Modified: pkg/quantstrat/man/addOrder.Rd
===================================================================
--- pkg/quantstrat/man/addOrder.Rd	2010-02-08 14:55:19 UTC (rev 237)
+++ pkg/quantstrat/man/addOrder.Rd	2010-02-08 18:49:06 UTC (rev 238)
@@ -1,7 +1,7 @@
 \name{addOrder}
 \alias{addOrder}
 \title{add an order to the order book...}
-\usage{addOrder(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", statustime="", delay=1e-05)}
+\usage{addOrder(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", statustimestamp="", delay=1e-05)}
 \description{add an order to the order book}
 \details{we need to figure out how to handle stop entry and stop exit orders, maybe via a negative price to specify the pullback that would trigger the order at the market.
 
@@ -14,5 +14,5 @@
 \item{ordertype}{one of "market","limit",or "stop"}
 \item{side}{one of either "long" or "short"}
 \item{status}{one of "open", "closed", "canceled", or "replaced"}
-\item{statustime}{timestamp of a status update, will be blank when order is initiated}
+\item{statustimestamp}{timestamp of a status update, will be blank when order is initiated}
 \item{delay}{what delay to add to timestamp when inserting the order into the order book, in seconds}}

Modified: pkg/quantstrat/man/getOrderBook.Rd
===================================================================
--- pkg/quantstrat/man/getOrderBook.Rd	2010-02-08 14:55:19 UTC (rev 237)
+++ pkg/quantstrat/man/getOrderBook.Rd	2010-02-08 18:49:06 UTC (rev 238)
@@ -3,5 +3,5 @@
 \title{get the order book object...}
 \usage{getOrderBook(portfolio)}
 \description{get the order book object}
-\details{I don't think this should be exported.}
+\details{I don't think this should be exported, but it is for now while we're in test mode.}
 \arguments{\item{portfolio}{text name of the portfolio the order book is associated with}}

Modified: pkg/quantstrat/man/getOrdersByStatus.Rd
===================================================================
--- pkg/quantstrat/man/getOrdersByStatus.Rd	2010-02-08 14:55:19 UTC (rev 237)
+++ pkg/quantstrat/man/getOrdersByStatus.Rd	2010-02-08 18:49:06 UTC (rev 238)
@@ -1,13 +1,13 @@
 \name{getOrdersByStatus}
 \alias{getOrdersByStatus}
-\title{get orders by status...}
+\title{get orders by time span, status, and type...}
 \usage{getOrdersByStatus(portfolio, symbol, status="open", timestamp, ordertype, starttime=-86400)}
-\description{get orders by status}
+\description{get orders by time span, status, and type}
 \details{This function exists so that other code can find open orders, potentially to update or cancel them.
 
 It has some use as a reporting or post-hoc analytics tool, but it may not always be exported.
 
-should this be symbols in stead of symbol?}
+should this be symbols instead of symbol?}
 \arguments{\item{portfolio}{text name of the portfolio to associate the order book with}
 \item{symbol}{identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC) should match these}
 \item{status}{one of "open", "closed", "canceled", or "replaced"}

Added: pkg/quantstrat/man/updateOrder.Rd
===================================================================
--- pkg/quantstrat/man/updateOrder.Rd	                        (rev 0)
+++ pkg/quantstrat/man/updateOrder.Rd	2010-02-08 18:49:06 UTC (rev 238)
@@ -0,0 +1,17 @@
+\name{updateOrder}
+\alias{updateOrder}
+\title{update an order...}
+\usage{updateOrder(portfolio, symbol, timestamp, oldstatus, newstatus, statustimestamp)}
+\description{update an order}
+\details{When an order gets filled, it should have its status moved to 'closed'.
+
+When an order is updated with a new order, the order status should change to 'replaced' with a StatusTime that is the same as the one for the new order.
+
+When a risk event or over-limit event happens, typically open orders will be 'canceled'.  Possibly new orders will be added to close open positions.  
+Many models will also want to run a process at the close of market that will cancel all open orders.}
+\arguments{\item{portfolio}{text name of the portfolio to associate the order book with}
+\item{symbol}{identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC) should match these}
+\item{timestamp}{timestamp coercible to POSIXct that will be the time the order will be inserted on}
+\item{oldstatus}{one of NULL, "open", "closed", "canceled", or "replaced"}
+\item{newstatus}{one of "open", "closed", "canceled", or "replaced"}
+\item{statustimestamp}{timestamp of a status update, will be blank when order is initiated}}


Property changes on: pkg/quantstrat/man/updateOrder.Rd
___________________________________________________________________
Name: svn:keywords
   + Revision Id Date Author



More information about the Blotter-commits mailing list