[Rd] Convention difference in tseries.maxdrawdown (PR#8872)

rproject at boonstra.org rproject at boonstra.org
Wed May 17 22:37:30 CEST 2006


Full_Name: Brian K. Boonstra
Version: 2.2.1
OS: WinXP, OSX
Submission from: (NULL) (63.172.178.137)


The maxdrawdown function in tseries defines the maximum drawdown in terms of
absolute dollars (or whatever units the input is in).  Industry convention is to
do this in percentage terms.  I have written the code below as
maximumdrawdown(), which retains backward compatibility with the current
version.  It has the flaw that it does not check for zero or negative values.

maximumdrawdown <- function (x) 
{
    if (NCOL(x) > 1) 
        stop("x is not a vector or univariate time series")
    if (any(is.na(x))) 
        stop("NAs in x")
    cminx <- x/cummax(x)
    mdd <- min(cminx)
    to <- which(mdd == cminx)
    from <- double(NROW(to))
    for (i in 1:NROW(to)) {
      from[i] <- max( which(cminx[1:to[i]] == 1) )
      }
    return(list(maximumdrawdown = 1-mdd, maxdrawdown = (1-mdd)*x[from], from =
from, to = to))
}



More information about the R-devel mailing list