[Blotter-commits] r133 - in pkg/blotter: . R demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 11 22:41:18 CET 2010


Author: braverock
Date: 2010-01-11 22:41:18 +0100 (Mon, 11 Jan 2010)
New Revision: 133

Added:
   pkg/blotter/NAMESPACE
Modified:
   pkg/blotter/R/addTxn.R
   pkg/blotter/R/calcEndEq.R
   pkg/blotter/R/chart.Posn.R
   pkg/blotter/R/getEndEq.R
   pkg/blotter/R/getPos.R
   pkg/blotter/R/getPosAvgCost.R
   pkg/blotter/R/getPosQty.R
   pkg/blotter/R/getRealizedPL.R
   pkg/blotter/R/getTxn.R
   pkg/blotter/R/getTxnFees.R
   pkg/blotter/R/getTxnValue.R
   pkg/blotter/R/initAcct.R
   pkg/blotter/R/initPortf.R
   pkg/blotter/R/initPosPL.R
   pkg/blotter/R/initTxn.R
   pkg/blotter/R/updateAcct.R
   pkg/blotter/R/updateEndEq.R
   pkg/blotter/R/updatePortf.R
   pkg/blotter/R/updatePosPL.R
   pkg/blotter/demo/longtrend.R
Log:
- multiple changes to move primary storage to environment .blotter
- add NAMESPACE
- add some roxygen comments to facilitate creation of NAMESPACE file, *do not* run R CMD roxygen with -d option

Added: pkg/blotter/NAMESPACE
===================================================================
--- pkg/blotter/NAMESPACE	                        (rev 0)
+++ pkg/blotter/NAMESPACE	2010-01-11 21:41:18 UTC (rev 133)
@@ -0,0 +1,10 @@
+export(addTxn)
+export(chart.Posn)
+export(getEndEq)
+export(getPosQty)
+export(initAcct)
+export(initPortf)
+export(updateAcct)
+export(updateEndEq)
+export(updatePortf)
+#export(updatePosPL)

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/addTxn.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,24 +1,21 @@
-`addTxn` <-
-function(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, TxnFees=0, verbose=TRUE)
+#' Adds transactions to a portfolio.
+#' 
+#' Fees are indicated as negative values and will be subtracted from the transaction value. TxnFees can either be a fixed amount, or a function of two arguments Qty and Price in which case the function is evaluated to determine the fee amount.
+#' 
+#' @param Portfolio  a portfolio name that points to a portfolio object structured with initPortf()
+#' @param Symbol an instrument identifier for a symbol included in the portfolio,e.g., IBM
+#' @param TxnDate  transaction date as ISO 8106, e.g., '2008-09-01'
+#' @param TxnQty total units (such as shares) transacted.  Positive values indicate a 'buy'; negative values indicate a 'sell'
+#' @param TxnPrice  price at which the transaction was done
+#' @param TxnFees fees associated with the transaction, e.g. commissions., See Details  
+#' @param verbose 
+#' @author Peter Carl
+#' @export
+addTxn <- function(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, TxnFees=0, verbose=TRUE)
 { # @author Peter Carl
-
-    # DESCRIPTION:
-    # Adds transactions to a portfolio.
-
-    # Inputs
-    # Portfolio: a portfolio object structured with initPortf()
-    # Symbol: an instrument identifier for a symbol included in the portfolio,
-    #   e.g., IBM
-    # TxnDate: transaction date as ISO 8106, e.g., '2008-09-01'
-    # TxnQty: total units (such as shares) transacted.  Positive values indicate
-    #   a 'buy'; negative values indicate a 'sell'
-    # TxnPrice: price at which the transaction was done
-    # TxnFees: fees associated with the transaction, e.g. commissions.  Fees are
-    #   indicated as negative values and will be subtracted from the transaction value.
-    #   TxnFees can either be a fixed amount, or a function of two arguments
-    #   Qty and Price in which case the function is evaluated to determine the
-    #   fee amount.
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    
     # Outputs:
     # Portfolio: hands back the entire portfolio object with the additional
     # transaction in the correct slot: Portfolio[[Symbol]]$txn
@@ -31,11 +28,11 @@
     TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty)
 
     # Calculate the change in position
-    PrevPosQty = getPosQty(Portfolio, Symbol, TxnDate)
+    PrevPosQty = getPosQty(pname, Symbol, TxnDate)
     PosQty = PrevPosQty + TxnQty
 
     # Calculate the resulting position's average cost
-    PrevPosAvgCost = getPosAvgCost(Portfolio, Symbol, TxnDate)
+    PrevPosAvgCost = getPosAvgCost(pname, Symbol, TxnDate)
     PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty)
 
     # Calculate any realized profit or loss (net of fees) from the transaction
@@ -48,7 +45,8 @@
 
     if(verbose)
         print(paste(TxnDate, Symbol, TxnQty, "@",TxnPrice, sep=" "))
-    return(Portfolio)
+    
+    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
 }
 
 ## example cost function

Modified: pkg/blotter/R/calcEndEq.R
===================================================================
--- pkg/blotter/R/calcEndEq.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/calcEndEq.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,6 +1,10 @@
 `calcEndEq` <-
 function(Account, Date)
 { 
+    aname<-Account
+    Account<-try(get(paste("account",aname,sep='.'), envir=.blotter))
+    if(inherits(Account,"try-error"))
+        stop(paste("Account",aname," not found, use initAcct() to create a new account"))
     # DESCRIPTION
     # Calculates End.Eq and Net.Performance
 
@@ -9,7 +13,7 @@
     # withdrawals, fees, interest, etc.)
     Dates = time(Account[[1]])
     PrevDate = Dates[grep(Date, Dates)-1]
-    PrevEndEq = getEndEq(Account, PrevDate)
+    PrevEndEq = getEndEq(aname, PrevDate)
     Additions = as.numeric(Account[[1]][Date]$Additions)
     Withdrawals = as.numeric(Account[[1]][Date]$Withdrawals)
     IntIncome = as.numeric(Account[[1]][Date]$Int.Income)

Modified: pkg/blotter/R/chart.Posn.R
===================================================================
--- pkg/blotter/R/chart.Posn.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/chart.Posn.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
-`chart.Posn` <- 
-function(Portfolio, Symbol = NULL, Dates = NULL, ...)
+#' @export
+chart.Posn <- function(Portfolio, Symbol = NULL, Dates = NULL, ...)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION
     # Charts the transaction series of a symbol against prices
 
@@ -17,8 +21,8 @@
     # FUNCTION
 
     require(quantmod)
-    # @TODO: check that Portfolio is a Portfolio object
-    # @TODO: add date scoping
+    # TODO: check that Portfolio is a Portfolio object
+    # TODO: add date scoping
     Prices=get(Symbol)
 
     Buys = Portfolio[[Symbol]]$txn$Txn.Price*(Portfolio[[Symbol]]$txn$Txn.Qty>0)

Modified: pkg/blotter/R/getEndEq.R
===================================================================
--- pkg/blotter/R/getEndEq.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getEndEq.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
-`getEndEq` <-
-function(Account, Date)
+#' @export
+getEndEq <- function(Account, Date)
 { # @author Peter Carl
-
+    aname<-Account
+    Account<-try(get(paste("account",aname,sep='.'), envir=.blotter))
+    if(inherits(Account,"try-error"))
+        stop(paste("Account",aname," not found, use initAcct() to create a new account"))
+    
     # DESCRIPTION:
     # Retrieves the most recent value of the capital account
 

Modified: pkg/blotter/R/getPos.R
===================================================================
--- pkg/blotter/R/getPos.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getPos.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
 `getPos` <-
 function(Portfolio, Symbol, Date)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION:
     # Retrieves all information about the position as of a date
 

Modified: pkg/blotter/R/getPosAvgCost.R
===================================================================
--- pkg/blotter/R/getPosAvgCost.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getPosAvgCost.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
 `getPosAvgCost` <-
 function(Portfolio, Symbol, Date)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    #Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    #if(inherits(Portfolio,"try-error"))
+    #    stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION:
     # Retrieves the most recent average cost of the position
 
@@ -14,7 +18,7 @@
     # Numeric value of the average cost of the current position
  
     # FUNCTION
-    PosAvgCost = as.numeric(getPos(Portfolio, Symbol, Date)[,"Pos.Avg.Cost"])
+    PosAvgCost = as.numeric(getPos(pname, Symbol, Date)[,"Pos.Avg.Cost"])
     return(PosAvgCost)
 }
 

Modified: pkg/blotter/R/getPosQty.R
===================================================================
--- pkg/blotter/R/getPosQty.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getPosQty.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
-`getPosQty` <-
-function(Portfolio, Symbol, Date)
+#' @export
+getPosQty <- function(Portfolio, Symbol, Date)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION:
     # Gets the previous position 
 
@@ -14,7 +18,7 @@
     # Numeric value of the most recent position.
 
     # FUNCTION
-    PosQty = as.numeric(getPos(Portfolio, Symbol, Date)[,"Pos.Qty"])
+    PosQty = as.numeric(getPos(pname, Symbol, Date)[,"Pos.Qty"])
     return(PosQty)
 }
 

Modified: pkg/blotter/R/getRealizedPL.R
===================================================================
--- pkg/blotter/R/getRealizedPL.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getRealizedPL.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
 `getRealizedPL` <-
 function(Portfolio, Symbol, Date)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION:
     # Retrieves realized PL for a period
 

Modified: pkg/blotter/R/getTxn.R
===================================================================
--- pkg/blotter/R/getTxn.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getTxn.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
 `getTxn` <-
 function(Portfolio, Symbol, Date)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION:
     # Gets the value of that period's transactions and returns the sum 
 

Modified: pkg/blotter/R/getTxnFees.R
===================================================================
--- pkg/blotter/R/getTxnFees.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getTxnFees.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,6 +1,11 @@
 `getTxnFees` <-
 function(Portfolio, Symbol, Date)
 {
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     TxnData = Portfolio[[Symbol]]$txn
     TxnFees = sum(TxnData[Date,'Txn.Fees'])
     return(TxnFees)

Modified: pkg/blotter/R/getTxnValue.R
===================================================================
--- pkg/blotter/R/getTxnValue.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/getTxnValue.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,7 +1,11 @@
 `getTxnValue` <-
 function(Portfolio, Symbol, Date)
 { # @author Peter Carl
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # DESCRIPTION:
     # Gets the value of that period's transactions and returns the sum 
 

Modified: pkg/blotter/R/initAcct.R
===================================================================
--- pkg/blotter/R/initAcct.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/initAcct.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,57 +1,70 @@
-`initAcct` <-
-function(portfolios, initDate="1950-01-01", initEq=100000)
-{ # @author Peter Carl
+.onLoad <- function(lib, pkg) {
+    if(!exists('.blotter'))  
+        .blotter <<- new.env()
+}
 
-    # DESCRIPTION
-    # Constructs the data container used to store calculated account values
-    # such as aggregated P&L, equity, etc.
 
-    # Inputs
-    # portfolios: a list of portfolio object names to attach to the account
-    # initDate: date prior to the first close price given, used to contain
-    #           initial account equity and initial position 
-    # initEq: initial equity or starting capitaal, default is 100,000
+#' Constructs the data container used to store calculated account values such as aggregated P&L, equity, etc.
+#' 
+#' Inputs
+#' portfolios: a list of portfolio object names to attach to the account
+#' initDate: date prior to the first close price given, used to contain
+#'           initial account equity and initial position 
+#' initEq: initial equity or starting capitaal, default is 100,000
+#' 
+#' Outputs
+#' Constructs multi-column xts object used to store aggregated portfolio 
+#' calculations
+#' 
+#' NOTES
+#' An Account object is a list of portfolios with portfolio summary information
+#' 
+#' The Account object is modeled on the CFTC Thirteen-column presentation table.
+#' Start with the CFTC six column presentation, which includes:
+#' Beg.Eq, Additions, Withdrawals, Net.Perf, End.Eq, Period.ROR
+#' No reason to persist Period.ROR, and Beg.Eq = Previous End.Eq,
+#' So we're left with four columns.  Note that Period.ROR can be calc'd 
+#' several different ways and is best left as a function.
 
-    # Outputs
-    # Constructs multi-column xts object used to store aggregated portfolio 
-    # calculations
+#' To get to the CFTC thirteen columns add:
+#' Gross.Realized, Commission, Net.Realized, Int.Income, 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) Realized.PL + Txn.Fees
 
-    # NOTES
-    # An Account object is a list of portfolios with portfolio summary information
+#' TODO:
+#' Add calcPeriodROR function
+#' add functions addCapital, drawCapital, addFees
+#' initDate and initEq can be used in addCapital to initalize the account?
+#' Track cash at this level???
+#' Calc gross PL and subtract fees? Or calc net PL and add fees.
+#' use getPortfSummary to populate portfolio tables?
 
-    # The Account object is modeled on the CFTC Thirteen-column presentation table.
-    # Start with the CFTC six column presentation, which includes:
-    # Beg.Eq, Additions, Withdrawals, Net.Perf, End.Eq, Period.ROR
-    # No reason to persist Period.ROR, and Beg.Eq = Previous End.Eq,
-    # So we're left with four columns.  Note that Period.ROR can be calc'd 
-    # several different ways and is best left as a function.
+#' @param name 
+#' @param portfolios 
+#' @param initDate 
+#' @param initEq 
+#' @export
+initAcct <- function(name='default', portfolios, initDate="1950-01-01", initEq=100000)
+{ # @author Peter Carl
 
-    # To get to the CFTC thirteen columns add:
-    # Gross.Realized, Commission, Net.Realized, Int.Income, 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) Realized.PL + Txn.Fees
-
-    ## TODOs:
-    ## Add calcPeriodROR function
-    ## add functions addCapital, drawCapital, addFees
-    ## initDate and initEq can be used in addCapital to initalize the account?
-
-    ## Track cash at this level???
-    ## Calc gross PL and subtract fees? Or calc net PL and add fees.
-    ## use getPortfSummary to populate portfolio tables?
-
+    if(exists(paste("account",name,sep='.'), envir=.blotter,inherits=TRUE)) 
+        stop(paste("Account",name,"already exists, use updateAcct() or create a new account."))
+    
     # FUNCTION
     account=vector("list",length=(length(portfolios)+1))
-    names(account)=c("TOTAL",portfolios)
+    names(account)=c("TOTAL",paste("portfolio",portfolios,sep='.'))
     account[["TOTAL"]] = xts( as.matrix(t(c(0,0,0,0,0,0,0,0,0,initEq))), order.by=as.POSIXct(initDate) )
     colnames(account[["TOTAL"]]) = c('Additions', 'Withdrawals', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
     for(portfolio in portfolios){
-        account[[portfolio]] = xts( as.matrix(t(c(0,0,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
-        colnames(account[[portfolio]]) = c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Txn.Fees','Realized.PL', 'Unrealized.PL', 'Trading.PL')
+        account[[paste("portfolio",portfolio,sep=".")]] = xts( as.matrix(t(c(0,0,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
+        colnames(account[[paste("portfolio",portfolio,sep=".")]]) = c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Txn.Fees','Realized.PL', 'Unrealized.PL', 'Trading.PL')
     }
-    return(account)
+    # return(account)
+    class("portfolio_account")
+    assign(paste("account",as.character(name),sep='.'),account,envir=.blotter)  
+    return(name) # not sure this is a good idea
 }
 
 ###############################################################################

Modified: pkg/blotter/R/initPortf.R
===================================================================
--- pkg/blotter/R/initPortf.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/initPortf.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,25 +1,28 @@
-`initPortf` <-
-function(symbols, initPosQty = 0, initDate = '1950-01-01')
+#' Initializes a portfolio object, 
+#' 
+#' Initializes a portfolio object, which is constructed from is constructed of the following:
+#' symbols: the identifier used for each instrument contained in the portfolio.
+#'   Use names(Portfolio) to get a list of symbols.
+#' $[symbol]$txn: irregular xts object of transactions data 
+#' $[symbol]$posPL: regular xts object of positions P&L calculated from 
+#'   transactions
+#' 
+#' TODO: add $account: name of the (one) affiliated account
+#
+#' Outputs
+#' Initialized portfolio structure with a start date and initial positions.
+#' 
+#' @param name 
+#' @param symbols  instrument identifiers of those instruments contained in the portfolio 
+#' @param initPosQty initial position, default is zero
+#' @param initDate date prior to the first close price given, used to contain initial account equity and initial position
+#' @author Peter Carl
+#' @export
+initPortf <- function(name="default", symbols, initPosQty = 0, initDate = '1950-01-01')
 { # @author Peter Carl
-
-    # DESCRIPTION
-    # Initializes a portfolio object, whichis constructed of the following:
-    # symbols: the identifier used for each instrument contained in the portfolio.
-    #   Use names(Portfolio) to get a list of symbols.
-    # $[symbol]$txn: irregular xts object of transactions data 
-    # $[symbol]$posPL: regular xts object of positions P&L calculated from 
-    #   transactions
-    ## @todo: add $account: name of the (one) affiliated account
-
-    # Inputs
-    # symbols: instrument identifiers of those instruments contained in the portfolio
-
-    # Outputs
-    # Initialized portfolio structure with a start date and initial positions.
-    # initDate: date prior to the first close price given, used to contain
-    #           initial account equity and initial position
-    # initPosQty: initial position, default is zero
-
+    if(exists(paste("portfolio",name,sep='.'), envir=.blotter,inherits=TRUE))
+        stop(paste("Portfolio",name,"already exists, use updatePortf() to update it."))
+    
     # FUNCTION
     portfolio=vector("list",length=length(symbols))
     names(portfolio)=symbols
@@ -32,7 +35,10 @@
         portfolio[[instrument]]$txn = initTxn(initDate = initDate, initPosQty = initPosQty[i])
         portfolio[[instrument]]$posPL = initPosPL(initDate = initDate, initPosQty = initPosQty[i])
     }
-    return(portfolio)
+    class(portfolio)<-c("blotter_portfolio", "portfolio")
+    #return(portfolio)
+    assign(paste("portfolio",as.character(name),sep='.'),portfolio,envir=.blotter)
+    return(name) # not sure this is a good idea
 }
 
 ###############################################################################

Modified: pkg/blotter/R/initPosPL.R
===================================================================
--- pkg/blotter/R/initPosPL.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/initPosPL.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,5 +1,7 @@
-`initPosPL` <-
-function(initDate="1950-01-01", initPosQty=0)
+#' initializes position P&L for a portfolio instrument
+#' @param initDate 
+#' @param initPosQty 
+initPosPL <- function(initDate="1950-01-01", initPosQty=0)
 { # @author Peter Carl
 
     # DESCRIPTION
@@ -17,6 +19,7 @@
     # FUNCTION
     posPL <- xts( as.matrix(t(c(initPosQty,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
     colnames(posPL) <- c('Pos.Qty', 'Pos.Value', 'Txn.Value', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL','Trading.PL')
+    class(posPL)<- c("posPL",class(posPL))
     return(posPL)
 }
 

Modified: pkg/blotter/R/initTxn.R
===================================================================
--- pkg/blotter/R/initTxn.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/initTxn.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,5 +1,4 @@
-`initTxn` <-
-function(initDate="1950-01-01", initPosQty=0)
+initTxn <- function(initDate="1950-01-01", initPosQty=0)
 { # @author Peter Carl
 
     # DESCRIPTION
@@ -15,10 +14,11 @@
     # Constructs multi-column xts object used to store transactions
 
     # FUNCTION
-    ## @todo: Add 'Txn.Type' column
-    ## @todo: DIVIDEND Txn.Type creates a realized gain
+    ## TODO: Add 'Txn.Type' column
+    ## TODO: DIVIDEND Txn.Type creates a realized gain
     txn <- xts( as.matrix(t(c(0,0,0,0,0,initPosQty,0,0))), order.by=as.POSIXct(initDate) )
     colnames(txn) <- c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Realized.PL')
+    class(txn)<-c("transactions",class(txn))
     return(txn)
 }
 

Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/updateAcct.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,23 +1,29 @@
-updateAcct <- function(Account, Dates) 
+#' Constructs the equity account calculations from the portfolio data and
+#' corresponding close prices.
+#'
+#' Inputs
+#' Prices: close prices in an xts OHLC object with a columnname == "Close"
+#' Dates: Dates from which to calculate equity account
+#'
+#' NOTES:
+#' Realized.PL is net of transaction fees.  To support 
+#' 
+#' @param name 
+#' @param Dates 
+#' @export
+updateAcct <- function(name='default', Dates) 
 { # @author Peter Carl
 
-    # DESCRIPTION
-    # Constructs the equity account calculations from the portfolio data and
-    # corresponding close prices. 
+    Account<-try(get(paste("account",name,sep='.'), envir=.blotter))
+    if(inherits(Account,"try-error"))
+        stop(paste("Account",name," not found, use initAcct() to create a new account"))
+    
 
-    # Inputs
-    # Prices: close prices in an xts OHLC object with a columnname == "Close"
-    # Dates: Dates from which to calculate equity account
-
-    # Outputs
-    # Account object.
-
-    # NOTES:
-    # Realized.PL is net of transaction fees.  To support 
-
     # FUNCTION
+    Account<-get(paste("account",name,sep='.'), envir=.blotter)
     Portfolios = names(Account)[-1]
-    Portfolio = get(Portfolios[1])
+    # TODO fix this so that it finds the date range in *any*/all portfolios
+    Portfolio = get(Portfolios[1],envir=.blotter)
     if(is.null(Dates)) # if no date is specified, get all available dates
         Dates = time(Portfolio[[1]]$posPL)
     else
@@ -29,7 +35,7 @@
     # for(date in Dates)
         # Append the portfolio summary data to the portfolio slot
         for(i in 1:length(Portfolios)){
-            Portfolio = get(Portfolios[i])
+            Portfolio = get(Portfolios[i],envir=.blotter)
             row = calcPortfSummary(Portfolio, Dates[d])
             Account[[i+1]] = rbind(Account[[i+1]],row)
         }
@@ -44,7 +50,8 @@
         Account[['TOTAL']] <- rbind(Account[['TOTAL']], row)
     # This function does not calculate End.Eq 
     }
-    return(Account) 
+    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	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/updateEndEq.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,6 +1,16 @@
-`updateEndEq` <-
-function(Account, Dates)
+#' update ending equity for an account
+#' 
+#' @param Account 
+#' @param Dates 
+#' @author Peter Carl
+#' @export
+updateEndEq <- function(Account, Dates)
 {
+    aname<-Account
+    Account<-try(get(paste("account",aname,sep='.'), envir=.blotter))
+    if(inherits(Account,"try-error"))
+        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[[1]])
     else
@@ -8,9 +18,10 @@
 
     # For each date, calculate realized and unrealized P&L
     for(d in 1:length(Dates)){ # d is a date slot counter
-	Account = calcEndEq(Account, as.character(Dates[d])) ## WTF?
+	Account = calcEndEq(aname, as.character(Dates[d])) ## WTF?
     }
-    return(Account)
+    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	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/updatePortf.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,26 +1,32 @@
-`updatePortf` <-
-function(Portfolio, Dates)
-{ # @author Peter Carl
+#' Function goes through each symbol and calculates the PL for each day prices are available
+#' 
+#' Inputs
+#' Portfolio: a portfolio object containing transactions
+#' Symbol: an instrument identifier for a symbol included in the portfolio
+#' Dates: Dates for which to calculate equity account
+#' These dates must appear in the price stream
+#' 
+#' Outputs
+#' Regular time series of position information and PL
+#' 
+#' @param Portfolio 
+#' @param Dates 
+#' @export
+updatePortf <- function(Portfolio, Dates)
+{ #' @author Peter Carl
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter,inherits=TRUE)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",pname," not found, use initPortf() to create a new account"))
+    
 
-    # DESCRIPTION
-    # Function goes through each symbol and calculates the PL for each day 
-    # prices are available
-
-    # Inputs
-    # Portfolio: a portfolio object containing transactions
-    # Symbol: an instrument identifier for a symbol included in the portfolio
-    # Dates: Dates for which to calculate equity account
-    # These dates must appear in the price stream
-
-    # Outputs
-    # Regular time series of position information and PL
-
     # FUNCTION
     symbols = names(Portfolio)
     for(symbol in symbols){
-      Portfolio = updatePosPL(Portfolio, symbol, Dates, Cl(get(symbol)))
+      Portfolio = updatePosPL(pname, symbol, Dates, Cl(get(symbol)))
   }
-  return(Portfolio)
+  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	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/R/updatePosPL.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -1,21 +1,20 @@
-`updatePosPL` <-
-function(Portfolio, Symbol, Dates, Prices=Cl(get(Symbol)))
+#' Calculates position PL from the position data and corresponding close price data. 
+#' 
+#' @param Portfolio a portfolio name to a portfolio structured with initPortf()
+#' @param Symbol an instrument identifier for a symbol included in the portfolio
+#' @param Dates xts subset of dates, e.g., "2007-01::2008-04-15". These dates must appear in the price stream
+#' @param Prices close prices in an xts object with a columnname == "Close"
+#' @return Regular time series of position information and PL 
+#' @author Peter Carl
+#' @export
+updatePosPL <- function(Portfolio, Symbol, Dates, Prices=Cl(get(Symbol)))
 { # @author Peter Carl
 
-    # DESCRIPTION
-    # Calculates position PL from the position data and
-    # corresponding close price data. 
-
-    # Inputs
-    # Portfolio: a portfolio object structured with initPortf()
-    # Symbol: an instrument identifier for a symbol included in the portfolio
-    # Prices: close prices in an xts object with a columnname == "Close"
-    # Dates: xts subset of dates, e.g., "2007-01::2008-04-15"
-    ## These dates must appear in the price stream
-
-    # Outputs
-    # Regular time series of position information and PL
-
+    pname<-Portfolio
+    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter,inherits=TRUE)
+    if(inherits(Portfolio,"try-error"))
+        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
+    
     # FUNCTION
     PosAvgCost = 0
     PosQty = 0
@@ -35,16 +34,16 @@
           if(length(PrevDate)==0)
              PrevDate = NA
 
-        TxnValue = getTxnValue(Portfolio, Symbol, CurrentDate)
-        TxnFees = getTxnFees(Portfolio, Symbol, CurrentDate)
-        PosQty = getPosQty(Portfolio, Symbol, CurrentDate)
+        TxnValue = getTxnValue(pname, Symbol, CurrentDate)
+        TxnFees = getTxnFees(pname, Symbol, CurrentDate)
+        PosQty = getPosQty(pname, Symbol, CurrentDate)
         ClosePrice = as.numeric(Prices[CurrentDate, grep("Close", colnames(Prices))]) #not necessary
         PosValue = calcPosValue(PosQty,ClosePrice)
 
         if(is.na(PrevDate))
             PrevPosQty = 0
         else
-            PrevPosQty = getPosQty(Portfolio, Symbol, PrevDate) 
+            PrevPosQty = getPosQty(pname, Symbol, PrevDate) 
 
         if(PrevPosQty==0)
             PrevClosePrice = 0
@@ -53,14 +52,15 @@
 
         PrevPosValue = calcPosValue(PrevPosQty,PrevClosePrice)
         TradingPL = calcTradingPL(PosValue, PrevPosValue, TxnValue)
-        RealizedPL = getRealizedPL(Portfolio, Symbol, CurrentDate)
-        UnrealizedPL = TradingPL - RealizedPL # @todo: calcUnrealizedPL(TradingPL, RealizedPL)
+        RealizedPL = getRealizedPL(pname, Symbol, CurrentDate)
+        UnrealizedPL = TradingPL - RealizedPL # TODO: calcUnrealizedPL(TradingPL, RealizedPL)
 
         NewPeriod = as.xts(t(c(PosQty, PosValue, TxnValue, TxnFees, RealizedPL, UnrealizedPL, TradingPL)), order.by=as.POSIXct(CurrentDate))
         colnames(NewPeriod) = c('Pos.Qty', 'Pos.Value', 'Txn.Value', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Trading.PL')
         Portfolio[[Symbol]]$posPL <- rbind(Portfolio[[Symbol]]$posPL, NewPeriod) 
     }
-    return(Portfolio)
+    # return(Portfolio)
+    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
 }
 
 ###############################################################################

Modified: pkg/blotter/demo/longtrend.R
===================================================================
--- pkg/blotter/demo/longtrend.R	2010-01-11 15:56:17 UTC (rev 132)
+++ pkg/blotter/demo/longtrend.R	2010-01-11 21:41:18 UTC (rev 133)
@@ -62,18 +62,21 @@
 
 # Set up a portfolio object and an account object in blotter
 print("Initializing portfolio and account structure")
-portfolio = initPortf('GSPC', initDate=initDate)
-account = initAcct(portfolios='portfolio', initDate=initDate)
+portfolio='longtrend'
+account='longtrend'
+initPortf(portfolio,'GSPC', initDate=initDate)
+initAcct(account,portfolios='longtrend', initDate=initDate)
 verbose=TRUE
 
 # Create trades
 for( i in 10:NROW(GSPC) ) { 
+    # browser()
     CurrentDate=time(GSPC)[i]
     cat(".")
     equity = getEndEq(account, CurrentDate)
 
     ClosePrice = as.numeric(Ad(GSPC[i,]))
-    Posn = getPosQty(Portfolio=portfolio, Symbol='GSPC', Date=CurrentDate)
+    Posn = getPosQty(portfolio, Symbol='GSPC', Date=CurrentDate)
     UnitSize = as.numeric(trunc(equity/ClosePrice))
 
     # Position Entry (assume fill at close)
@@ -82,21 +85,21 @@
         if( as.numeric(Ad(GSPC[i,])) > as.numeric(GSPC[i,'SMA10m']) ) { 
             cat('\n')
             # Store trade with blotter
-            portfolio = addTxn(Portfolio=portfolio, Symbol='GSPC', TxnDate=CurrentDate, TxnPrice=ClosePrice, TxnQty = UnitSize , TxnFees=0, verbose=verbose)
+            addTxn(portfolio, Symbol='GSPC', TxnDate=CurrentDate, TxnPrice=ClosePrice, TxnQty = UnitSize , TxnFees=0, verbose=verbose)
         } 
     } else {
     # Have a position, so check exit
         if( as.numeric(Ad(GSPC[i,]))  <  as.numeric(GSPC[i,'SMA10m'])) { 
             cat('\n')
             # Store trade with blotter
-            portfolio = addTxn(Portfolio=portfolio, Symbol='GSPC', TxnDate=CurrentDate, TxnPrice=ClosePrice, TxnQty = -Posn , TxnFees=0, verbose=verbose)
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/blotter -r 133


More information about the Blotter-commits mailing list