Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • maximum drawdown

    Hi,

    I want to compute the maximum drawdown for a series of returns.
    My data looks like this:

    Code:
     date           return
    1990m1          0.01
    1990m2         -0.02
    1990m3         -0.04
    1990m4          0.01
    in this example the maximum drawdown would be 0.98*0.96=0.9408=> 5.92% !

    I tried something like mvsumm (sum) with ln(return+1), but I dont know how to specify the function, so it stops if a return is positive and starts all over when a negative return series starts again.

  • #2
    Code:
    . gen return2 = 1 + return if return<0
    (2 missing values generated)
    
    . replace return2 = return2 * return2[_n-1] if return2[_n-1]<.
    (1 real change made)
    
    . gen drawdown = (1 - return2) * 100
    (2 missing values generated)
    
    . sum drawdown
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
        drawdown |          2    3.959999    2.771859   1.999998   5.919999
    Last edited by Friedrich Huebler; 27 Jun 2015, 09:43. Reason: Error in code corrected

    Comment


    • #3
      Your code does exactly what I have asked for. It computes the maximum of a series of negative returns.
      However, I think I misstated the maximum drawdown.There can actually be positive values in between. I found the following pseudocode for the real maximum drawdown:
      Code:
      MDD = 0
      peak = -99999
      for i = 1 to N step 1  
      if (NAV[i] > peak) # peak will be the maximum value seen so far (0 to i)    
      peak = NAV[i]  
      endif  
      DD[i] = 100.0 * (peak - NAV[i]) / peak  
      if (DD[i] > MDD) # Same idea as peak variable, MDD keeps track of the maximum drawdown so far.  
      MDD = DD[i]  
      endif
      endfor
      As I have no experience with loops, I dont get it to work. Here is what I have been tried to do it with:
      Code:
      gen MDD = 0
      gen peak = -99999
      foreach i = 1/N {
      replace peak=NAV`i' if NAV`i'>peak
          
       
       gen DD`i' = 100.0 * (peak - NAV`i') / peak
       replace MDD=DD`i' if DD`i' > MDD
        
      }
      I get invalid syntax
      Can somebody help me translate the code from above into stata?
      Last edited by Friedrich Franz; 28 Jun 2015, 06:38.

      Comment


      • #4
        There are several problems here.

        1. The foreach syntax is illegal and is perhaps based on confusion with forval.

        2. Neither loop construct will evaluate any N for you; invoking N is in turn perhaps based on a confusion with _N. You nowhere show your data structure or explain what you imagine N to be, but at a guess you are hoping that somehow N is magically the number of observations.

        3. The code inside the loop would set up a new variable for every observation holding the same constant (except that NAV`i' would not exist if you had not created it). Without anything else said, that is a bad idea any way and may well fail if you breach Stata's limits on number of variables. More fundamentally, you are confusing subscripting with creating a new variable. Or so it seems.

        4. I think you are trying to subscript without subscripting. The pseudocode you cite does use subscripting in a way similar to Stata's subscripting, but you don't translate it. The statements inside the loop won't automatically change even if the loop index is over observations.

        If I understand your problem it is this.

        Some variable NAV.

        You want to keep track of the highest value so far. That doesn't in fact need an explicit loop at all, as Stata will loop over observations automatically given

        Code:
        gen peak = NAV in 1
        replace peak = max(peak[_n-1], NAV) in 2/L
        (naturally this code depends on observations being in the right sort order).

        This has been an FAQ since 2006.
        http://www.stata.com/support/faqs/da...m-of-sequence/

        Recall that the FAQ for this list advise consulting FAQs before posting.

        Drawdown then appears to be

        Code:
        gen DD = 100.0 * (peak - NAV) / peak
        and the maximum drawdown so far follows from the first code block

        Code:
        gen maxDD = DD in 1  
        replace maxDD = max(maxDD[_n-1], DD) in 2/L
        No doubt you will flag if I am misunderstanding anything.

        If you have panel structure, the code will need to be generalised accordingly.
        Last edited by Nick Cox; 28 Jun 2015, 08:15.

        Comment

        Working...
        X