Calculate the rolling drawdown of returns in R

1.4k views Asked by At

The XTS is called 'x' and has the daily returns of seven currencies.

x=structure(c(0, 0, -0.00237747604278071, -4.10901316858503e-05, 
-0.00292075554430804, -0.00327746821541597, 0, 0, -0.0024832242245989, 
-7.13774881431206e-05, -0.00288771987280823, -0.00239072530655426, 
0, 0, -0.00238446679144366, -0.00169293890925615, -0.0036441365731773, 
-0.00370471543592221, 0, 0, -0.00244664834224606, 0, -0.00285354593582876, 
-0.00288034192721653, 0, 0, -0.00310375411764707, -0.00146643546387215, 
-0.00427725501167975, -0.00376793265740194, 0, 0, -0.00183281566844917, 
0, -0.00294652695187159, -0.0021703202366018, 0, 0, -0.00243030417112311, 
-0.000372071536924534, -0.00330455837816801, -0.00245257154873846
), class = c("xts", "zoo"), index = structure(c(1297296000, 1297382400, 
1297641600, 1297728000, 1297814400, 1297900800), tzone = "UTC", tclass = "Date"), .Dim = 6:7, .Dimnames = list(
    NULL, c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")))

I've used the PerformanceAnalytics function Drawdowns to calculate and plot the cumulative drawdown which is fine. However, I would also like a rolling 60 day drawdown and tried both rollapply and apply.rolling but no luck.

Can anyone please recommend a solution?

structure(c(0.00371570839572193, 0.00118836834224599, -0.00237747604278075, 
0.00234195385026738, -0.00287978374331551, -0.000357757593582888, 
0.00265543171122995, 0.000720424064171123, -0.00248322422459893, 
0.00241785080213904, -0.00281654342245989, 0.000498433903743315, 
0.00302572497326203, 0.00161579593582888, -0.00238446679144385, 
0.000693180748663102, -0.00195450652406417, -6.08004278074866e-05, 
0.00387943005347594, 0.00128238518716578, -0.00244664834224599, 
0.00274308647058824, -0.00285354593582888, -2.68726737967914e-05, 
0.00367270588235294, 0.00235053556149733, -0.00310375411764706, 
0.00164241631016043, -0.00281494748663102, 0.000511510213903743, 
0.00330757946524064, 0.00212843374331551, -0.0018328156684492, 
0.00260363310160428, -0.00294652695187166, 0.000778500588235294, 
0.00273476534759358, 0.000962438395721925, -0.00243030417112299, 
0.00206324695187166, -0.00293357834224599, 0.000854811604278075
), class = c("xts", "zoo"), index = structure(c(1297296000, 1297382400, 
1297641600, 1297728000, 1297814400, 1297900800), tzone = "UTC", tclass = "Date"), .Dim = 6:7, .Dimnames = list(
    NULL, c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")))

rollapply(x, width = 60, FUN = Drawdowns)

Error in array(ans, c(len.a%/%d2, d.ans), if (!is.null(names(dn.ans)) || : length of 'dimnames' [1] not equal to array extent

apply.rolling(x, width = 60, FUN = SharpeRatio)

Error in rbind(calcs, calc) : number of columns of matrices must match (see arg 2)

1

There are 1 answers

0
tester On

here's a suggestion using the xts and PerformanceAnalytics packages: I create a xts-object with simulated returns and apply the rollapply function from the zoo package (loaded with xts) with a width of 25 days.

tester

library(xts)
library(PerformanceAnalytics)

set.seed(123)
returns <- matrix(rnorm(n = 365*7, mean = 0.001, sd = 0.002), ncol = 7)
timeindex <- seq.Date(as.Date('2019-01-01'), to = as.Date('2019-12-31'), by = 'days')
test_xts <- xts(x = returns,
                order.by = timeindex)
colnames(test_xts) <- c("AUD", "EUR", "GBP", "JPY", "KRW", "TWD", "USD")

rolling_maxDD <- rollapply(test_xts, width = 25, FUN = maxDrawdown)

plot.xts(-rolling_maxDD, legend.loc = 'bottomleft')