[Blotter-commits] r1507 - in pkg/quantstrat: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Sep 16 19:24:52 CEST 2013
Author: opentrades
Date: 2013-09-16 19:24:52 +0200 (Mon, 16 Sep 2013)
New Revision: 1507
Modified:
pkg/quantstrat/NAMESPACE
pkg/quantstrat/R/orders.R
pkg/quantstrat/R/ruleOrderProc.R
pkg/quantstrat/R/ruleSignal.R
pkg/quantstrat/man/add.distribution.Rd
pkg/quantstrat/man/addOrder.Rd
pkg/quantstrat/man/apply.paramset.Rd
pkg/quantstrat/man/delete.paramset.Rd
pkg/quantstrat/man/getOrders.Rd
pkg/quantstrat/man/ruleSignal.Rd
Log:
- implemented time-in-force feature
Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/NAMESPACE 2013-09-16 17:24:52 UTC (rev 1507)
@@ -1,5 +1,5 @@
+export(add.distribution.constraint)
export(add.distribution)
-export(add.distribution.constraint)
export(add.indicator)
export(add.init)
export(add.rule)
Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/R/orders.R 2013-09-16 17:24:52 UTC (rev 1507)
@@ -82,8 +82,8 @@
orders<-list()
orders[[portfolio]]<-list()
}
- ordertemplate<-xts(as.matrix(t(c(0,NA,"init","long",0,"closed",as.character(as.POSIXct(initDate)),'','',0,''))),order.by=as.POSIXct(initDate), ...=...)
- colnames(ordertemplate) <- c("Order.Qty","Order.Price","Order.Type","Order.Side","Order.Threshold","Order.Status","Order.StatusTime","Prefer", "Order.Set","Txn.Fees","Rule")
+ ordertemplate<-xts(as.matrix(t(c(0,NA,"init","long",0,"closed",as.character(as.POSIXct(initDate)),'','',0,'',''))),order.by=as.POSIXct(initDate), ...=...)
+ colnames(ordertemplate) <- c("Order.Qty","Order.Price","Order.Type","Order.Side","Order.Threshold","Order.Status","Order.StatusTime","Prefer", "Order.Set","Txn.Fees","Rule","Time.In.Force")
if(is.null(symbols)) {
pfolio<-getPortfolio(portfolio)
@@ -119,13 +119,14 @@
#' @seealso addOrder
#' @concept order book
#' @export
-getOrders <- function(portfolio,symbol,status="open",timespan=NULL,ordertype=NULL, side=NULL, qtysign=NULL, orderset=NULL, which.i=FALSE)
+getOrders <- function(portfolio,symbol,status="open",timespan=NULL,ordertype=NULL, side=NULL, qtysign=NULL, orderset=NULL, which.i=FALSE, time.in.force=NULL)
{
#if(is.null(timespan)) stop("timespan must be an xts style timestring")
# get order book
orderbook <- getOrderBook(portfolio)
if(!any(names(orderbook[[portfolio]]) == symbol)) stop(paste("symbol",symbol,"does not exist in portfolio",portfolio,"having symbols",names(orderbook[[portfolio]])))
- ordersubset<-orderbook[[portfolio]][[symbol]]
+
+ ordersubset <- orderbook[[portfolio]][[symbol]]
if(is.null(ordersubset))
return(NULL)
@@ -142,6 +143,7 @@
(if(!is.null(ordertype)) ordersubset[,"Order.Type"]==ordertype else TRUE) &
(if(!is.null(side)) ordersubset[,"Order.Side"]==side else TRUE) &
(if(!is.null(orderset)) ordersubset[,"Order.Set"]==orderset else TRUE) &
+ (if(!is.null(time.in.force)) strptime(ordersubset[,'Time.In.Force'], format='%Y-%m-%d %H:%M:%S')<time.in.force else TRUE) &
(if(!is.null(qtysign)) sign(as.numeric(ordersubset[,"Order.Qty"]))==qtysign else TRUE)
)
@@ -254,6 +256,7 @@
#' @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)
#' @param label text label, default to '', set to rule label by \code{\link{ruleSignal}}
+#' @param time.in.force timestamp time-in-force; either a time stamp, or a number of seconds, or 'GTC' / '', 'GTC' and '' both meaning 'Good Till Canceled'; order expires if still 'open' at this timestamp, default is ''
#' @seealso getOrderBook
#' @seealso updateOrders
#' @concept order book
@@ -276,7 +279,8 @@
return=FALSE,
...,
TxnFees=0,
- label=''
+ label='',
+ time.in.force=''
)
{
# get order book
@@ -365,6 +369,20 @@
statustimestamp=NA # new orders don't have a status time
+ # time in force
+ if(time.in.force != '')
+ {
+ if(time.in.force == 'GTC')
+ time.in.force <- ''
+ else
+ {
+ if(is.numeric(time.in.force))
+ time.in.force <- timestamp + time.in.force
+
+ time.in.force <- format(time.in.force, "%Y-%m-%d %H:%M:%S")
+ }
+ }
+
#set up the other parameters
if (!length(qty)==length(price)) qty <- rep(qty,length(price))
if (!length(ordertype)==length(price)) ordertype <- rep(ordertype,length(price))
@@ -382,17 +400,17 @@
{
neworder <- xts(t(c(as.character(qty[i]), price[i], ordertype[i],
side, threshold[i], status, statustimestamp, prefer[i],
- orderset[i], TxnFees, label)), order.by=ordertime,
+ orderset[i], TxnFees, label, time.in.force)), order.by=ordertime,
dimnames=list(NULL, c("Order.Qty", "Order.Price", "Order.Type",
"Order.Side", "Order.Threshold", "Order.Status", "Order.StatusTime",
- "Prefer", "Order.Set", "Txn.Fees", "Rule")))
+ "Prefer", "Order.Set", "Txn.Fees", "Rule", "Time.In.Force")))
if(is.null(orders)) orders<-neworder
else orders <- rbind(orders,neworder)
}
}
- if(!is.null(orders) && ncol(orders)!=11) {
+ if(!is.null(orders) && ncol(orders)!=12) {
print("bad order(s):")
print(orders)
return()
Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/R/ruleOrderProc.R 2013-09-16 17:24:52 UTC (rev 1507)
@@ -52,12 +52,23 @@
ruleOrderProc <- function(portfolio, symbol, mktdata, timestamp=NULL, ordertype=NULL, ..., slippageFUN=NULL)
{
if(is.null(timestamp)) return()
+
orderbook <- getOrderBook(portfolio)
ordersubset <- orderbook[[portfolio]][[symbol]]
- # get open orders
- OpenOrders.i=NULL
- #TODO calculate timespan here?
+ ### first check time-in-force for open orders, and flag as 'expired' where appropriate
+ ExpiredOrders.i <- getOrders(portfolio=portfolio, symbol=symbol, status="open", time.in.force=timestamp, which.i=TRUE)
+ if(!is.null(ExpiredOrders.i))
+ {
+ ordersubset[ExpiredOrders.i, "Order.Status"] = 'expired'
+ ordersubset[ExpiredOrders.i, "Order.StatusTime"]<-ordersubset[ExpiredOrders.i, "Time.In.Force"]
+
+ orderbook[[portfolio]][[symbol]] <- ordersubset
+ assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
+ }
+
+ ### now retrieve open orders
+ ### TODO calculate timespan here?
OpenOrders.i<-getOrders(portfolio=portfolio, symbol=symbol, status="open", timespan=timespan, ordertype=ordertype, which.i=TRUE)
if(hasArg(prefer)) prefer=match.call(expand.dots=TRUE)$prefer
Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/R/ruleSignal.R 2013-09-16 17:24:52 UTC (rev 1507)
@@ -50,10 +50,11 @@
#' @param label rule label, default '', added by \code{\link{applyRules}}
#' @param order.price the order price to use, will overrule any mktdata lookup as well as chain.price (see below), meant to specify eg. a stop-loss price that is unrelated to the fill price (see chain.price)
#' @param chain.price the price that the parent order got filled for, used to pass to children in the order chain, will overrule all mktdata lookup, only meant for internal use really, default NULL
+#' @param time.in.force timestamp time-in-force; either a time stamp, or a number of seconds, or 'GTC' / '', 'GTC' and '' both meaning 'Good Till Canceled'; order expires if still 'open' at this timestamp, default is ''
#' @seealso \code{\link{osNoOp}} , \code{\link{add.rule}}
#' @export
-ruleSignal <- function(mktdata=mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype, orderside=NULL, orderset=NULL, threshold=NULL, tmult=FALSE, replace=TRUE, delay=0.0001, osFUN='osNoOp', pricemethod=c('market','opside','active'), portfolio, symbol, ..., ruletype, TxnFees=0, prefer=NULL, sethold=FALSE, label='', order.price=NULL, chain.price=NULL)
+ruleSignal <- function(mktdata=mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype, orderside=NULL, orderset=NULL, threshold=NULL, tmult=FALSE, replace=TRUE, delay=0.0001, osFUN='osNoOp', pricemethod=c('market','opside','active'), portfolio, symbol, ..., ruletype, TxnFees=0, prefer=NULL, sethold=FALSE, label='', order.price=NULL, chain.price=NULL, time.in.force='')
{
if(!is.function(osFUN))
osFUN<-match.fun(osFUN)
@@ -223,7 +224,8 @@
...=...,
prefer=prefer,
TxnFees=TxnFees,
- label=label)
+ label=label,
+ time.in.force=time.in.force)
# }
}
}
Modified: pkg/quantstrat/man/add.distribution.Rd
===================================================================
--- pkg/quantstrat/man/add.distribution.Rd 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/man/add.distribution.Rd 2013-09-16 17:24:52 UTC (rev 1507)
@@ -39,7 +39,7 @@
Jan Humme
}
\seealso{
- \code{\link{add.constraint}},
+ \code{\link{add.distribution.constraint}},
\code{\link{delete.paramset}},
\code{\link{apply.paramset}}
}
Modified: pkg/quantstrat/man/addOrder.Rd
===================================================================
--- pkg/quantstrat/man/addOrder.Rd 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/man/addOrder.Rd 2013-09-16 17:24:52 UTC (rev 1507)
@@ -6,7 +6,8 @@
ordertype, side, threshold = NULL, orderset = "",
status = "open", statustimestamp = "", prefer = NULL,
delay = 1e-05, tmult = FALSE, replace = TRUE,
- return = FALSE, ..., TxnFees = 0, label = "")
+ return = FALSE, ..., TxnFees = 0, label = "",
+ time.in.force = "")
}
\arguments{
\item{portfolio}{text name of the portfolio to associate
@@ -68,6 +69,11 @@
\item{label}{text label, default to '', set to rule label
by \code{\link{ruleSignal}}}
+
+ \item{time.in.force}{timestamp time-in-force; either a
+ time stamp, or a number of seconds, or 'GTC' / '', 'GTC'
+ and '' both meaning 'Good Till Canceled'; order expires
+ if still 'open' at this timestamp, default is ''}
}
\description{
It is important to understand that all the order
Modified: pkg/quantstrat/man/apply.paramset.Rd
===================================================================
--- pkg/quantstrat/man/apply.paramset.Rd 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/man/apply.paramset.Rd 2013-09-16 17:24:52 UTC (rev 1507)
@@ -68,8 +68,8 @@
Jan Humme
}
\seealso{
- \code{\link{add.constraint}},
- \code{\link{add.constraint}},
+ \code{\link{add.distribution.constraint}},
+ \code{\link{add.distribution.constraint}},
\code{\link{delete.paramset}}
}
Modified: pkg/quantstrat/man/delete.paramset.Rd
===================================================================
--- pkg/quantstrat/man/delete.paramset.Rd 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/man/delete.paramset.Rd 2013-09-16 17:24:52 UTC (rev 1507)
@@ -22,7 +22,7 @@
}
\seealso{
\code{\link{add.distibution}},
- \code{\link{add.constraint}},
+ \code{\link{add.distribution.constraint}},
\code{\link{apply.paramset}}
}
Modified: pkg/quantstrat/man/getOrders.Rd
===================================================================
--- pkg/quantstrat/man/getOrders.Rd 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/man/getOrders.Rd 2013-09-16 17:24:52 UTC (rev 1507)
@@ -4,7 +4,8 @@
\usage{
getOrders(portfolio, symbol, status = "open",
timespan = NULL, ordertype = NULL, side = NULL,
- qtysign = NULL, orderset = NULL, which.i = FALSE)
+ qtysign = NULL, orderset = NULL, which.i = FALSE,
+ time.in.force = NULL)
}
\arguments{
\item{portfolio}{text name of the portfolio to associate
Modified: pkg/quantstrat/man/ruleSignal.Rd
===================================================================
--- pkg/quantstrat/man/ruleSignal.Rd 2013-09-14 11:56:33 UTC (rev 1506)
+++ pkg/quantstrat/man/ruleSignal.Rd 2013-09-16 17:24:52 UTC (rev 1507)
@@ -9,7 +9,8 @@
pricemethod = c("market", "opside", "active"),
portfolio, symbol, ..., ruletype, TxnFees = 0,
prefer = NULL, sethold = FALSE, label = "",
- order.price = NULL, chain.price = NULL)
+ order.price = NULL, chain.price = NULL,
+ time.in.force = "")
}
\arguments{
\item{mktdata}{an xts object containing market data.
@@ -92,6 +93,11 @@
filled for, used to pass to children in the order chain,
will overrule all mktdata lookup, only meant for internal
use really, default NULL}
+
+ \item{time.in.force}{timestamp time-in-force; either a
+ time stamp, or a number of seconds, or 'GTC' / '', 'GTC'
+ and '' both meaning 'Good Till Canceled'; order expires
+ if still 'open' at this timestamp, default is ''}
}
\description{
As described elsewhere in the documentation, quantstrat
More information about the Blotter-commits
mailing list