[Returnanalytics-commits] r2208 - in pkg/PortfolioAnalytics/sandbox/attribution: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jul 26 14:08:47 CEST 2012
Author: ababii
Date: 2012-07-26 14:08:47 +0200 (Thu, 26 Jul 2012)
New Revision: 2208
Added:
pkg/PortfolioAnalytics/sandbox/attribution/R/AcctReturns.R
pkg/PortfolioAnalytics/sandbox/attribution/man/AcctReturns.Rd
Modified:
pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION
pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE
Log:
- Account returns function for the blotter package
- handles cashflows
- time-weighted returns and linked modified Dietz returns
Modified: pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION 2012-07-26 10:37:19 UTC (rev 2207)
+++ pkg/PortfolioAnalytics/sandbox/attribution/DESCRIPTION 2012-07-26 12:08:47 UTC (rev 2208)
@@ -25,7 +25,6 @@
URL: http://r-forge.r-project.org/projects/returnanalytics/
Copyright: (c) 2004-2012
Collate:
- 'AppraisalRatio.R'
'Attribution.geometric.R'
'attribution.levels.R'
'attribution.R'
@@ -44,3 +43,4 @@
'MarketTiming.R'
'Weight.level.R'
'Weight.transform.R'
+ 'AcctReturns.R'
Modified: pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE 2012-07-26 10:37:19 UTC (rev 2207)
+++ pkg/PortfolioAnalytics/sandbox/attribution/NAMESPACE 2012-07-26 12:08:47 UTC (rev 2208)
@@ -1,4 +1,4 @@
-export(AppraisalRatio)
+export(AcctReturns)
export(Attribution)
export(Attribution.geometric)
export(Attribution.levels)
Added: pkg/PortfolioAnalytics/sandbox/attribution/R/AcctReturns.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/R/AcctReturns.R (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/R/AcctReturns.R 2012-07-26 12:08:47 UTC (rev 2208)
@@ -0,0 +1,125 @@
+#' Calculate account returns
+#'
+#' Similar to the \code{PortfReturns}, but gives returns for the entire account
+#' and takes into account cashflows. Allows selecting between time-weighted
+#' returns and linked modified Dietz approach. If time-weighted method is
+#' selected, the returns are computed using: \deqn{r_{t}=\frac{V_{t}}{V_{t-1}+C_{t}}-1}
+#' where \eqn{V_{t}} - account value at time \eqn{t}, \eqn{C_{t}} - cashflow at
+#' time \eqn{t}.These returns then can be linked geometrically (for instance
+#' using \code{Return.cumulative} function from the \code{PerformanceAnalytics}
+#' package) to yield cumulative multiperiod returns:
+#' \deqn{1+r=\prod_{t=1}^{T}(1+r_{t})=\prod_{t=1}^{T}\frac{V_{t}}{V_{t-1}+C_{t}}}
+#' In case if there were no cashflows, the result reduces to simple one-period
+#' returns.
+#' If Modified Dietz method is selected, monthly returns are computed taking
+#' into account cashflows within each month:
+#' \deqn{r = \frac{V_{t}-V_{t-1}-C}{V_{t-1}+\sum_{t}C_{t}\times W_{t}}}
+#' where \eqn{C} - total external cash flows within a month,
+#' \eqn{C_{t}} - external cashflow on day \eqn{t},
+#' \eqn{W_{t}=\frac{TD-D_{t}}{TD}} - weighting ratio to be applied to external
+#' cashflow on day \eqn{t},
+#' \eqn{TD} - total number of days wihting the month,
+#' \eqn{D_{t}} - number of days since the beginning of the month including
+#' weekends and public holidays.
+#' Finally monthly Modified Dietz returns can also be linked geometrically.
+#'
+#' @aliases AcctReturns
+#' @param Account string name of the account to generate returns for
+#' @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 method Used to select between time-weighted and linked modified Dietz
+#' returns. May be any of: \itemize{\item timeweighted \item dietz} By default
+#' time-weigthed is selected
+#' @return returns xts with account returns
+#' @author Brian Peterson, Andrii Babii
+#' @seealso PortfReturns
+#' @references Christopherson, Jon A., Carino, David R., Ferson, Wayne E.
+#' \emph{Portfolio Performance Measurement and Benchmarking}. McGraw-Hill.
+#' 2009. Chapter 5 \cr Bacon, C. \emph{Practical Portfolio Performance
+#' Measurement and Attribution}. Wiley. 2004. Chapter 2 \cr
+#' @keywords portfolio returns
+#' @note
+#' TODO handle portfolio and account in different currencies (not hard, just not done)
+#'
+#' TODO explicitly handle portfolio weights
+#'
+#' TODO provide additional methods of calculating returns
+#'
+#' TODO support additions and withdrawals to available capital
+#' @export
+AcctReturns <-
+function(Account, Dates = NULL, Portfolios = NULL, method = c("timeweighted", "dietz"), ...)
+{ # @author Brian Peterson, Andrii Babii
+ aname <- Account
+ if(!grepl("account\\.", aname)){
+ Account <- try(get(paste("account", aname, sep = '.'), envir = .blotter))
+ } else{
+ Account <- try(get(aname, envir = .blotter))
+ }
+ 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.")
+ }
+ if(is.null(Portfolios)){
+ Portfolios = names(Account$portfolios)
+ }
+
+ # Get xts with net trading P&L for all portfolios associated with account
+ 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)
+ if(is.null(table)){
+ table=ptable
+ }
+ else{
+ table=cbind(table,ptable)
+ }
+ }
+ 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().")
+ }
+
+ #TODO check portfolio and account currencies and convert if necessary
+
+ CF = Account$summary$Additions - Account$summary$Withdrawals # Cashflows
+ V = initEq + reclass(rowSums(table), table) # Account values
+ method = method[1]
+
+ if (method == timeweighted){
+ # Time-weighted returns
+ returns = V / (lag(V) + CF) - 1
+ }
+
+ if (method == dietz){
+ # Linked modified Dietz
+ C = apply.monthly(CF, sum) # total monthly cashflow
+ V = apply.monthly(V, first) # monthly account values
+ cfweighted <- function(CF){
+ TD = ndays(CF) # total number of days within the period
+ # number of days since the beginning of the period
+ D = round(as.vector((index(CF) - index(CF)[1])/3600/24))
+ W = (TD - D) / TD # weights
+ cashfl = sum(CF * W) # weighted sum of cashflows within the period
+ return(cashfl)
+ }
+ cashfl = apply.monthly(CF, cfweighted)
+ returns = (V - lag(V) - C) / (lag(V) + cashfl) # Modified Dietz
+ }
+ }
+ return(returns)
+}
\ No newline at end of file
Added: pkg/PortfolioAnalytics/sandbox/attribution/man/AcctReturns.Rd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/attribution/man/AcctReturns.Rd (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/attribution/man/AcctReturns.Rd 2012-07-26 12:08:47 UTC (rev 2208)
@@ -0,0 +1,84 @@
+\name{AcctReturns}
+\alias{AcctReturns}
+\title{Calculate account returns}
+\usage{
+ AcctReturns(Account, Dates = NULL, Portfolios = NULL,
+ method = c("timeweighted", "dietz"), ...)
+}
+\arguments{
+ \item{Account}{string name of the account to generate
+ returns for}
+
+ \item{\dots}{any other passthru parameters (like
+ \code{native} for \code{.getBySymbol}}
+
+ \item{Dates}{xts style ISO 8601 date subset to retrieve,
+ default NULL (all dates)}
+
+ \item{Portfolios}{concatenated string vector for
+ portfolio names to retrieve returns on, default NULL (all
+ portfolios)}
+
+ \item{method}{Used to select between time-weighted and
+ linked modified Dietz returns. May be any of:
+ \itemize{\item timeweighted \item dietz} By default
+ time-weigthed is selected}
+}
+\value{
+ returns xts with account returns
+}
+\description{
+ Similar to the \code{PortfReturns}, but gives returns for
+ the entire account and takes into account cashflows.
+ Allows selecting between time-weighted returns and linked
+ modified Dietz approach. If time-weighted method is
+ selected, the returns are computed using:
+ \deqn{r_{t}=\frac{V_{t}}{V_{t-1}+C_{t}}-1} where
+ \eqn{V_{t}} - account value at time \eqn{t}, \eqn{C_{t}}
+ - cashflow at time \eqn{t}.These returns then can be
+ linked geometrically (for instance using
+ \code{Return.cumulative} function from the
+ \code{PerformanceAnalytics} package) to yield cumulative
+ multiperiod returns:
+ \deqn{1+r=\prod_{t=1}^{T}(1+r_{t})=\prod_{t=1}^{T}\frac{V_{t}}{V_{t-1}+C_{t}}}
+ In case if there were no cashflows, the result reduces to
+ simple one-period returns. If Modified Dietz method is
+ selected, monthly returns are computed taking into
+ account cashflows within each month: \deqn{r =
+ \frac{V_{t}-V_{t-1}-C}{V_{t-1}+\sum_{t}C_{t}\times
+ W_{t}}} where \eqn{C} - total external cash flows within
+ a month, \eqn{C_{t}} - external cashflow on day \eqn{t},
+ \eqn{W_{t}=\frac{TD-D_{t}}{TD}} - weighting ratio to be
+ applied to external cashflow on day \eqn{t}, \eqn{TD} -
+ total number of days wihting the month, \eqn{D_{t}} -
+ number of days since the beginning of the month including
+ weekends and public holidays. Finally monthly Modified
+ Dietz returns can also be linked geometrically.
+}
+\note{
+ TODO handle portfolio and account in different currencies
+ (not hard, just not done)
+
+ TODO explicitly handle portfolio weights
+
+ TODO provide additional methods of calculating returns
+
+ TODO support additions and withdrawals to available
+ capital
+}
+\author{
+ Brian Peterson, Andrii Babii
+}
+\references{
+ Christopherson, Jon A., Carino, David R., Ferson, Wayne
+ E. \emph{Portfolio Performance Measurement and
+ Benchmarking}. McGraw-Hill. 2009. Chapter 5 \cr Bacon, C.
+ \emph{Practical Portfolio Performance Measurement and
+ Attribution}. Wiley. 2004. Chapter 2 \cr
+}
+\seealso{
+ PortfReturns
+}
+\keyword{portfolio}
+\keyword{returns}
+
More information about the Returnanalytics-commits
mailing list