[Blotter-commits] r1487 - pkg/blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Aug 13 16:22:09 CEST 2013
Author: braverock
Date: 2013-08-13 16:22:09 +0200 (Tue, 13 Aug 2013)
New Revision: 1487
Modified:
pkg/blotter/R/updateAcct.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/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R 2013-08-12 22:30:33 UTC (rev 1486)
+++ pkg/blotter/R/updateAcct.R 2013-08-13 14:22:09 UTC (rev 1487)
@@ -75,6 +75,7 @@
#multiply by the currency multiplier
psummary<-psummary*CcyMult
}
+
# now bind it
Account$portfolios[[pname]] = rbind(Account$portfolios[[pname]],psummary)
}
@@ -85,13 +86,9 @@
table = .getByPortf(Account, 'Net.Trading.PL', Dates)
obsLength = length(index(table))
obsDates = index(table)
- if(obsLength > 1) # can't estimate periodicity of one observation
- on=periodicity(table)$units
- else
- on="none"
# Now aggregate the portfolio information into the $summary slot
- Attributes = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Interest', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
+ Attributes = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
for(Attribute in Attributes) {
switch(Attribute,
@@ -103,24 +100,9 @@
table = .getByPortf(Account, Attribute, Dates)
result = xts(rowSums(table,na.rm=TRUE),order.by=index(table))
},
- Additions = {
- result = if(on=="none")
- as.xts(sum(Account$Additions[paste("::",obsDates, sep="")]), order.by=index(table))
- else
- period.apply(Account$Additions[obsDates], endpoints(Account$Additions[obsDates], on=on), sum) # aggregates multiple account txns
- },
- Withdrawals = {
- result = if(on=="none")
- as.xts(sum(Account$Withdrawals[paste("::",obsDates, sep="")]), order.by=index(table))
- else
- period.apply(Account$Withdrawals[obsDates], endpoints(Account$Withdrawals[obsDates], on=periodicity(table)$units), sum)
- },
- Interest = {
- result = if(on=="none")
- as.xts(sum(Account$Interest[paste("::",obsDates, sep="")]),, order.by=index(table))
- else
- period.apply(Account$Interest[obsDates], endpoints(Account$Interest[obsDates], on=periodicity(table)$units), sum)
- },
+ Additions = ,
+ Withdrawals = ,
+ Int.Income = ,
Advisory.Fees = ,
Net.Performance = ,
End.Eq = {
@@ -128,14 +110,32 @@
result = xts(rep(0,obsLength),order.by=obsDates)
}
)
+
colnames(result) = Attribute
if(is.null(summary)) {summary=result}
else {summary=cbind(summary,result)}
}
- summary[is.na(summary)] <- 0 # replace any NA's with zero
- Account$summary <- rbind(Account$summary, summary)
- # This function does not calculate End.Eq
+ Account$summary <- rbind(Account$summary, summary)
+
+ # 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(Account$summary)) | duplicated(.index(Account$summary), fromLast=TRUE)
+ if(any(d)){
+ dedups <- function(x) {
+ suppressWarnings(
+ xts(t(colSums(x)),order.by=last(index(x)))
+ )
+ }
+
+ alist <- do.call(rbind, lapply(split(Account$summary[d,], .index(Account$summary[d,])), dedups) )
+
+ Account$summary <- rbind(Account$summary[!d,], alist) #put it all back together
+
+ }
+
+ # This function does not calculate End.Eq
+
assign(paste("account",name,sep='.'),Account, envir=.blotter)
return(name) #not sure this is a good idea
}
More information about the Blotter-commits
mailing list