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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Oct 8 20:12:10 CEST 2013


Author: braverock
Date: 2013-10-08 20:12:10 +0200 (Tue, 08 Oct 2013)
New Revision: 1519

Added:
   pkg/blotter/man/addAcctTxn.Rd
   pkg/blotter/man/put.account.Rd
   pkg/blotter/man/put.portfolio.Rd
Removed:
   pkg/blotter/man/addTxns.Rd
Modified:
   pkg/blotter/R/addPortfInstr.R
   pkg/blotter/R/addTxn.R
   pkg/blotter/R/updateAcct.R
   pkg/blotter/R/updateEndEq.R
   pkg/blotter/R/updatePortf.R
   pkg/blotter/R/updatePosPL.R
   pkg/blotter/man/PortfReturns.Rd
   pkg/blotter/man/addTxn.Rd
   pkg/blotter/man/initAcct.Rd
   pkg/blotter/man/initPortf.Rd
Log:
- remove assign's of portfolio and account objects, these are in an environment, and updated in place
- update roxygen docs

Modified: pkg/blotter/R/addPortfInstr.R
===================================================================
--- pkg/blotter/R/addPortfInstr.R	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/R/addPortfInstr.R	2013-10-08 18:12:10 UTC (rev 1519)
@@ -19,8 +19,8 @@
         portfolio$symbols[[instrument]]$posPL = .initPosPL(initDate = initDate, initPosQty = 0)
 		portfolio$symbols[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio$symbols[[instrument]]$posPL		
     }
-
-    assign(paste("portfolio",as.character(pname),sep='.'),portfolio,envir=.blotter)    
+    #portfolio is already an environment, it's been updated in place
+    #assign(paste("portfolio",as.character(pname),sep='.'),portfolio,envir=.blotter)    
 }
 
 ###############################################################################

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/R/addTxn.R	2013-10-08 18:12:10 UTC (rev 1519)
@@ -12,7 +12,7 @@
 #' name of a function) in which case the function is
 #' evaluated to determine the fee amount.
 #'
-#' The \code{pennyPerShare} function provides a simple
+#' The \code{\link{pennyPerShare}} function provides a simple
 #' example of a transaction cost function.
 #'
 #' Transactions which would cross the position through zero
@@ -38,15 +38,18 @@
 #' @param ConMult Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification
 #' @param verbose If TRUE (default) the function prints the elements of the transaction in a line to the screen, e.g., "2007-01-08 IBM 50 @@ 77.6". Suppress using FALSE.
 #' @param eps value to add to force unique indices
+#' @param TxnData  An xts object containing all required txn fields (for addTxns)
 #' @note 
 #' The addTxn function will eventually also handle other transaction types, 
 #' such as adjustments for corporate actions or expire/assign for options. 
+#' See \code{\link{addDiv}} 
 #'
+#' TODO figure out if we can fully vectorize the addTxns function to make it faster
 #' @seealso \code{\link{addTxns}}, \code{\link{pennyPerShare}}, \code{\link{initPortf}}
-#' @author Peter Carl
+#' @author Peter Carl, Brian G. Peterson
 #' @export
 addTxn <- function(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, ..., TxnFees=0, ConMult=NULL, verbose=TRUE, eps=1e-06)
-{ # @author Peter Carl
+{ 
 
     pname <- Portfolio
     PrevPosQty = getPosQty(pname, Symbol, TxnDate)
@@ -76,11 +79,11 @@
         }
     }
 
-	#If there is no table for the symbol then create a new one
-	if (is.null(Portfolio$symbols[[Symbol]])){ 
-		addPortfInstr(Portfolio=pname, symbols=Symbol)
-		Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
-	}
+  	#If there is no table for the symbol then create a new one
+  	if (is.null(Portfolio$symbols[[Symbol]])){ 
+  		addPortfInstr(Portfolio=pname, symbols=Symbol)
+  		Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+  	}
 
 
     # FUNCTION
@@ -112,13 +115,13 @@
     # Calculate any realized profit or loss (net of fees) from the transaction
     GrossTxnRealizedPL = TxnQty * ConMult * (PrevPosAvgCost - TxnAvgCost)
 
-	# if the previous position is zero, RealizedPL = 0
-	# if previous position is positive and position is larger, RealizedPL =0
-	# if previous position is negative and position is smaller, RealizedPL =0
-	if(abs(PrevPosQty) < abs(PosQty) | (PrevPosQty = 0))
-		GrossTxnRealizedPL = 0
+  	# if the previous position is zero, RealizedPL = 0
+  	# if previous position is positive and position is larger, RealizedPL =0
+  	# if previous position is negative and position is smaller, RealizedPL =0
+  	if(abs(PrevPosQty) < abs(PosQty) | (PrevPosQty = 0))
+  		GrossTxnRealizedPL = 0
 	
-	NetTxnRealizedPL = GrossTxnRealizedPL + txnfees
+	  NetTxnRealizedPL = GrossTxnRealizedPL + txnfees
 
     # Store the transaction and calculations
     NewTxn = xts(t(c(TxnQty, TxnPrice, TxnValue, TxnAvgCost, PosQty, PosAvgCost, GrossTxnRealizedPL, txnfees, NetTxnRealizedPL, ConMult)), order.by=TxnDate)
@@ -126,11 +129,12 @@
     Portfolio$symbols[[Symbol]]$txn<-rbind(Portfolio$symbols[[Symbol]]$txn, NewTxn)
 
     if(verbose)
-#        print(paste(TxnDate, Symbol, TxnQty, "@",TxnPrice, sep=" "))
-        print(paste(format(TxnDate, "%Y-%m-%d %H:%M:%S"), Symbol, TxnQty, "@",TxnPrice, sep=" "))
-        #print(Portfolio$symbols[[Symbol]]$txn)
+      # print(paste(TxnDate, Symbol, TxnQty, "@",TxnPrice, sep=" "))
+      print(paste(format(TxnDate, "%Y-%m-%d %H:%M:%S"), Symbol, TxnQty, "@",TxnPrice, sep=" "))
+      #print(Portfolio$symbols[[Symbol]]$txn)
     
-    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
+    #portfolio is already an environment, it's been updated in place
+    #assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
 }
 
 #' Example TxnFee cost function
@@ -141,17 +145,9 @@
     return(abs(TxnQty) * -0.01)
 }
 
-#' Add multiple transactions to a portfolio
-#' @param Portfolio  A portfolio name that points to a portfolio object structured with \code{\link{initPortf}}
-#' @param Symbol An instrument identifier for a symbol included in the portfolio, e.g., "IBM"
-#' @param TxnData  An xts object containing all required txn fields
-#' @param \dots Any other passthrough parameters
-#' @param verbose If TRUE (default) the function prints the elements of the transaction in a line to the screen, e.g., "2007-01-08 IBM 50 @@ 77.6". Suppress using FALSE.
-#' @param ConMult Contract or instrument multiplier for the Symbol if it is not defined in an instrument specification
-#' @seealso \code{\link{addTxn}}, \code{\link{initPortf}}
-#' @note
-#' TODO figure out if we can fully vectorize this function to make it faster
-addTxns<- function(Portfolio, Symbol, TxnData , verbose=TRUE, ..., ConMult=NULL)
+#' @rdname addTxn
+#' @export
+addTxns<- function(Portfolio, Symbol, TxnData , verbose=FALSE, ..., ConMult=NULL)
 {
     pname<-Portfolio
     Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
@@ -176,7 +172,7 @@
         TxnPrice       <- as.numeric(TxnData[row,'Price'])
         # If TxnFees are to be used, it must be a column in TxnData
         TxnFees <- if (any(grepl("TxnFees", colnames(TxnData)))) {
-            as.numeric(TxnData[row, "TxnFees"])
+          as.numeric(TxnData[row, "TxnFees"])
         } else 0
         #TxnFees         <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
         TxnValue       <- .calcTxnValue(TxnQty, TxnPrice, TxnFees, ConMult)
@@ -184,8 +180,8 @@
         #PrevPosQty     <- getPosQty(pname, Symbol, index(TxnData[row,]))
         PosQty         <- PrevPosQty+TxnQty
         PosAvgCost     <- .calcPosAvgCost(PrevPosQty, PrevPosAvgCost, 0, PosQty, ConMult) # lag this over the data?
-		GrossTxnRealizedPL = TxnQty * ConMult * (PrevPosAvgCost - TxnAvgCost)
-		NetTxnRealizedPL = GrossTxnRealizedPL - TxnFees
+        GrossTxnRealizedPL = TxnQty * ConMult * (PrevPosAvgCost - TxnAvgCost)
+        NetTxnRealizedPL = GrossTxnRealizedPL - TxnFees
         PrevPosQty     <- PosQty
         PrevPosAvgCost <- PosAvgCost
         
@@ -211,8 +207,9 @@
     Portfolio$symbols[[Symbol]]$txn<-rbind(Portfolio$symbols[[Symbol]]$txn,NewTxns) 
 
     if(verbose) print(NewTxns)
-    
-    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)    
+
+    #portfolio is already an environment, it's been updated in place
+    # assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)    
 }
 
 #' Add cash dividend transactions to a portfolio.
@@ -281,7 +278,8 @@
         print(paste(TxnDate, Symbol, "Dividend", DivPerShare, "on", PrevPosQty, "shares:", -TxnValue, sep=" "))
         #print(Portfolio$symbols[[Symbol]]$txn)
 
-    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
+    #portfolio is already an environment, it's been updated in place
+    #assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
 }
 ###############################################################################
 # Blotter: Tools for transaction-oriented trading systems development

Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/R/updateAcct.R	2013-10-08 18:12:10 UTC (rev 1519)
@@ -148,8 +148,9 @@
     summary[is.na(summary)] <- 0 # replace any NA's with zero
     Account$summary <- rbind(Account$summary, summary)
     # This function does not calculate End.Eq 
-
-    assign(paste("account",name,sep='.'),Account, envir=.blotter) 
+    
+    #account is already an environment, it's been updated in place
+    #assign(paste("account",name,sep='.'),Account, envir=.blotter) 
     return(name) #not sure this is a good idea
 }
 

Modified: pkg/blotter/R/updateEndEq.R
===================================================================
--- pkg/blotter/R/updateEndEq.R	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/R/updateEndEq.R	2013-10-08 18:12:10 UTC (rev 1519)
@@ -34,7 +34,8 @@
     EndCapital = PrevEndEq + cumsum(Additions + Withdrawals + NetPerformance) 
     Account$summary$End.Eq[Dates] <- EndCapital
 	
-    assign(paste("account",aname,sep='.'),Account, envir=.blotter) 
+	  #account is already an environment, it's been updated in place
+	  #assign(paste("account",aname,sep='.'),Account, envir=.blotter) 
     return(aname) #not sure this is a good idea
 }
 

Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/R/updatePortf.R	2013-10-08 18:12:10 UTC (rev 1519)
@@ -101,8 +101,9 @@
      }else{
        Portfolio$summary<-rbind(Portfolio$summary[paste('::',startDate,sep='')],summary)
      }
-     # assign Portfolio to environment
-     assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
+
+     #portfolio is already an environment, it's been updated in place
+     #assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
      
      return(pname) #not sure this is a good idea
 }

Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/R/updatePosPL.R	2013-10-08 18:12:10 UTC (rev 1519)
@@ -219,8 +219,8 @@
     #stick it in posPL.ccy
     Portfolio$symbols[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]]<-rbind(Portfolio$symbols[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]],TmpPeriods)
 	
-    # assign Portfolio to environment
-    assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
+    #portfolio is already an environment, it's been updated in place
+    #assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
 }
 
 ###############################################################################

Modified: pkg/blotter/man/PortfReturns.Rd
===================================================================
--- pkg/blotter/man/PortfReturns.Rd	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/man/PortfReturns.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -3,7 +3,8 @@
 \title{Calculate portfolio instrument returns}
 \usage{
   PortfReturns(Account, method = c("contribution"), ...,
-    Dates = NULL, Portfolios = NULL)
+    Dates = NULL, Portfolios = NULL,
+    period = c("daily", "none"))
 }
 \arguments{
   \item{Account}{string name of the account to generate
@@ -20,6 +21,8 @@
   \item{Portfolios}{concatenated string vector for
   portfolio names to retrieve returns on, default NULL (all
   portfolios)}
+
+  \item{period}{one of daily}
 }
 \description{
   This function (for now) calculates return on initial

Added: pkg/blotter/man/addAcctTxn.Rd
===================================================================
--- pkg/blotter/man/addAcctTxn.Rd	                        (rev 0)
+++ pkg/blotter/man/addAcctTxn.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -0,0 +1,61 @@
+\name{addAcctTxn}
+\alias{addAcctTxn}
+\title{Add capital account transactions, such as capital additions and withdrawals or interest income (expense)}
+\usage{
+  addAcctTxn(Account, TxnDate,
+    TxnType = c("Additions", "Withdrawals", "Interest"),
+    Amount, ..., verbose = TRUE)
+}
+\arguments{
+  \item{name}{Account name, as string}
+
+  \item{TxnDate}{transaction date as ISO 8601, e.g.,
+  '2008-09-01' or '2010-01-05 09:54:23.12345'}
+
+  \item{TxnType}{string indicating the type of account
+  transaction, only "Addition", "Withdrawal", or "Interest"
+  are currently supported}
+
+  \item{amount}{As of now, the currency of the transaction
+  MUST MATCH the currency of the Account.  Patches
+  welcome.}
+
+  \item{\dots}{any other passthrough parameters}
+
+  \item{verbose}{If TRUE (default) the function prints the
+  elements of the transaction in a line to the screen,
+  e.g., "2007-01-08 Withdrawal 15,012,235". Suppress using
+  FALSE.}
+}
+\description{
+  For the specified Account, take in the date, amount, and
+  type of transaction and append it to the correct list in
+  the account object
+}
+\details{
+  Adds capital transactions to a rudimentary transactions
+  table in the Account object.  This may be useful when
+  tracking the denominator of returns when there are
+  changes to the account's capital or significant interest
+  income. In the Account$summary table, there are several
+  placeholder columns that mimic the CFTC's 13-column
+  report.  Columns of interest here are "Additions",
+  "Withdrawals", and "Interest". Transactions added with
+  this function will be added into the appropriate one of
+  three slots in the Account object (Account$additions,
+  Account$withdrawals, or Account$Interest), which contains
+  an xts object of individual transactions with a date and
+  amount.  The \code{\link{updateAcct}} function will read
+  the transactions from each list in turn, aggregate them
+  by the specified date scope, and slot them into the
+  \code{Account$summary} table as it's built.
+  \code{\link{UpdateEndEq}} should then just work.
+}
+\author{
+  Peter Carl
+}
+\seealso{
+  \code{\link{initAcct}}, \code{\link{updateAcct}},
+  \code{\link{updateEndEq}}
+}
+

Modified: pkg/blotter/man/addTxn.Rd
===================================================================
--- pkg/blotter/man/addTxn.Rd	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/man/addTxn.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -1,10 +1,14 @@
 \name{addTxn}
 \alias{addTxn}
+\alias{addTxns}
 \title{Add transactions to a portfolio.}
 \usage{
   addTxn(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, ...,
     TxnFees = 0, ConMult = NULL, verbose = TRUE,
-    eps = 0.000001)
+    eps = 1e-06)
+
+  addTxns(Portfolio, Symbol, TxnData, verbose = FALSE, ...,
+    ConMult = NULL)
 }
 \arguments{
   \item{Portfolio}{A portfolio name that points to a
@@ -36,6 +40,9 @@
   e.g., "2007-01-08 IBM 50 @ 77.6". Suppress using FALSE.}
 
   \item{eps}{value to add to force unique indices}
+
+  \item{TxnData}{An xts object containing all required txn
+  fields (for addTxns)}
 }
 \description{
   When a trade or adjustment is made to the Portfolio, the
@@ -52,8 +59,8 @@
   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.
+  The \code{\link{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
@@ -71,10 +78,14 @@
 \note{
   The addTxn function will eventually also handle other
   transaction types, such as adjustments for corporate
-  actions or expire/assign for options.
+  actions or expire/assign for options. See
+  \code{\link{addDiv}}
+
+  TODO figure out if we can fully vectorize the addTxns
+  function to make it faster
 }
 \author{
-  Peter Carl
+  Peter Carl, Brian G. Peterson
 }
 \seealso{
   \code{\link{addTxns}}, \code{\link{pennyPerShare}},

Deleted: pkg/blotter/man/addTxns.Rd
===================================================================
--- pkg/blotter/man/addTxns.Rd	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/man/addTxns.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -1,38 +0,0 @@
-\name{addTxns}
-\alias{addTxns}
-\title{Add multiple transactions to a portfolio}
-\usage{
-  addTxns(Portfolio, Symbol, TxnData, verbose = TRUE, ...,
-    ConMult = NULL)
-}
-\arguments{
-  \item{Portfolio}{A portfolio name that points to a
-  portfolio object structured with \code{\link{initPortf}}}
-
-  \item{Symbol}{An instrument identifier for a symbol
-  included in the portfolio, e.g., "IBM"}
-
-  \item{TxnData}{An xts object containing all required txn
-  fields}
-
-  \item{\dots}{Any other passthrough parameters}
-
-  \item{verbose}{If TRUE (default) the function prints the
-  elements of the transaction in a line to the screen,
-  e.g., "2007-01-08 IBM 50 @ 77.6". Suppress using FALSE.}
-
-  \item{ConMult}{Contract or instrument multiplier for the
-  Symbol if it is not defined in an instrument
-  specification}
-}
-\description{
-  Add multiple transactions to a portfolio
-}
-\note{
-  TODO figure out if we can fully vectorize this function
-  to make it faster
-}
-\seealso{
-  \code{\link{addTxn}}, \code{\link{initPortf}}
-}
-

Modified: pkg/blotter/man/initAcct.Rd
===================================================================
--- pkg/blotter/man/initAcct.Rd	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/man/initAcct.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -48,7 +48,7 @@
   function.
 
   To get to the CFTC thirteen columns add: Gross.Realized,
-  Commission, Net.Realized, Int.Income, Ch.Unrealized,
+  Commission, Net.Realized, Interest, Ch.Unrealized,
   Advisory.Fees, Wealth.Index Again, no need to add
   Wealth.Index. Eventually, these additional columns will
   be useful. Gross.Realized will be calculated as (Net)

Modified: pkg/blotter/man/initPortf.Rd
===================================================================
--- pkg/blotter/man/initPortf.Rd	2013-10-08 14:49:07 UTC (rev 1518)
+++ pkg/blotter/man/initPortf.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -32,7 +32,7 @@
   Initializes a portfolio object, which is constructed from
   the following: $symbols: the identifier used for each
   instrument contained in the portfolio. Use
-  \code{names(Portfolio$symbols)} to get a list of symbols.
+  \code{ls(Portfolio$symbols)} to get a list of symbols.
   $symbols$[symbol]$txn: irregular xts object of
   transactions data $symbols$[symbol]$posPL: regular xts
   object of positions P&L calculated from transactions

Added: pkg/blotter/man/put.account.Rd
===================================================================
--- pkg/blotter/man/put.account.Rd	                        (rev 0)
+++ pkg/blotter/man/put.account.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -0,0 +1,18 @@
+\name{put.account}
+\alias{put.account}
+\title{put a account object in .blotter env}
+\usage{
+  put.account(account.st, account, envir = .blotter)
+}
+\arguments{
+  \item{account.st}{string identifying account}
+
+  \item{account}{account object}
+
+  \item{envir}{the environment to save the account object
+  in, defaults to .blotter}
+}
+\description{
+  put a account object in .blotter env
+}
+

Added: pkg/blotter/man/put.portfolio.Rd
===================================================================
--- pkg/blotter/man/put.portfolio.Rd	                        (rev 0)
+++ pkg/blotter/man/put.portfolio.Rd	2013-10-08 18:12:10 UTC (rev 1519)
@@ -0,0 +1,18 @@
+\name{put.portfolio}
+\alias{put.portfolio}
+\title{put a portfolio object in .blotter env}
+\usage{
+  put.portfolio(portfolio.st, portfolio, envir = .blotter)
+}
+\arguments{
+  \item{portfolio.st}{string identifying portfolio}
+
+  \item{portfolio}{portfolio object}
+
+  \item{envir}{the environment to save the portfolio object
+  in, defaults to .blotter}
+}
+\description{
+  put a portfolio object in .blotter env
+}
+



More information about the Blotter-commits mailing list