[Blotter-commits] r1486 - pkg/blotter/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 13 00:30:33 CEST 2013


Author: braverock
Date: 2013-08-13 00:30:33 +0200 (Tue, 13 Aug 2013)
New Revision: 1486

Modified:
   pkg/blotter/R/updatePortf.R
Log:
- fix bug reported by Guy Yollin where Portfolio and Accout index rows could be duplicated. 
  Thanks to Josh Ulrich for help with the fix

Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R	2013-07-23 17:49:22 UTC (rev 1485)
+++ pkg/blotter/R/updatePortf.R	2013-08-12 22:30:33 UTC (rev 1486)
@@ -75,6 +75,25 @@
 		else {summary=cbind(summary,result)}
     }
 	
+	# get rid of duplicated indices in the summary data, 
+	# thanks to Guy Yollin for the bug report and Josh Ulrich for the elegant approach to fixing it
+	d <- duplicated(.index(summary)) | duplicated(.index(summary), fromLast=TRUE)
+	if(any(d)){
+		f <- function(x) {
+			cLast <- c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value')
+			cSums <- c('Period.Realized.PL', 'Period.Unrealized.PL', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
+			setNames(suppressWarnings(
+							merge(last(x[,cLast]), 
+							xts(t(colSums(x[,cSums],na.rm=TRUE)),order.by=last(index(x))))
+						)
+				  ,nm=colnames(x))
+		}
+		
+		slist <- do.call(rbind, lapply(split(summary[d,], .index(summary[d,])), f))
+		summary <- merge(summary[!d,], slist) #put it all back together
+	}
+	
+	
 	if(!is.timeBased(Dates)) Dates = xts:::time.xts(Portfolio$symbols[[1]][["posPL"]][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 



More information about the Blotter-commits mailing list