Skip to content

Instantly share code, notes, and snippets.

@jfreels
Created August 28, 2013 21:23
Show Gist options
  • Save jfreels/6371494 to your computer and use it in GitHub Desktop.
Save jfreels/6371494 to your computer and use it in GitHub Desktop.
Drawdown function for use with plyr / data.table
## Drawdown using plyr / data.table
vami<-function (ror) { cumprod(na.omit(ror) + 1) }
dd<-function (ror) { -(1 - vami(ror)/cummax(c(1, cummax(vami(ror))))[-1]) }
require(PerformanceAnalytics)
require(plyr)
require(reshape2)
require(data.table)
data(edhec)
edhec_melt<-melt(data.frame(date=index(edhec),coredata(edhec)),id.vars='date')
## plyr method
ddply(edhec_melt,.(variable),transform,dd=dd(value))
## data.table method
edhec_dt<-data.table(edhec_melt,key='variable')
edhec_dt[,dd:=dd(value),by=variable]
edhec_dt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment