[Blotter-commits] r1068 - in pkg/quantstrat: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jun 23 21:20:48 CEST 2012
Author: braverock
Date: 2012-06-23 21:20:48 +0200 (Sat, 23 Jun 2012)
New Revision: 1068
Modified:
pkg/quantstrat/R/orders.R
pkg/quantstrat/R/ruleSignal.R
pkg/quantstrat/man/addOrder.Rd
pkg/quantstrat/man/getOrders.Rd
pkg/quantstrat/man/updateOrders.Rd
Log:
- update support for replacing ordersets
- update roxygen docs
Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R 2012-06-23 17:36:28 UTC (rev 1067)
+++ pkg/quantstrat/R/orders.R 2012-06-23 19:20:48 UTC (rev 1068)
@@ -73,6 +73,7 @@
#' @param ordertype one of NULL, "market","limit","stoplimit", "stoptrailing", or "iceberg" default NULL
#' @param side one of NULL, "long" or "short", default NULL
#' @param qtysign one of NULL, -1,0,1 ; could be useful when all qty's are reported as positive numbers and need to be identified other ways, default NULL
+#' @param orderset a tag identifying the orderset
#' @param which.i if TRUE, return the row index numbers rather than the order rows matching the criteria, default FALSE
#' @seealso getOrderBook
#' @seealso addOrder
@@ -126,7 +127,9 @@
#' \emph{MAY} become transactions if market prices and liquidity cooperate.
#'
#' By default, this function will locate and replace any 'open' order(s)
-#' on the requested portfolio/symbol that have the same type and side.
+#' on the requested portfolio/symbol that have the same order
+#' type and side. If an orderset is also specified and replace=TRUE,
+#' \emph{all open orders} for the orderset will be replaced.
#' If you do not want open orders to be canceled and replaced with the new order,
#' set \code{replace=FALSE}.
#'
@@ -196,11 +199,12 @@
#' @param ordertype one of "market","limit","stoplimit", "stoptrailing",or "iceberg"
#' @param side one of either "long" or "short"
#' @param threshold numeric threshold to apply to trailing stop orders and limit orders, default NULL
+#' @param orderset set a tag identifying the orderset
#' @param status one of "open", "closed", "canceled", or "replaced", default "open"
#' @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
#' @param tmult if TRUE, threshold is a percent multiplier for \code{price}, not a scalar to be added/subtracted from price. threshold will be dynamically converted to a scalar at time of order entry
-#' @param replace TRUE/FALSE, whether to replace any other open order(s) on this portfolio symbol, default TRUE
+#' @param replace TRUE/FALSE, whether to replace any other open order(s) on this symbol with the same properties as this order, default TRUE, see Details
#' @param return if TRUE, return the row that makes up the order, default FALSE (will assign into the environment)
#' @param \dots any other passthru parameters
#' @param TxnFees numeric fees (usually negative) or function name for calculating TxnFees (processing happens later, not in this function)
@@ -209,7 +213,26 @@
#' @seealso updateOrders
#' @concept order book
#' @export
-addOrder <- function(portfolio, symbol, timestamp, qty, price, ordertype, side, orderset='', threshold=NULL, status="open", statustimestamp='' , prefer=NULL, delay=.00001, tmult=FALSE, replace=TRUE, return=FALSE, ..., TxnFees=0,label='')
+addOrder <- function(portfolio,
+ symbol,
+ timestamp,
+ qty,
+ price,
+ ordertype,
+ side,
+ threshold=NULL,
+ orderset='',
+ status="open",
+ statustimestamp='' ,
+ prefer=NULL,
+ delay=.00001,
+ tmult=FALSE,
+ replace=TRUE,
+ return=FALSE,
+ ...,
+ TxnFees=0,
+ label=''
+ )
{
# get order book
#orderbook <- getOrderBook(portfolio)
@@ -256,7 +279,7 @@
if(is.null(threshold)) threshold=NA #NA is not ignored like NULL is
- if(!length(grep(status,c("open", "closed", "canceled","replaced")))==1) stop(paste("order status:",status,' must be one of "open", "closed", "canceled", or "replaced"'))
+ if(!length(grep(status,c("open", "closed", "canceled","replaced",'rejected')))==1) stop(paste("order status:",status,' must be one of "open", "closed", "canceled", "replaced", or "rejected"'))
# TODO do we need to check for collision, and increment timestamp? or alternately update?
# subset by time and symbol
@@ -282,16 +305,17 @@
for (i in 1:length(price)) {
if(is.null(prefer[i])) prefer[i] = ''
neworder<-xts(as.matrix(t(c(as.character(qty[i]),
- price[i],
- ordertype[i],
- side,
- threshold[i],
- status,
- statustimestamp,
- prefer[i],
- orderset[i],
- TxnFees, label))),
- order.by=(ordertime))
+ price[i],
+ ordertype[i],
+ side,
+ threshold[i],
+ status,
+ statustimestamp,
+ prefer[i],
+ orderset[i],
+ TxnFees, label))),
+ order.by=(ordertime))
+
if(is.null(orders)) orders<-neworder
else orders <- rbind(orders,neworder)
}
@@ -306,7 +330,17 @@
else qtysign <- NULL
if(!isTRUE(return)){
- if(isTRUE(replace)) updateOrders(portfolio=portfolio, symbol=symbol,timespan=timespan, side=side, qtysign=qtysign, oldstatus="open", newstatus="replaced", statustimestamp=timestamp)
+ if(isTRUE(replace)) {
+ updateOrders(portfolio=portfolio,
+ symbol=symbol,
+ timespan=timespan,
+ side=side,
+ qtysign=qtysign,
+ orderset=orderset,
+ oldstatus="open",
+ newstatus="replaced",
+ statustimestamp=timestamp)
+ }
# get order book
orderbook <- getOrderBook(portfolio)
orderbook[[portfolio]][[symbol]]<-rbind(orderbook[[portfolio]][[symbol]],orders)
@@ -332,13 +366,16 @@
#' 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.
#'
+#' If orderset is passed to updateOrders, all orders with oldstatus
+#' for the orderset will be updated, regardless of their other parameters.
+#'
#' @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 timespan xts-style character timespan to be the period to find orders of the given status and ordertype
#' @param ordertype one of NULL, "market","limit","stoplimit", or "stoptrailing" default NULL
#' @param side one of NULL, "long" or "short", default NULL
#' @param qtysign one of NULL, -1,0,1 ; could be useful when all qty's are reported as positive numbers and need to be identified other ways, default NULL
-#' @param set a tag identifying the orderset
+#' @param orderset set a tag identifying the orderset
#' @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
@@ -347,22 +384,45 @@
#' @seealso getOrderBook
#' @concept order book
#' @export
-updateOrders <- function(portfolio, symbol, timespan, ordertype=NULL, side=NULL, qtysign=NULL, oldstatus="open", newstatus, statustimestamp, orderset=NULL)
+updateOrders <- function(portfolio,
+ symbol,
+ timespan,
+ ordertype=NULL,
+ side=NULL,
+ qtysign=NULL,
+ orderset=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(oldstatus) && !length(grep(oldstatus,c("open", "closed", "canceled","replaced",'rejected')))==1)
+ stop(paste("old order status:",oldstatus,' must be one of "open", "closed", "canceled", "replaced", or "rejected"'))
+ if(!length(grep(newstatus,c("open", "closed", "canceled","replaced",'rejected')))==1)
+ stop(paste("new order status:",newstatus,' must be one of "open", "closed", "canceled", "replaced", or "rejected"'))
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 NULL, -1, 0, or 1"))
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"'))
-
+ if(!is.null(orderset) && newstatus=='replaced'){
+ #replace any outstanding orders for this orderset
+ ordertype=NULL
+ side=NULL
+ qtysign=NULL
+ }
# 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, qtysign=qtysign, which.i=TRUE)
+ updatedorders<-getOrders(portfolio=portfolio,
+ symbol=symbol,
+ status=oldstatus,
+ timespan=timespan,
+ ordertype=ordertype,
+ side=side,
+ qtysign=qtysign,
+ orderset=orderset,
+ which.i=TRUE)
if(length(updatedorders)>=1){
# get order book
#TODO this gets the order book again after it was already retrieved by getOrdersByStatus.
Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R 2012-06-23 17:36:28 UTC (rev 1067)
+++ pkg/quantstrat/R/ruleSignal.R 2012-06-23 19:20:48 UTC (rev 1068)
@@ -156,7 +156,23 @@
if(!is.null(orderqty) && orderqty!=0 && !is.null(orderprice)) #orderprice could have length > 1
{
- addOrder(portfolio=portfolio, symbol=symbol, timestamp=timestamp, qty=orderqty, price=as.numeric(orderprice), ordertype=ordertype, side=orderside, orderset=orderset, threshold=threshold, status="open", replace=replace , delay=delay, tmult=tmult, ...=..., prefer=prefer, TxnFees=TxnFees,label=label)
+ addOrder(portfolio=portfolio,
+ symbol=symbol,
+ timestamp=timestamp,
+ qty=orderqty,
+ price=as.numeric(orderprice),
+ ordertype=ordertype,
+ side=orderside,
+ orderset=orderset,
+ threshold=threshold,
+ status="open",
+ replace=replace ,
+ delay=delay,
+ tmult=tmult,
+ ...=...,
+ prefer=prefer,
+ TxnFees=TxnFees,
+ label=label)
}
}
if(sethold) hold <<- TRUE
Modified: pkg/quantstrat/man/addOrder.Rd
===================================================================
--- pkg/quantstrat/man/addOrder.Rd 2012-06-23 17:36:28 UTC (rev 1067)
+++ pkg/quantstrat/man/addOrder.Rd 2012-06-23 19:20:48 UTC (rev 1068)
@@ -3,7 +3,7 @@
\title{add an order to the order book}
\usage{
addOrder(portfolio, symbol, timestamp, qty, price,
- ordertype, side, orderset = "", threshold = NULL,
+ ordertype, side, threshold = NULL, orderset = "",
status = "open", statustimestamp = "", prefer = NULL,
delay = 1e-05, tmult = FALSE, replace = TRUE,
return = FALSE, ..., TxnFees = 0, label = "")
@@ -32,6 +32,8 @@
\item{threshold}{numeric threshold to apply to trailing
stop orders and limit orders, default NULL}
+ \item{orderset}{set a tag identifying the orderset}
+
\item{status}{one of "open", "closed", "canceled", or
"replaced", default "open"}
@@ -47,7 +49,8 @@
scalar at time of order entry}
\item{replace}{TRUE/FALSE, whether to replace any other
- open order(s) on this portfolio symbol, default TRUE}
+ open order(s) on this symbol with the same properties as
+ this order, default TRUE, see Details}
\item{return}{if TRUE, return the row that makes up the
order, default FALSE (will assign into the environment)}
@@ -80,9 +83,11 @@
\details{
By default, this function will locate and replace any
'open' order(s) on the requested portfolio/symbol that
- have the same type and side. If you do not want open
- orders to be canceled and replaced with the new order,
- set \code{replace=FALSE}.
+ have the same order type and side. If an orderset is
+ also specified and replace=TRUE, \emph{all open orders}
+ for the orderset will be replaced. If you do not want
+ open orders to be canceled and replaced with the new
+ order, set \code{replace=FALSE}.
We have modeled two types of stop orders, which should be
sufficient to model most types of stops.
Modified: pkg/quantstrat/man/getOrders.Rd
===================================================================
--- pkg/quantstrat/man/getOrders.Rd 2012-06-23 17:36:28 UTC (rev 1067)
+++ pkg/quantstrat/man/getOrders.Rd 2012-06-23 19:20:48 UTC (rev 1068)
@@ -30,6 +30,8 @@
all qty's are reported as positive numbers and need to be
identified other ways, default NULL}
+ \item{orderset}{a tag identifying the orderset}
+
\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 2012-06-23 17:36:28 UTC (rev 1067)
+++ pkg/quantstrat/man/updateOrders.Rd 2012-06-23 19:20:48 UTC (rev 1068)
@@ -4,8 +4,8 @@
\usage{
updateOrders(portfolio, symbol, timespan,
ordertype = NULL, side = NULL, qtysign = NULL,
- oldstatus = "open", newstatus, statustimestamp,
- orderset = NULL)
+ orderset = NULL, oldstatus = "open", newstatus,
+ statustimestamp)
}
\arguments{
\item{portfolio}{text name of the portfolio to associate
@@ -28,7 +28,7 @@
all qty's are reported as positive numbers and need to be
identified other ways, default NULL}
- \item{set}{a tag identifying the orderset}
+ \item{orderset}{set a tag identifying the orderset}
\item{oldstatus}{one of NULL, "open", "closed",
"canceled", or "replaced", default "open"}
@@ -57,6 +57,10 @@
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.
+
+ If orderset is passed to updateOrders, all orders with
+ oldstatus for the orderset will be updated, regardless of
+ their other parameters.
}
\seealso{
addOrder
More information about the Blotter-commits
mailing list