[Blotter-commits] r240 - in pkg/quantstrat: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Feb 9 18:17:18 CET 2010
Author: braverock
Date: 2010-02-09 18:17:18 +0100 (Tue, 09 Feb 2010)
New Revision: 240
Added:
pkg/quantstrat/man/getOrders.Rd
pkg/quantstrat/man/updateOrders.Rd
Modified:
pkg/quantstrat/R/orders.R
Log:
- change updateOrder to updateOrders
- change getOrdersByStatus to getOrders
- add replace option to addOrder
- change .Rd file names
Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R 2010-02-09 13:08:22 UTC (rev 239)
+++ pkg/quantstrat/R/orders.R 2010-02-09 17:17:18 UTC (rev 240)
@@ -51,7 +51,7 @@
assign(paste("order_book",portfolio,sep='.'),orders,envir=.strategy)
}
-#' get orders by time span, status, and type
+#' get orders by time span, status, type, and side
#'
#' This function exists so that other code can find open orders, potentially to update or cancel them.
#'
@@ -61,12 +61,13 @@
#'
#' @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"
+#' @param status one of "open", "closed", "canceled", or "replaced", default "open"
#' @param timestamp timestamp coercible to POSIXct that will be the period to find orders of the given status and ordertype
-#' @param ordertype one of "market","limit",or "stop"
+#' @param ordertype one of NULL, "market","limit",or "stop", default NULL
+#' @param side one of NULL, "long" or "short", default NULL
#' @param starttime difference to current timestamp to search, in seconds(numeric) or as a POSIXct timestamp, defaults to -86400 (one day)
#' @export
-getOrdersByStatus <- function(portfolio,symbol,status="open",timestamp=NULL,ordertype=NULL,starttime=-86400)
+getOrders <- function(portfolio,symbol,status="open",timestamp=NULL,ordertype=NULL, side=NULL, starttime=-86400)
{
# get order book
orderbook <- getOrderBook(portfolio)
@@ -99,11 +100,17 @@
if(!is.null(ordertype)) {
orderset<-orderset[which(orderset[,"Order.Type"==ordertype])]
}
+ if(!is.null(side)) {
+ orderset<-orderset[which(orderset[,"Order.Side"==side])]
+ }
return(orderset)
}
#' add an order to the order book
#'
+#' By default, this function will locate and replace any 'open' order(s)
+#' on the requested portfolio/symbol that have the same type and side.
+#'
#' 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.
#'
#' trailing stops should be modeled with replaced orders as prices change
@@ -115,11 +122,12 @@
#' @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 status one of "open", "closed", "canceled", or "replaced", default "open"
+#' @param replace TRUE/FALSE, whether to replace any other open order(s) on this portfolio symbol, default TRUE
#' @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", statustimestamp='' , delay=.00001)
+addOrder <- function(portfolio, symbol, timestamp, qty, price, ordertype, side, status="open", replace=TRUE, statustimestamp='' , delay=.00001)
{
# get order book
orderbook <- getOrderBook(portfolio)
@@ -133,6 +141,7 @@
if(!length(grep(status,c("open", "closed", "canceled","replaced")))==1) stop(paste("order status:",status,' must be one of "open", "closed", "canceled", or "replaced"'))
# TODO do we need to check for collision, and increment timestamp? or alternately update?
+ if(isTRUE(replace)) updateOrders(portfolio=portfolio, symbol=symbol,timestamp=timestamp, ordertype=ordertype, side=side, oldstatus="open", newstatus="replaced", statustimestamp=timestamp)
# insert new order
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")
@@ -142,7 +151,7 @@
assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
}
-#' update an order
+#' update an order or orders
#'
#' When an order gets filled, it should have its status moved to 'closed'.
#'
@@ -153,19 +162,23 @@
#'
#' @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 timestamp timestamp coercible to POSIXct that will be the time to search for orders before this time
+#' @param ordertype one of NULL, "market","limit",or "stop", default NULL
+#' @param side one of NULL, "long" or "short", default NULL
+#' @param oldstatus one of NULL, "open", "closed", "canceled", or "replaced", default "open"
#' @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)
+updateOrders <- function(portfolio, symbol, timestamp, ordertype=NULL, side=NULL, oldstatus="open", 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"'))
-
+ 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(ordertype) & !length(grep(ordertype,c("market","limit","stop")))==1) stop(paste("ordertype:",ordertype,' must be one of "market","limit",or "stop"'))
+
# need the ability to pass a range like we do in blotter
- updatedorders<-getOrdersByStatus(portfolio, symbol, timestamp, oldstatus,starttime=-.00001)
+ updatedorders<-getOrders(portfolio=portfolio, symbol=symbol, status=oldstatus, timestamp=timestamp, ordertype=ordertype, side=side)
# get order book
Copied: pkg/quantstrat/man/getOrders.Rd (from rev 238, pkg/quantstrat/man/getOrdersByStatus.Rd)
===================================================================
--- pkg/quantstrat/man/getOrders.Rd (rev 0)
+++ pkg/quantstrat/man/getOrders.Rd 2010-02-09 17:17:18 UTC (rev 240)
@@ -0,0 +1,17 @@
+\name{getOrders}
+\alias{getOrders}
+\title{get orders by time span, status, type, and side...}
+\usage{getOrders(portfolio, symbol, status="open", timestamp, ordertype, side, starttime=-86400)}
+\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.
+
+It has some use as a reporting or post-hoc analytics tool, but it may not always be exported.
+
+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", default "open"}
+\item{timestamp}{timestamp coercible to POSIXct that will be the period to find orders of the given status and ordertype}
+\item{ordertype}{one of NULL, "market","limit",or "stop", default NULL}
+\item{side}{one of NULL, "long" or "short", default NULL}
+\item{starttime}{difference to current timestamp to search, in seconds(numeric) or as a POSIXct timestamp, defaults to -86400 (one day)}}
Copied: pkg/quantstrat/man/updateOrders.Rd (from rev 238, pkg/quantstrat/man/updateOrder.Rd)
===================================================================
--- pkg/quantstrat/man/updateOrders.Rd (rev 0)
+++ pkg/quantstrat/man/updateOrders.Rd 2010-02-09 17:17:18 UTC (rev 240)
@@ -0,0 +1,19 @@
+\name{updateOrders}
+\alias{updateOrders}
+\title{update an order or orders...}
+\usage{updateOrders(portfolio, symbol, timestamp, ordertype, side, oldstatus="open", newstatus, statustimestamp)}
+\description{update an order or orders}
+\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 to search for orders before this time}
+\item{ordertype}{one of NULL, "market","limit",or "stop", default NULL}
+\item{side}{one of NULL, "long" or "short", default NULL}
+\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