1

How to convert the following data into zoo/xts/tseries object to calculate maximum drawdown by using the function Return.calculate and Maxdrawdown function?

Hello friends i have equity(eg CENTURYTEX,AAPL,RIL etc) specific eod prices for around 400 equities in a single csv.

I want to calculate maximum drawdown. To calculate returns i am trying

ret=by[ddr3csv[,4],ddr3csv[,2],
                   function(x)Return.calculate(x,method="simple")]

which is giving 'closure' is not subsettable' error.

Once returns are obtained finding maximum drawdown will be easy.I am searching for the error but not able to figure out much.

rownum     name     code       date    price    readings

86916 CENTURYTEX 500040 11/04/2011 364.60     2994

86917 CENTURYTEX 500040 13/04/2011 376.70     2994

86918 CENTURYTEX 500040 15/04/2011 370.90     2994

86919 CENTURYTEX 500040 18/04/2011 365.85     2994
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • If you various stock prices in single csv file, how are you going to calculate maximum drawdown? – CHP Feb 27 '13 at 05:02
  • Do you calculate the maximum drawdown for each returns? You must add also that you use `PerformanceAnalytics package. – agstudy Feb 27 '13 at 05:11
  • @agstudy Oh sorry i forgot ot add that i am using performance analytics package. – user2110832 Feb 27 '13 at 06:16
  • @geektrader yes sir i have around 400 equities in a single file and want to calculate drawdown for each of them. for each equity the records are ordered – user2110832 Feb 27 '13 at 06:18

1 Answers1

2

First of all you need to change [ by (( , so

ret=by[ddr3csv[,4],ddr3csv[,2],
                   function(x)Return.calculate(x,method="simple")]

To

library(PerformanceAnalytics)
ret=by(ddr3csv[,4],ddr3csv[,2],FUN=
                   function(x)Return.calculate(x,method="simple"))

Without data , I can't do beter. I imagine that you put your prices in the long format and use colmun 2 of your csv to separate between different stocks.

EDIT

You need to :

  • split your data by code
  • create an xts object for each group
  • Compute the drawdows components.

    ## I read the data 
    ddr3csv <- read.table(text = 'rownum     name     code       date    price    readings
                                  86916 CENTURYTEX 500040 11/04/2011 364.60     2994
                                  86917 CENTURYTEX 500040 13/04/2011 376.70     2994
                                  86918 CENTURYTEX 500040 15/04/2011 370.90     2994
                                  86919 CENTURYTEX 500040 18/04/2011 365.85     2994',head=T)
    ## I coerce to Date , because xts needs numeric values as index
    ddr3csv$date<- as.Date(dat$date,format='%d/%m/%Y')
    ## I keep only computational columns (avoid naming/memory problems)
    dat <- ddr3csv[,c('code','date','price')]
    library(plyr)
    ddply(dat,.(code), function(x){
              x.xts <- xts(x$price,x$date)
             unlist(findDrawdowns (Return.calculate(x.xts,method="simple")))
    })
    

here my result

    code return from trough to length peaktotrough recovery
    1 500040      0    1      1  4      4            1        3
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • yes sir column 2 or column 3 can be used to distinguish between equitiies. basically only column 3,4,5 are important where column 3 is used for distinguishing. I tried your solution but its giving Error in UseMethod("xtsAttributes<-") : no applicable method for 'xtsAttributes<-' applied to an object of class "zoo". – user2110832 Feb 27 '13 at 06:24
  • It is anther problem. I can't help you without knowing what is ddr3csv object? is it the data.frame you show or other thing? can you please add to your question , the resulat of this : `dput(head(ddr3csv))` or at least `str(ddr3csv)` – agstudy Feb 27 '13 at 06:28
  • str(ddr3csv) 'data.frame': 1048575 obs. of 5 variables: $ name : Factor w/ 374 levels "ABB","ABBOTINDIA",..: 260 260 260 260 260 260 260 260 260 260 ... $ code : int 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 ... $ date : Factor w/ 1498 levels "01/01/2008","01/01/2009",..: 91 142 194 343 394 446 494 545 691 738 ... $ price : num 14015 13872 13861 13652 13566 ... $ readings: int 1498 1498 1498 1498 1498 1498 1498 1498 1498 1498 ... – user2110832 Feb 27 '13 at 07:11
  • i obtained ddr3csv by ddr3csv=read.csv("dd2.csv",header=T,sep="\t") – user2110832 Feb 27 '13 at 07:13
  • thanks a lot sir its working,i used maxDrawdown function in place of findDrawdowns – user2110832 Feb 27 '13 at 09:58
  • @user2110832 don(t forget to check the solution if your are stasified with this answer. – agstudy Feb 28 '13 at 00:17