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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 23 18:46:05 CEST 2012


Author: braverock
Date: 2012-06-23 18:46:05 +0200 (Sat, 23 Jun 2012)
New Revision: 1066

Modified:
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/R/osFUNs.R
   pkg/quantstrat/R/ruleOrderProc.R
   pkg/quantstrat/R/ruleSignal.R
   pkg/quantstrat/R/rules.R
   pkg/quantstrat/man/addOrder.Rd
   pkg/quantstrat/man/initStrategy.Rd
   pkg/quantstrat/man/ruleSignal.Rd
   pkg/quantstrat/man/sigFormula.Rd
   pkg/quantstrat/man/updateStrategy.Rd
Log:
- fixes to better handle orderqty=='all' and defer processing until ruleOrderProc
- remove check for order crossing through zero, that's handed in blotter now
- update roxygen documentation
- bump version

Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/DESCRIPTION	2012-06-23 16:46:05 UTC (rev 1066)
@@ -1,7 +1,7 @@
 Package: quantstrat
 Type: Package
 Title: Quantitative Strategy Model Framework
-Version: 0.6.7
+Version: 0.6.8
 Date: $Date$
 Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
     Jeffrey A. Ryan, Joshua Ulrich, Garrett See

Modified: pkg/quantstrat/R/osFUNs.R
===================================================================
--- pkg/quantstrat/R/osFUNs.R	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/R/osFUNs.R	2012-06-23 16:46:05 UTC (rev 1066)
@@ -13,14 +13,16 @@
 #' @param ruletype one of "risk","order","rebalance","exit","enter", see \code{\link{add.rule}}
 #' @export
 osNoOp <- function(timestamp, orderqty, portfolio, symbol, ruletype, ...){
-	if(orderqty=='all'){
-		if (ruletype=='exit') {
-			orderqty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)
-		} else {
-			message("orderqty 'all' would produce nonsense, maybe use osMaxPos instead?")
-			orderqty=0
-		}
-	} 
+    # handle orderqty=='all' for ruletype!= 'exit' or 'risk'
+    if(orderqty=='all' && !(ruletype=='exit' || ruletype=='risk')) {
+        stop(paste("orderqty 'all' would produce nonsense, maybe use osMaxPos instead?\n",
+                   "Order Details:\n",
+                   'Timestamp:',timestamp,
+                   'Qty:',orderqty,
+                   'Symbol:',symbol)
+        )
+        orderqty=0 # in case we go back to returning this so that we reject it in the orderbook
+    }
 	return(orderqty)
 }
 

Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/R/ruleOrderProc.R	2012-06-23 16:46:05 UTC (rev 1066)
@@ -87,9 +87,12 @@
             txnfees=ordersubset[ii,"Txn.Fees"]
             orderPrice <- as.numeric(ordersubset[ii,"Order.Price"])
             orderQty <- ordersubset[ii,"Order.Qty"]
-            if(orderQty=='all') orderQty <- osNoOp(timestamp=timestamp, orderqty=orderQty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+            if(orderQty=='all'){
+                # this has to be an exit or risk order, so 
+                orderQty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)
+            }
             orderQty<-as.numeric(orderQty)
-            if(orderQty==0) next()
+            if(orderQty==0) next() #nothing to do, move along
             orderThreshold <- as.numeric(ordersubset[ii,"Order.Threshold"])
             # mktdataTimestamp <- mktdata[timestamp]
             #FIXME Should we only keep the last observation per time stamp?
@@ -283,25 +286,28 @@
             if(!is.null(txnprice) && !isTRUE(is.na(txnprice))) {
                 #make sure we don't cross through zero
                 pos<-getPosQty(portfolio,symbol,timestamp)
-                if ( (pos > 0 && orderQty < -pos) || (pos < 0 && orderQty > -pos) ) {
-                    warning("orderQty of ",orderQty,
-                            " would cross through zero, adjusting qty to ",-pos)
-                    orderQty <- -pos
-                }
+                
+                # this is handled correctly in addTxn now, so this isn't needed anymore
+#                if ( (pos > 0 && orderQty < -pos) || (pos < 0 && orderQty > -pos) ) {
+#                    warning("orderQty of ",orderQty,
+#                            " would cross through zero, adjusting qty to ",-pos)
+#                    orderQty <- -pos
+#                }
+    
                 if (orderQty != 0) {
                     #now add the transaction
                     addTxn(Portfolio=portfolio, Symbol=symbol, TxnDate=txntime, 
-                                 TxnQty=orderQty, TxnPrice=txnprice , ...=..., TxnFees=txnfees)
+                            TxnQty=orderQty, TxnPrice=txnprice , ...=..., TxnFees=txnfees)
                     ordersubset[ii,"Order.Status"]<-'closed'
                     ordersubset[ii,"Order.StatusTime"]<-as.character(timestamp)
-
+                    
                     #close all other orders in the order set
                     OrdersetTag = toString(ordersubset[ii,"Order.Set"])
-		    OpenInOrderset.i = which(ordersubset[,"Order.Status"] == 'open' & ordersubset[,"Order.Set"] == OrdersetTag)
+                    OpenInOrderset.i = which(ordersubset[,"Order.Status"] == 'open' & ordersubset[,"Order.Set"] == OrdersetTag)
                     # skip this if there are no orders
                     if(length(OpenInOrderset.i)>0) {
-                      ordersubset[OpenInOrderset.i, "Order.Status"] = 'canceled'
-                      ordersubset[OpenInOrderset.i, "Order.StatusTime"]<-as.character(timestamp)
+                        ordersubset[OpenInOrderset.i, "Order.Status"] = 'canceled'
+                        ordersubset[OpenInOrderset.i, "Order.StatusTime"]<-as.character(timestamp)
                     }
                 } 
             }

Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/R/ruleSignal.R	2012-06-23 16:46:05 UTC (rev 1066)
@@ -1,6 +1,11 @@
 
 #' default rule to generate a trade order on a signal
 #' 
+#' As described elsewhere in the documentation, quantstrat models 
+#' \emph{orders}.  This function is the default provided rule function to 
+#' generate those orders, which will be acted on later as they 
+#' interact with your market data.
+#' 
 #' \code{pricemethod} may be one of 
 #'      \describe{ 
 #'          \item{'market', 'opside', or 'active'}{ will use the 'ask' price if you're buying and 
@@ -144,24 +149,24 @@
 
 	## now size the order
 	#TODO add fancy formals matching for osFUN
-	if(orderqty!='all' || ordertype=='market' || (ruletype!='risk' && ruletype!='exit'))
+	if(orderqty!='all')
 	{
 		orderqty <- osFUN(strategy=strategy, data=data, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol,...=...,ruletype=ruletype, orderprice=as.numeric(orderprice))
+    }
+#		if(ruletype=='risk' || ruletype=='exit')
+#		{
+#			if(orderqty==0)	# cancel any open orders from orderset associated with this exit/risk order
+#				updateOrders(portfolio, symbol, oldstatus="open", newstatus='canceled', statustimestamp=timestamp, orderset=orderset)
+#
+#			if(((orderqty>0 && orderside=='long') || (orderqty<0 && orderside=='short')))
+#			{
+#				warning('trying to exit/all position but orderqty sign is wrong')
+#				orderqty = NULL		# dirty trick to suppress adding order below JH; (why?)
+#			}
+#		}
+#
+#        }
 
-		if(ruletype=='risk' || ruletype=='exit')
-		{
-			if(orderqty==0)	# cancel any open orders from orderset associated with this exit/risk order
-				updateOrders(portfolio, symbol, oldstatus="open", newstatus='canceled', statustimestamp=timestamp, orderset=orderset)
-
-			if(((orderqty>0 && orderside=='long') || (orderqty<0 && orderside=='short')))
-			{
-				warning('trying to exit/all position but orderqty sign is wrong')
-				orderqty = NULL		# dirty trick to suppress adding order below JH; (why?)
-			}
-		}
-
-        }
-
 	if(!is.null(orderqty) && orderqty!=0 && !is.null(orderprice)) #orderqty 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)

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/R/rules.R	2012-06-23 16:46:05 UTC (rev 1066)
@@ -409,7 +409,11 @@
                     for (slorder in stoplimitorders) {
                         dindex <- get.dindex()
                         tmpqty <- ordersubset[oo.idx[slorder],'Order.Qty']
-                        if (tmpqty=='all') tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+                        if (tmpqty=='all'){
+                            #tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+                            #set to 0, and let the next block figure it out from orderside
+                            tmpqty=0
+                        } 
                         if (tmpqty==0) {
                             #no position, so do some sleight of hand to figure out when the index may be needed
                             side <- ordersubset[oo.idx[slorder],'Order.Side']
@@ -452,14 +456,17 @@
                     for (lorder in limitorders){
                         dindex<-get.dindex()
                         tmpqty<-ordersubset[oo.idx[lorder],'Order.Qty']
-                        if (tmpqty=='all') tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
-#                        #don't think this block is needed for resular limit orders: BGP
-#                        if (tmpqty==0) {
-#                            #no position, so do some sleight of hand to figure out when the index may be needed
-#                            side <- ordersubset[oo.idx[lorder],'Order.Side']
-#                            if(side=='long') tmpqty=-1
-#                            else tmpqty=1
-#                        }
+                        if (tmpqty=='all'){
+                            #tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+                            #set to 0, and let the next block figure it out from orderside
+                            tmpqty=0
+                        } 
+                        if (tmpqty==0) {
+                            #no position, so do some sleight of hand to figure out when the index may be needed
+                            side <- ordersubset[oo.idx[lorder],'Order.Side']
+                            if(side=='long') tmpqty=-1
+                            else tmpqty=1
+                        }
                         tmpqty<-as.numeric(tmpqty)
                         
                         tmpprice<-as.numeric(ordersubset[oo.idx[lorder],'Order.Price'])
@@ -518,9 +525,20 @@
                         onum<-oo.idx[torder]
                         orderThreshold <- as.numeric(ordersubset[onum,'Order.Threshold'])
                         tmpqty<-ordersubset[onum,'Order.Qty']
-                        if (tmpqty=='all') tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+                        if (tmpqty=='all'){
+                            #tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+                            #set to 0, and let the next block figure it out from orderside
+                            tmpqty=0
+                        } 
                         if (tmpqty==0) {
                             #no position, so do some sleight of hand to figure out when the index may be needed
+                            side <- ordersubset[oo.idx[slorder],'Order.Side']
+                            if(side=='long') tmpqty=-1
+                            else tmpqty=1
+                        }
+                        tmpqty<-as.numeric(tmpqty)
+                        if (tmpqty==0) {
+                            #no position, so do some sleight of hand to figure out when the index may be needed
                             side <- ordersubset[onum,'Order.Side']
                             if(side=='long') tmpqty=-1
                             else tmpqty=1

Modified: pkg/quantstrat/man/addOrder.Rd
===================================================================
--- pkg/quantstrat/man/addOrder.Rd	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/man/addOrder.Rd	2012-06-23 16:46:05 UTC (rev 1066)
@@ -4,7 +4,7 @@
 \usage{
   addOrder(portfolio, symbol, timestamp, qty, price,
     ordertype, side, orderset = "", threshold = NULL,
-    status = "open", statustimestamp = "", prefer = "",
+    status = "open", statustimestamp = "", prefer = NULL,
     delay = 1e-05, tmult = FALSE, replace = TRUE,
     return = FALSE, ..., TxnFees = 0, label = "")
 }

Modified: pkg/quantstrat/man/initStrategy.Rd
===================================================================
--- pkg/quantstrat/man/initStrategy.Rd	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/man/initStrategy.Rd	2012-06-23 16:46:05 UTC (rev 1066)
@@ -3,9 +3,9 @@
 \title{run standard and custom strategy initialization functions}
 \usage{
   initStrategy(strategy, portfolio, symbols,
-    parameters = NULL, get.Symbols = TRUE,
-    init.Portf = TRUE, init.Acct = TRUE,
-    init.Orders = TRUE, unique = TRUE, ...)
+    get.Symbols = TRUE, init.Portf = TRUE,
+    init.Acct = TRUE, init.Orders = TRUE, unique = TRUE,
+    ...)
 }
 \arguments{
   \item{strategy}{object of type \code{strategy} to
@@ -15,44 +15,22 @@
 
   \item{symbols}{symbols}
 
-  \item{parameters}{named list of parameters to be applied
-  during evaluation of the strategy, default NULL}
+  \item{get.Symbols}{TRUE/FALSE, default TRUE:}
 
-  \item{get.Symbols}{TRUE/FALSE, default TRUE}
+  \item{init.Portf}{TRUE/FALSE, default TRUE:}
 
-  \item{init.Portf}{TRUE/FALSE, default TRUE}
+  \item{init.Acct}{TRUE/FALSE, default TRUE:}
 
-  \item{init.Acct}{TRUE/FALSE, default TRUE}
+  \item{init.Orders}{TRUE/FALSE, default TRUE:}
 
-  \item{init.Orders}{TRUE/FALSE, default TRUE}
+  \item{unique}{TRUE/FALSE, default TRUE:}
 
-  \item{unique}{TRUE/FALSE, default TRUE}
-
-  \item{\dots}{any other passthrough parameters}
+  \item{\dots}{any other passtrhrough parameters}
 }
 \description{
-  \code{initStrategy} will run a series of common
-  initialization functions at the beginning of an
-  \code{\link{applyStrategy}} call.
+  run standard and custom strategy initialization functions
 }
-\details{
-  \describe{ \item{get.Symbols}{if TRUE, will call
-  \code{\link[quantmod]{getSymbols}} on all symbols
-  included in the \code{symbols} vector}
-  \item{init.Portf}{if TRUE, will call
-  \code{\link[blotter]{initPortf}} to initialize the
-  portfolio object} \item{init.Acct}{if TRUE, will call
-  \code{\link[blotter]{initAccount}} to initialize the
-  account object} \item{init.Orders}{if TRUE, will call
-  \code{\link{initOrders}} to initialize the order book for
-  this test} \item{unique}{not yet implemented, will force
-  a unique portfolio and account name if the portfolio,
-  account, or order book already exist} }
-}
 \author{
   Garrett See, Brian Peterson
 }
-\seealso{
-  \code{\link{applyStrategy}}, \code{\link{add.init}},
-}
 

Modified: pkg/quantstrat/man/ruleSignal.Rd
===================================================================
--- pkg/quantstrat/man/ruleSignal.Rd	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/man/ruleSignal.Rd	2012-06-23 16:46:05 UTC (rev 1066)
@@ -81,11 +81,11 @@
   \code{\link{applyRules}}}
 }
 \description{
-  The \code{ruleSignal} function is a default sample rule
-  function for generating orders from a signal.  While some
-  strategies may require custom rule functions, we've found
-  that ruleSignal is sufficient for many strategies, with
-  custom code more often found in indicators and rules.
+  As described elsewhere in the documentation, quantstrat
+  models \emph{orders}.  This function is the default
+  provided rule function to generate those orders, which
+  will be acted on later as they interact with your market
+  data.
 }
 \details{
   \code{pricemethod} may be one of \describe{

Modified: pkg/quantstrat/man/sigFormula.Rd
===================================================================
--- pkg/quantstrat/man/sigFormula.Rd	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/man/sigFormula.Rd	2012-06-23 16:46:05 UTC (rev 1066)
@@ -19,7 +19,7 @@
 \description{
   This code takes advantage of some base R functionality
   that can treat an R object (in this case the internal
-  mktdata object in quantstrat) as an environment or
+  mktdata object in quantstrat) as an enfironment or
   'frame' using \code{\link{parent.frame}}. This allows the
   columns of the data to be addressed without any major
   manipulation, simply by column name.  In most cases in

Modified: pkg/quantstrat/man/updateStrategy.Rd
===================================================================
--- pkg/quantstrat/man/updateStrategy.Rd	2012-06-21 19:45:21 UTC (rev 1065)
+++ pkg/quantstrat/man/updateStrategy.Rd	2012-06-23 16:46:05 UTC (rev 1066)
@@ -2,16 +2,12 @@
 \alias{updateStrategy}
 \title{run standard and custom strategy wrapup functions such as updating portfolio, account, and ending equity}
 \usage{
-  updateStrategy(strategy, portfolio = "default",
-    account = portfolio, Symbols = NULL, parameters = NULL,
-    Dates = NULL, Prices = NULL, update.Portf = TRUE,
-    update.Acct = TRUE, update.EndEq = TRUE, showEq = TRUE,
-    chart = TRUE, ...)
+  updateStrategy(portfolio = "default",
+    account = portfolio, Symbols = NULL, Dates = NULL,
+    Prices = NULL, update.Portf = TRUE, update.Acct = TRUE,
+    update.EndEq = TRUE, showEq = TRUE, chart = TRUE, ...)
 }
 \arguments{
-  \item{strategy}{object of type \code{strategy} to
-  initialize data/containers for}
-
   \item{portfolio}{string identifying a portfolio}
 
   \item{account}{string identifying an account. Same as
@@ -20,9 +16,6 @@
   \item{Symbols}{character vector of names of symbols whose
   portfolios will be updated}
 
-  \item{parameters}{named list of parameters to be applied
-  during evaluation of the strategy, default NULL}
-
   \item{Dates}{optional xts-style ISO-8601 time range to
   run updatePortf over, default NULL (will use times from
   Prices)}
@@ -67,16 +60,6 @@
   than tick.  A custom wrapup function could take your high
   frequency data and transform it to lower frequency data
   before the call to \code{\link{updatePortf}}.
-
-  The 'standard wrapup functions included are: \describe{
-  \item{update.Portf}{ if TRUE, will call
-  \code{\link[blotter]{updatePortf}} to mark the book in
-  the portfolio.  } \item{update.Acct}{ if TRUE, will call
-  \code{\link[blotter]{updateAcct}} to mark the blotter
-  account for this test.  } \item{update.EndEq}{ if TRUE,
-  will call \code{\link[blotter]{updateEndEq}} to update
-  the account equity after all other accounting has been
-  completed.  } }
 }
 \author{
   Garrett See, Brian Peterson



More information about the Blotter-commits mailing list