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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 27 15:24:53 CET 2010


Author: braverock
Date: 2010-01-27 15:24:52 +0100 (Wed, 27 Jan 2010)
New Revision: 205

Modified:
   pkg/blotter/NAMESPACE
   pkg/blotter/R/calcPortfAttr.R
   pkg/blotter/R/calcPortfWgt.R
   pkg/blotter/R/getBySymbol.R
   pkg/blotter/R/updateAcct.R
Log:
- fix multiple confusions in passing by value or by reference

Modified: pkg/blotter/NAMESPACE
===================================================================
--- pkg/blotter/NAMESPACE	2010-01-26 21:04:02 UTC (rev 204)
+++ pkg/blotter/NAMESPACE	2010-01-27 14:24:52 UTC (rev 205)
@@ -3,7 +3,7 @@
 export(chart.Posn)
 export(getEndEq)
 export(getPosQty)
-export(getPortAcct)
+export(getPortfAcct)
 export(getPortfolio)
 export(getTxns)
 export(initAcct)
@@ -11,5 +11,4 @@
 export(updateAcct)
 export(updateEndEq)
 export(updatePortf)
-#export(updatePosPL)
-export(buildSpread)
\ No newline at end of file
+#export(updatePosPL)
\ No newline at end of file

Modified: pkg/blotter/R/calcPortfAttr.R
===================================================================
--- pkg/blotter/R/calcPortfAttr.R	2010-01-26 21:04:02 UTC (rev 204)
+++ pkg/blotter/R/calcPortfAttr.R	2010-01-27 14:24:52 UTC (rev 205)
@@ -10,43 +10,43 @@
 
     switch(Attribute,
         Trading.PL = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Trading.PL', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Trading.PL', Date = Date, Symbols = Symbols)
             result = xts(apply(table, FUN='sum', MARGIN=1), Date)
             colnames(result) = 'Trading.PL'
         },
         Txn.Fees = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Txn.Fees', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Txn.Fees', Date = Date, Symbols = Symbols)
             result = xts(rowSums(table), Date)
             colnames(result) = 'Txn.Fees'
         },
         Realized.PL = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Realized.PL', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Realized.PL', Date = Date, Symbols = Symbols)
             result = xts(rowSums(table), Date)
             colnames(result) = 'Realized.PL'
         },
         Unrealized.PL = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Unrealized.PL', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Unrealized.PL', Date = Date, Symbols = Symbols)
             result = xts(rowSums(table), Date)
             colnames(result) = 'Unrealized.PL'
         },
         Net.Value = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
             result = xts(rowSums(table), Date)
             colnames(result) = 'Net.Value'
         },
         Gross.Value = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
             result = xts(rowSums(abs(table)), Date)
             colnames(result) = 'Gross.Value'
         },
         Long.Value = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
             table = apply(table,MARGIN=c(1,2),FUN=max,0)
             result = xts(rowSums(table), Date)
             colnames(result) = 'Long.Value'
         },
         Short.Value = {
-            table = getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
+            table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
             table = apply(table,MARGIN=c(1,2),FUN=min,0) # comes out a matrix
             result = xts(rowSums(table), Date)
             colnames(result) = 'Short.Value'

Modified: pkg/blotter/R/calcPortfWgt.R
===================================================================
--- pkg/blotter/R/calcPortfWgt.R	2010-01-26 21:04:02 UTC (rev 204)
+++ pkg/blotter/R/calcPortfWgt.R	2010-01-27 14:24:52 UTC (rev 205)
@@ -16,7 +16,7 @@
 
     # FUNCTION
 
-    pos.value = getBySymbol(Portfolio = Portfolio, Date = Dates, Attribute = "Pos.Value", Symbols = Symbols)
+    pos.value = .getBySymbol(Portfolio = Portfolio, Date = Dates, Attribute = "Pos.Value", Symbols = Symbols)
     portf.value = calcPortfAttr(Portfolio = Portfolio, Date = Dates, Attribute = denominator[1])
     weights = apply(pos.value, MARGIN = 2, FUN = function(x,y){return(x/y)}, y=portf.value) 
 

Modified: pkg/blotter/R/getBySymbol.R
===================================================================
--- pkg/blotter/R/getBySymbol.R	2010-01-26 21:04:02 UTC (rev 204)
+++ pkg/blotter/R/getBySymbol.R	2010-01-27 14:24:52 UTC (rev 205)
@@ -1,11 +1,6 @@
-`getBySymbol` <-
-function(Portfolio, Attribute, Date=NULL, Symbols = NULL)
+.getBySymbol <- function(Portfolio, Attribute, Date=NULL, Symbols = NULL)
 { # @author Peter Carl
-    pname<-Portfolio
-    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
-    if(inherits(Portfolio,"try-error"))
-        stop(paste("Portfolio",pname," not found, use initPortf() to create a new portfolio"))
-    
+
     # DESCRIPTION:
     # Retrieves calculated attributes for each position in the portfolio
     # from the posPL table.  Assembles into a symbol-by-time table useful

Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R	2010-01-26 21:04:02 UTC (rev 204)
+++ pkg/blotter/R/updateAcct.R	2010-01-27 14:24:52 UTC (rev 205)
@@ -11,7 +11,7 @@
 #' @param name 
 #' @param Dates 
 #' @export
-updateAcct <- function(name='default', Dates) 
+updateAcct <- function(name='default', Dates=NULL) 
 { # @author Peter Carl
 
     Account<-try(get(paste("account",name,sep='.'), envir=.blotter))
@@ -24,10 +24,12 @@
     Portfolios = names(Account)[-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
-        Dates = time(Portfolio[[1]]$posPL[Dates,])
+    if(is.null(Dates)) 
+        Dates = time(Portfolio[[1]]$posPL) # if no date is specified, get all available dates
+    if(length(Dates)==1){
+        # Dates is an xts range, turn it into a list of Dates
+        Dates = time(Portfolio[[1]]$posPL[Dates])
+    } 
 
     # For each date, calculate realized and unrealized P&L
     for(d in 1:length(Dates)){ # d is a date slot counter



More information about the Blotter-commits mailing list