3

In R, I see there are two packages that have a maxdrawdown function.

One is fTrading and the other is PerformaceAnalytics.

Each of those does a different calculation.

  1. fTrading seems to make the assumption that the values are prices of an asset so the drawdown is in valuation.
  2. The same applies to PerformanceAnalytics except it gives the answer as a percentage.

Is there a package with a maxdrawdown function that expects P/L data in a series and gives the draw down based on that?

e.g if you had the following data

c(12,10,5,-4,-2,1,5,6)

The max drawdown would be :

-4 + -2=-6.
3
  • I usually use maxdrawdown from the tseries package ...
    – GSee
    Dec 5, 2012 at 23:02
  • the one from tseries is exactly the same as the one from fTrading Dec 6, 2012 at 13:27
  • I was really just pointing out another package with a maxdrawdown function that you didn't mention. fTrading::maxDrawdown is based on tseries::maxdrawdown (which is older). The advantage to using tseries is that it doesn't Depend on several packages that you might not want to install and load.
    – GSee
    Dec 6, 2012 at 13:40

2 Answers 2

7

It's easy to write your own function:

drawdown <- function(pnl) {
   cum.pnl  <- c(0, cumsum(pnl))
   drawdown <- cum.pnl - cummax(cum.pnl)
   return(tail(drawdown, -1))
}

maxdrawdown <- function(pnl)min(drawdown(pnl))

(Of course, you can change the sign and replace min by max if your convention is that drawdown should be a positive quantity)

pnl <- c(12,10,5,-4,-2,1,5,6)
drawdown(pnl)
# [1]  0  0  0 -4 -6 -5  0  0
maxdrawdown(pnl)
# [1] -6
3
  • 1
    how would you get it as a percentage? Mar 6, 2015 at 0:07
  • You can get percentage values by "pnl / total_investment". Where, total_investment is your invested amount.
    – Ganesh S
    Apr 24, 2021 at 13:48
  • Could you please explain how the last line works? maxdrawdown <- function(pnl)min(drawdown(pnl)) Sep 17, 2022 at 15:24
4

You're trying to find the maxDrawDown of the cumsum of PnL (which is the same as the account value).

> library(fTrading)
> maxDrawDown(cumsum(c(0, c(12,10,5,-4,-2,1,5,6))))
$maxdrawdown
[1] 6

$from
[1] 3

$to
[1] 5
1
  • To be exact, it should be maxDrawDown(cumsum(0, pnl)) to account for the possibility that the max value at any point is V(t=0) = 0. It is more obvious if you test with pnl <- c(-30, 30, 12,10,5,-4,-2,1,5,6)
    – flodel
    Dec 6, 2012 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.