[Blotter-commits] r1132 - in pkg/blotter: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Aug 25 10:42:47 CEST 2012


Author: braverock
Date: 2012-08-25 10:42:47 +0200 (Sat, 25 Aug 2012)
New Revision: 1132

Modified:
   pkg/blotter/R/addTxn.R
   pkg/blotter/R/getBySymbol.R
   pkg/blotter/R/updateEndEq.R
   pkg/blotter/R/updatePortf.R
   pkg/blotter/R/updatePosPL.R
   pkg/blotter/man/addTxn.Rd
   pkg/blotter/man/extractTxns.Rd
Log:
- fix passing TxnFees as character string adapted from patch submitted by Jian Li
- explicitly use time.xts anywhere we need it based on recommendation from Garrett See
- minor documentation updates 


Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/R/addTxn.R	2012-08-25 08:42:47 UTC (rev 1132)
@@ -6,19 +6,28 @@
 #' or loss (net of fees) from the transaction. Then it stores the transaction 
 #' and calculations in the Portfolio object.
 #'
-#' Fees are indicated as negative values and will be subtracted from the 
-#' transaction value. TxnFees can either be a fixed amount, or a function 
-#' in which case the function is evaluated to 
-#' determine the fee amount.
-#' The \code{pennyPerShare} function provides a simple example of a transaction cost
-#' function.
+#' Fees are indicated as negative values and will be
+#' subtracted from the transaction value. TxnFees can either
+#' be a fixed numeric amount, or a function (or charavcter
+#' name of a function) in which case the function is
+#' evaluated to determine the fee amount.
+#'
+#' The \code{pennyPerShare} function provides a simple
+#' example of a transaction cost function.
+#'
+#' Transactions which would cross the position through zero
+#' will be split into two transactions, one to flatten the
+#' position, and another to initiate a new position on the
+#' opposite side of the market.  The new (split) transaction
+#' will have its timestamp incremented by \code{eps} to
+#' preserve ordering.
+#'
+#' This transaction splitting vastly simplifies realized P&L
+#' calculations elsewhere in the code. Such splitting also
+#' mirrors many execution platforms and brokerage
+#' requirements in particular asset classes where the side
+#' of a trade needs to be specified with the order.
 #' 
-#' Transactions which would cross your position through zero will be split 
-#' into two transactions, one to flatten the position, and another to initiate 
-#' a new position on the opposite side of the market.  The new (split) 
-#' transaction will have it's timestamp inclremented by eps to preserve ordering. 
-#' This transaction splitting vastly simplifies realized P&L calculations elsewhere in the code.
-#' 
 #' @param Portfolio  A portfolio name that points to a portfolio object structured with \code{initPortf()}
 #' @param Symbol An instrument identifier for a symbol included in the portfolio, e.g., "IBM"
 #' @param TxnDate  Transaction date as ISO 8601, e.g., '2008-09-01' or '2010-01-05 09:54:23.12345'
@@ -32,7 +41,7 @@
 #' @note 
 #' The addTxn function will eventually also handle other transaction types, 
 #' such as adjustments for corporate actions or expire/assign for options. 
-
+#'
 #' @seealso \code{\link{addTxns}}, \code{\link{pennyPerShare}}, \code{\link{initPortf}}
 #' @author Peter Carl
 #' @export
@@ -75,8 +84,13 @@
 
 
     # FUNCTION
+    # Coerce the transaction fees to a function if a string was supplied
+    if (is.character(TxnFees)) TF <- try(match.fun(TxnFees))
+    if (!inherits(TF,"try-error")) TxnFees<-TF
+
     # Compute transaction fees if a function was supplied
     if (is.function(TxnFees)) txnfees <- TxnFees(TxnQty, TxnPrice) else txnfees<- as.numeric(TxnFees)
+
     if(is.null(txnfees) | is.na(txnfees)) txnfees = 0
     if(txnfees>0) warning('Positive Transaction Fees should only be used in the case of broker/exchange rebates for TxnFees ',TxnFees,'. See Documentation.')
     

Modified: pkg/blotter/R/getBySymbol.R
===================================================================
--- pkg/blotter/R/getBySymbol.R	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/R/getBySymbol.R	2012-08-25 08:42:47 UTC (rev 1132)
@@ -18,8 +18,8 @@
 
     # FUNCTION
     if(all(is.null(Dates)) || all(is.na(Dates))) # if no date is specified, get all available dates
-        Dates = time(Portfolio$symbols[[1]]$posPL)
-    # else  Dates = time(Portfolio$symbols[[1]]$posPL[Dates])
+        Dates = xts:::time.xts(Portfolio$symbols[[1]]$posPL)
+    # else  Dates = xts:::time.xts(Portfolio$symbols[[1]]$posPL[Dates])
     if(!is.null(attr(Portfolio,'currency')) & native==FALSE) {
         p.ccy.str<-attr(Portfolio,'currency')
         namePosPL = paste("posPL", p.ccy.str, sep=".")

Modified: pkg/blotter/R/updateEndEq.R
===================================================================
--- pkg/blotter/R/updateEndEq.R	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/R/updateEndEq.R	2012-08-25 08:42:47 UTC (rev 1132)
@@ -18,11 +18,11 @@
         stop(paste("Account",aname," not found, use initAcct() to create a new account"))
     
     if(is.null(Dates)) # if no date is specified, get all available dates
-        Dates = time(Account$summary)[-1]
+        Dates = xts:::time.xts(Account$summary)[-1]
     else
-        Dates = time(Account$summary[Dates])
+        Dates = xts:::time.xts(Account$summary[Dates])
 
-    PrevDate = time(Account$summary[first(Account$summary[Dates,which.i=TRUE])-1,]) # get index of previous end date 
+    PrevDate = xts:::time.xts(Account$summary[first(Account$summary[Dates,which.i=TRUE])-1,]) # get index of previous end date 
     PrevEndEq = getEndEq(aname, PrevDate)
     Additions = Account$summary[Dates]$Additions
     Withdrawals = Account$summary[Dates]$Withdrawals

Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/R/updatePortf.R	2012-08-25 08:42:47 UTC (rev 1132)
@@ -31,7 +31,7 @@
 	
     # Calculate and store portfolio summary table
     Portfolio<-getPortfolio(pname) # refresh with an updated object
-	if(is.null(Dates)) Dates <- time(Portfolio$symbols[[1]]$posPL)  #not quite right, only using first symbol...
+	if(is.null(Dates)) Dates <- xts:::time.xts(Portfolio$symbols[[1]]$posPL)  #not quite right, only using first symbol...
     #Symbols = names(Portfolio$symbols)
     Attributes = c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Period.Realized.PL', 'Period.Unrealized.PL', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
     summary = NULL
@@ -75,7 +75,7 @@
 		else {summary=cbind(summary,result)}
     }
 	
-	if(!is.timeBased(Dates)) Dates = time(Portfolio$symbols[[1]][Dates])
+	if(!is.timeBased(Dates)) Dates = xts:::time.xts(Portfolio$symbols[[1]][Dates])
 	startDate = first(xts:::.parseISO8601(Dates))$first.time-.00001 
 	# trim summary slot to not double count, related to bug 831 on R-Forge, and rbind new summary 
 	if( as.POSIXct(attr(Portfolio,'initDate'))>=startDate || length(Portfolio$summary)==0 ){

Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/R/updatePosPL.R	2012-08-25 08:42:47 UTC (rev 1132)
@@ -34,9 +34,9 @@
 
     # if no date is specified, get all available dates
     if(is.null(Dates)) {
-        Dates = time(prices)
+        Dates = xts:::time.xts(prices)
     } else if(!is.timeBased(Dates)) {
-        Dates = time(prices[Dates])
+        Dates = xts:::time.xts(prices[Dates])
     }
 
     if(.parseISO8601(Dates)$first.time < as.POSIXct(first(index(prices))) || is.na(.parseISO8601(Dates)$first.time)){

Modified: pkg/blotter/man/addTxn.Rd
===================================================================
--- pkg/blotter/man/addTxn.Rd	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/man/addTxn.Rd	2012-08-25 08:42:47 UTC (rev 1132)
@@ -47,18 +47,25 @@
 \details{
   Fees are indicated as negative values and will be
   subtracted from the transaction value. TxnFees can either
-  be a fixed amount, or a function in which case the
-  function is evaluated to determine the fee amount. The
-  \code{pennyPerShare} function provides a simple example
-  of a transaction cost function.
+  be a fixed numeric amount, or a function (or charavcter
+  name of a function) in which case the function is
+  evaluated to determine the fee amount.
 
-  Transactions which would cross your position through zero
+  The \code{pennyPerShare} function provides a simple
+  example of a transaction cost function.
+
+  Transactions which would cross the position through zero
   will be split into two transactions, one to flatten the
   position, and another to initiate a new position on the
   opposite side of the market.  The new (split) transaction
-  will have it's timestamp inclremented by eps to preserve
-  ordering. This transaction splitting vastly simplifies
-  realized P&L calculations elsewhere in the code.
+  will have its timestamp incremented by \code{eps} to
+  preserve ordering.
+
+  This transaction splitting vastly simplifies realized P&L
+  calculations elsewhere in the code. Such splitting also
+  mirrors many execution platforms and brokerage
+  requirements in particular asset classes where the side
+  of a trade needs to be specified with the order.
 }
 \note{
   The addTxn function will eventually also handle other

Modified: pkg/blotter/man/extractTxns.Rd
===================================================================
--- pkg/blotter/man/extractTxns.Rd	2012-08-23 15:22:16 UTC (rev 1131)
+++ pkg/blotter/man/extractTxns.Rd	2012-08-25 08:42:47 UTC (rev 1132)
@@ -2,11 +2,14 @@
 \alias{extractTxns}
 \title{Extract transactions from a portfolio}
 \usage{
-  extractTxns(Portfolio)
+  extractTxns(Portfolio, Symbol = NULL)
 }
 \arguments{
   \item{Portfolio}{string identifying the portfolio to
   extract from}
+
+  \item{Symbol}{an optional instrument identifier for a
+  symbol included in the portfolio}
 }
 \value{
   String vector of \code{\link{addTxn}} calls that would



More information about the Blotter-commits mailing list