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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Feb 8 13:45:14 CET 2010


Author: braverock
Date: 2010-02-08 13:45:14 +0100 (Mon, 08 Feb 2010)
New Revision: 235

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/man/addOrder.Rd
Log:
- implement addOrder fn

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2010-02-07 20:10:35 UTC (rev 234)
+++ pkg/quantstrat/R/orders.R	2010-02-08 12:45:14 UTC (rev 235)
@@ -66,7 +66,6 @@
     stop("stub function needs to be implemented")
 }
 
-# TODO addOrder
 #' add an order to the order book
 #' 
 #' 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.
@@ -76,19 +75,33 @@
 #' @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 qty 
-#' @param price 
+#' @param qty numeric quantity of the order
+#' @param price numeric price at which the order is to be inserted
 #' @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 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='' )
+addOrder <- function(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", statustime='' , delay=.00001)
 {
-    stop("stub function needs to be implemented")
     # get order book
+    orderbook <- getOrderBook(portfolio)
+    
+    #data quality checks
+    if(!is.numeric(qty)) stop (paste("Quantity must be numeric:",qty))
+    if(!is.numeric(price)) stop (paste("Price must be numeric:",price))
+    if(!length(grep(symbol,names(orderbook)))==1) stop(paste("symbol",symbol,"does not exist in portfolio",portfolio,"having symbols",names(orderbook)))
+    if(!length(grep(side,c('long','short')))==1) stop(paste("side:",side," must be one of 'long' or 'short'"))
+    if(!length(grep(ordertype,c("market","limit","stop")))==1) stop(paste("ordertype:",ordertype,' must be one of "market","limit",or "stop"'))
+    if(!length(grep(status,c("open", "closed", "canceled","replaced")))==1) stop(paste("order status:",status,' must be one of "open", "closed", "canceled", or "replaced"'))
+    
     # insert new order
+    order<-xts(c(qty, price, ordertype, side, status, statustime),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)
     # assign order book back into place (do we need a non-exported "put" function?)
+    assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
 }
 
 # TODO update an order

Modified: pkg/quantstrat/man/addOrder.Rd
===================================================================
--- pkg/quantstrat/man/addOrder.Rd	2010-02-07 20:10:35 UTC (rev 234)
+++ pkg/quantstrat/man/addOrder.Rd	2010-02-08 12:45:14 UTC (rev 235)
@@ -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="")}
+\usage{addOrder(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", statustime="", 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.
 
@@ -9,9 +9,10 @@
 \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{qty}{}
-\item{price}{}
+\item{qty}{numeric quantity of the order}
+\item{price}{numeric price at which the order is to be inserted}
 \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{statustime}{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}}



More information about the Blotter-commits mailing list