[Blotter-commits] r1416 - in pkg: blotter/R quantstrat/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 29 16:40:50 CET 2013


Author: braverock
Date: 2013-03-29 16:40:50 +0100 (Fri, 29 Mar 2013)
New Revision: 1416

Modified:
   pkg/blotter/R/PortfReturns.R
   pkg/quantstrat/demo/faber.R
Log:
- update PortfReturns, add daily frequency as default
- update faber demo to work with new PortfReturns

Modified: pkg/blotter/R/PortfReturns.R
===================================================================
--- pkg/blotter/R/PortfReturns.R	2013-03-27 14:18:03 UTC (rev 1415)
+++ pkg/blotter/R/PortfReturns.R	2013-03-29 15:40:50 UTC (rev 1416)
@@ -11,6 +11,7 @@
 #' @param \dots any other passthru parameters (like \code{native} for \code{.getBySymbol}
 #' @param Dates xts style ISO 8601 date subset to retrieve, default NULL (all dates)
 #' @param Portfolios concatenated string vector for portfolio names to retrieve returns on, default NULL (all portfolios)
+#' @param period one of daily
 #' @note
 #' TODO handle portfolio and account in different currencies (not hard, just not done)
 #' 
@@ -19,11 +20,14 @@
 #' TODO provide additional  methods of calculating returns
 #' 
 #' TODO support additions and withdrawals to available capital 
-PortfReturns <- function (Account, method=c('contribution'),...,Dates=NULL,Portfolios=NULL) 
+PortfReturns <- function (Account, method=c('contribution'),...,Dates=NULL,Portfolios=NULL,period=c('daily','none')) 
 { # @author Brian Peterson
-	aname<-Account
+	period <- period[1] #use first
+	aname  <- Account
+	
 	if(!grepl("account\\.",aname)) Account<-try(get(paste("account",aname,sep='.'),envir=.blotter), silent=TRUE)
 	else Account<-try(get(aname,envir=.blotter), silent=TRUE)
+	
 	if(inherits(Account,"try-error"))
 		stop(paste("Account ",aname," not found, use initAcct() to create a new account"))
 	if(!inherits(Account,"account")) stop("Account ",aname," passed is not the name of an account object.")
@@ -31,23 +35,35 @@
 	if(is.null(Portfolios)) Portfolios = names(Account$portfolios)
 	
 	table=NULL
-	for(pname in Portfolios){
-		Portfolio <- getPortfolio(pname)
-		if(is.null(Dates)) Dates <- paste("::",last(index(Portfolio$summary)),sep='')
-		ptable = .getBySymbol(Portfolio = Portfolio, Attribute = "Net.Trading.PL", Dates = Dates,...)
-		
-		#TODO check portfolio and account currencies and convert if necessary
-		
-		#TODO handle additions and withdrawals in equity
-		
-		if(!is.null(attr(Account,'initEq'))){
-			initEq<-as.numeric(attr(Account,'initEq'))
-			if(initEq==0) stop("Initial equity of zero would produce div by zero NaN,Inf,-Inf returns, please fix in initAcct().")
-			ptable = ptable/initEq
+	
+	
+	if(period=='daily'){
+		table = dailyEqPL(Portfolios = Portfolios)	
+	} else {
+		for(pname in Portfolios){
+			Portfolio <- getPortfolio(pname)
+			
+			if(is.null(Dates)) Dates <- paste("::",last(index(Portfolio$summary)),sep='')
+
+			#extract
+			ptable = .getBySymbol(Portfolio = Portfolio, Attribute = "Net.Trading.PL", Dates = Dates,...)
+			ptable = PerformanceAnalytics:::zerofill(ptable)
+			#combine
+			if(is.null(table)) table=ptable
+			else table=cbind(table,ptable)
 		}
-		if(is.null(table)) table=ptable
-		else table=cbind(table,ptable)
 	}
+
+	#TODO check portfolio and account currencies and convert if necessary
+	
+	#TODO handle additions and withdrawals in equity
+		
+	if(!is.null(attr(Account,'initEq'))){
+		initEq<-as.numeric(attr(Account,'initEq'))
+		if(initEq==0) stop("Initial equity of zero would produce div by zero NaN,Inf,-Inf returns, please fix in initAcct().")
+		table = table/initEq
+	}
+	
 	return(table)
 }
 

Modified: pkg/quantstrat/demo/faber.R
===================================================================
--- pkg/quantstrat/demo/faber.R	2013-03-27 14:18:03 UTC (rev 1415)
+++ pkg/quantstrat/demo/faber.R	2013-03-29 15:40:50 UTC (rev 1416)
@@ -140,14 +140,13 @@
 }
 
 ret1 <- PortfReturns('faber')
-ret1$total<-rowSums(ret1)
-ret1
+View(ret1)
 
 if("package:PerformanceAnalytics" %in% search() || require("PerformanceAnalytics",quietly=TRUE)){
 	getSymbols("SPY", src='yahoo', index.class=c("POSIXt","POSIXct"), from='1999-01-01')
 	SPY<-to.monthly(SPY)
 	SPY.ret<-Return.calculate(SPY$SPY.Close)
-	index(SPY.ret)<-index(ret1)
+	index(SPY.ret)<-c(as.Date('1999-01-01'),index(ret1))
 	dev.new()
 	charts.PerformanceSummary(cbind(ret1$total,SPY.ret), geometric=FALSE, wealth.index=TRUE)
 }



More information about the Blotter-commits mailing list