Calculating peak-to-trough drawdown

Ok, so this is a little bit technical but it’s an intriguing puzzle that got me thinking quite hard. So here’s the problem. Sometimes investors want to be able to judge what the absolute worst case scenario would have been if they’d invested in something. Look at the following random graph of pretend asset prices:

Peak-To-Trough

You’ll see that there are two points on the graph (marked in red) where if you had invested at the first point and pulled out on the second point you would have the worst-case loss. This is the point of this analysis and is a way for investors in the asset to see how bad, ‘bad’ has really been in the past. Clearly past prices are not an indicator of future losses. 🙂

The upper one is the ‘peak’ and the lower one is the ‘trough’. Well, finding these two babys by eye is trivial. To do it reliably (and quickly) on a computer, is not that straight forward. Part of the problem is coming up with a consistent natural language of what you want your peak and trough to be. This took me some time. I believe what I really want is: the largest positive difference of high minus low where the low occurs after the high in time-order. This was the best I could do. This led to the first solution (in Python):


def drawdown(prices):
	maxi = 0
	mini = 0
	for i in range(len(prices))[1:]:
	   maxj = 0
	   max = 0
	   for j in range(i+1, len(prices)):
		if prices[i] - prices[j] > max:
		    maxj = j
		    max = prices[i] - prices[j]
	   if max > prices[maxi] - prices[mini]:
	   	maxi = i
		mini = maxj
	return (prices[maxi], navs[mini])

Now this solution is easy to explain. It’s what I have come to know as a ‘between’ analysis. I don’t know if that’s the proper term but it harks back to the days when I used to be a number-cruncher for some statisticians. The deal is relatively straight-forward: compare the fist item against every item after it in the list and store the largest positive difference. If this difference is also the largest seen in the data-set so far then make it the largest positive difference of all points. At the end you just return the two points you found. This is a natural way to solve the problem because it looks at all possible start points and assesses what the worst outcome would be.

The problem with this solution is that it has quadratic complexity. That is for any data-series of size N the best and worst case will result in N * N-1 iterations, in shorthand this is O(N^2). For small n this doesn’t really matter, but for any decently sized data-series this baby will be slow-as-molasses. The challenge then is to find an O(N) solution to the problem and to save-those-much-needed-cycles for something really important:


def drawdown(prices):
  prevmaxi = 0
  prevmini = 0
  maxi = 0

  for i in range(len(prices))[1:]:
    if prices[i] >= prices[maxi]:
      maxi = i
    else:
      # You can only determine the largest drawdown on a downward price!
      if (prices[maxi] - prices[i]) > (prices[prevmaxi] - prices[prevmini]):
	prevmaxi = maxi
	prevmini = i
      return (prices[prevmaxi], prices[prevmini])

This solution is a bit harder to explain. We move through the prices and the first part of the ‘if’ will find the highest part of the peak so far. However, the second part of the ‘if’ is where the magic happens. If the next value is less than the maximum then we see if this difference is larger than any previously encountered difference, if it is then this is our new peak-to-trough.

The purist in me likes that fact that the O(N) solution looks like easier code to understand than the O(N^2) solution. Although the O(N^2) solution is, I think, an easier concept to grapple with, when it’s translated into code it just doesn’t grok.

12 thoughts on “Calculating peak-to-trough drawdown”

  1. Hi Steve,

    (Apologies if this is a repost – it hasn’t popped up yet, so having to retype)… The second solution – isn’t it a case of two if’s?

    My python is pretty weak (har har), but if I’ve translated correctly into C#, doesn’t the below code do the same, but in a simpler fashion?

    
            public static double DrawDown( double[] prices, out double minValue, out double maxValue )
            {
                maxValue = double.MinValue;
                minValue = double.MaxValue;
    
                for( int i = 0; i < prices.Length; i++ )
                {
                    if( prices[i] > maxValue )
                        maxValue = prices[i];
                    if( prices[i] < minValue )
                        minValue = prices[i];
                }
                return maxValue - minValue;
            }
    
  2. Hi Chris,

    Yes, what you’ve posted kind-of makes the point for me that this problem is harder than it looks. With your solution you’ll find that if the low comes before the high you’ll be showing a peak-to-trough that could never have occurred. In this case you’ll be showing the trough-to-peak gain! Which investors care about too but not for this measure.

    Steve

  3. Working on max drawdown problem and came across a case that I’m baffled with. If your data set has a peak value occuring on the very last value then you will have to trough value would the drawdown be 0 or would you use the previous peak/trough values which your solution provides ?

    Thanks
    Andy

  4. Hi Andy,

    Well the peak-to-trough draw-down is a metric that might tell an investor what’s the absolute worst-case loss ever seen historically. Therefore I would always expect the peak to happen before the trough, therefore a trailing peak should have no effect on the result.

    Hope that helps,

    Steve

  5. It is not nearly that complicated, it can also be done in excel in seconds.
    Step 1) Take first data point set as high
    Step 2) run if statement that if n+1 data point is > than n data point, n+1 data point is new high
    Step 3) take [(n / step 2) – 1] this gives you your % drawdown
    Step 4) take the minimum value achieved from Step 3

  6. Hi Chris,

    Yes, that’s easier, but this is not peak-to-trough draw-down as I defined it. This procedure will find the largest price difference.

    To appreciate the difference, consider what would happen if the minimum value is before the highest point. This is not draw-down because an investor could never ‘experience’ this loss.

    Once you bear this in mind you will realise that the peak-to-trough may not actually involve the highest point in the series at-all. Any local maximum can be the highest point as long as it’s followed by a local minimum that is low enough.

    Hope that helps

    Steve

  7. Hmmm, this is interesting but I’m not sure imposing temporal order on the data will satisfy your investors requirements. I would think most investors want to know what could be not what would have been if they had have done something (bought x time ago) that they didn’t actually do. In that case it becomes a question of variability regardless of temporal order. As you point out assumptions of historical prices (and events surrounding them) repeating over time is not all that sound but if the assumption is made (eg the highest high could happen again) then I would have thought the biggest difference looking at all the trends (either high to low or low to high remember we’re assuming rightly or wrongly the highs and lows are repeatable so if something has been so low before it can/will be again)is the worst case scenario.

  8. Gonz Thanks for your comment. Yes, I agree it’s not the greatest of measures. But it does appear quite a lot in hedge-fund prospectuses and the meaning I’ve given is the one that investors expect. Whether they place any weight behind it is another matter!

  9. Just stumbled on this but I wanted to share my solution:

    
    def bad(list):
        maxi = 0
        mini = 0
        loss = 0
        maxLoss = 0
        for i, x in enumerate(list):
            maxi = max(x, maxi)
            mini = min(list[i:])
            loss = maxi - mini
            maxLoss = max(maxLoss,loss)
        return maxLoss
    

    This walks through the list, updating the maximum point it has been through, and comparingit to the minimum from the remaining list, calculating a maximum loss at every step through the list. The maxLoss also updates to be the biggest value yet reached as the walk continues.

  10. Hello,

    Disclaimer: new to programming so apologies if I do something stupid. Running both codes on the same profit line gives me two different results, so I am afraid something is wrong. Below you find the code.

    Regards,

    Tirroy

    profitline = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, 2.0600000000000023, 2.0600000000000023, 2.420000000000016, 2.420000000000016, 2.420000000000016, 2.420000000000016, 2.420000000000016, 3.719999999999999, 5.02000000000001, 8.320000000000022, 10.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 12.420000000000044, 14.32000000000005, 18.420000000000044, 22.520000000000067, 22.520000000000067, 22.520000000000067, 22.520000000000067, 22.520000000000067, 22.520000000000067, 22.920000000000044, 23.420000000000044, 24.420000000000044, 25.220000000000056, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 26.12000000000006, 25.920000000000073, 25.62000000000006, 25.420000000000044, 25.02000000000004, 24.420000000000044, 24.12000000000006, 24.12000000000006, 24.62000000000006, 25.520000000000067, 25.920000000000073, 26.62000000000009, 26.62000000000009, 26.62000000000009, 26.62000000000009, 26.62000000000009, 26.62000000000009, 26.020000000000095, 25.580000000000098, 25.580000000000098, 26.480000000000103, 27.9200000000001, 28.82000000000008, 28.82000000000008, 28.82000000000008, 28.82000000000008, 28.82000000000008, 28.82000000000008, 29.420000000000073, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.760000000000076, 30.260000000000076, 29.060000000000088, 28.260000000000076, 28.260000000000076, 28.160000000000053, 28.460000000000065, 28.220000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 29.720000000000056, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 30.520000000000067, 29.020000000000067, 28.020000000000067, 26.86000000000007, 26.160000000000082, 26.460000000000093, 28.060000000000088, 27.8600000000001, 27.160000000000082, 27.160000000000082, 26.660000000000082, 26.3600000000001, 27.260000000000105, 27.960000000000093, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 28.86000000000007, 29.260000000000048, 29.260000000000048, 29.260000000000048, 29.260000000000048, 29.260000000000048, 29.260000000000048, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.260000000000034, 30.860000000000042, 31.680000000000035, 31.68000000000005, 31.68000000000005, 31.68000000000005, 31.68000000000005, 31.68000000000005, 31.68000000000005, 31.68000000000005, 31.68000000000005, 31.68000000000005, 32.98000000000005, 32.98000000000005, 32.98000000000005, 32.98000000000005, 32.98000000000005, 32.98000000000005, 33.58000000000004, 34.28000000000006, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.78000000000007, 34.280000000000086, 33.880000000000095, 33.580000000000084, 33.4800000000001, 33.48000000000009, 34.48000000000009, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.080000000000084, 34.88000000000008, 34.88000000000008, 35.380000000000095, 35.78000000000007, 37.88000000000008, 37.88000000000008, 37.88000000000008, 37.88000000000008, 37.88000000000008, 37.88000000000008, 37.88000000000008, 37.88000000000008, 38.68000000000009, 38.68000000000009, 38.68000000000009, 38.68000000000009, 38.68000000000009, 38.68000000000009, 38.68000000000009, 38.68000000000009, 39.680000000000106, 39.680000000000106, 39.680000000000106, 39.680000000000106, 39.680000000000106, 39.680000000000106, 39.680000000000106, 38.880000000000095, 37.38000000000011, 37.08000000000011, 37.08000000000011, 37.7000000000001, 37.80000000000011, 37.500000000000114, 37.38000000000011, 37.38000000000011, 37.38000000000011, 37.4800000000001, 37.4800000000001, 37.7800000000001, 38.080000000000126, 38.080000000000126, 38.080000000000126, 38.080000000000126, 38.080000000000126, 38.080000000000126, 38.080000000000126, 36.28000000000014, 35.480000000000146, 34.78000000000013, 35.18000000000015, 34.680000000000135, 33.98000000000013, 33.580000000000126, 34.180000000000106, 35.0800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 35.5800000000001, 34.88000000000011, 33.9800000000001, 32.9800000000001, 32.180000000000106, 31.18000000000012, 30.68000000000012, 30.180000000000135, 30.18000000000012, 30.18000000000012, 30.980000000000132, 31.680000000000135, 31.780000000000115, 31.780000000000115, 31.780000000000115, 31.780000000000115, 31.780000000000115, 31.780000000000115, 33.12000000000013, 35.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.42000000000013, 38.52000000000014, 39.02000000000014, 38.62000000000012, 38.02000000000011, 38.02000000000011, 37.520000000000095, 37.22000000000011, 36.82000000000011, 37.02000000000011, 37.420000000000115, 38.08000000000011, 38.08000000000011, 38.08000000000011, 38.08000000000011, 38.08000000000011, 38.08000000000011, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 39.88000000000012, 40.080000000000126, 40.58000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.38000000000011, 41.0800000000001, 40.48000000000009, 40.280000000000086, 40.48000000000009, 40.780000000000086, 41.780000000000086, 42.5800000000001, 43.5800000000001, 43.5800000000001, 43.5800000000001, 43.5800000000001, 43.5800000000001, 43.5800000000001, 43.680000000000106, 43.88000000000008, 43.980000000000075, 44.280000000000086, 44.880000000000095, 44.880000000000095, 44.880000000000095, 44.880000000000095, 44.880000000000095, 44.880000000000095, 44.880000000000095, 44.880000000000095, 44.880000000000095, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 45.18000000000009, 44.58000000000007, 43.58000000000007, 43.28000000000006, 43.180000000000064, 42.58000000000007, 41.980000000000075, 41.68000000000008, 41.88000000000007, 41.68000000000005, 41.26000000000005, 40.66000000000005, 40.86000000000007, 41.480000000000075, 42.480000000000075, 43.08000000000007, 43.08000000000007, 43.08000000000007, 43.08000000000007, 43.08000000000007, 43.08000000000007, 43.08000000000007, 43.08000000000007, 43.08000000000007, 42.98000000000006, 42.78000000000006, 42.68000000000008, 43.18000000000008, 43.18000000000008, 43.58000000000007, 43.58000000000007, 43.58000000000007, 43.58000000000007, 43.58000000000007, 43.58000000000007, 43.200000000000074, 43.200000000000074, 43.200000000000074, 43.18000000000008, 42.88000000000008, 42.88000000000008, 42.680000000000064, 42.680000000000064, 42.38000000000007, 42.080000000000055, 41.98000000000005, 42.08000000000007, 42.48000000000005, 42.780000000000044, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.38000000000005, 43.88000000000005, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 44.08000000000004, 43.38000000000005, 43.48000000000006, 43.78000000000007, 44.080000000000084, 44.080000000000084, 44.080000000000084, 44.080000000000084, 44.080000000000084, 44.080000000000084, 44.080000000000084, 44.080000000000084, 43.780000000000086, 43.880000000000095, 44.18000000000009, 44.5800000000001, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 44.98000000000009, 45.08000000000011, 45.5800000000001, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.68000000000009, 46.58000000000007, 46.38000000000008, 46.38000000000007, 46.780000000000086, 46.880000000000095, 46.880000000000095, 46.880000000000095, 46.880000000000095, 46.880000000000095, 46.880000000000095, 46.880000000000095, 46.88000000000008, 46.88000000000008, 46.88000000000008, 46.480000000000075, 46.44000000000007, 46.340000000000074, 46.500000000000085, 46.54000000000006, 47.04000000000006, 47.580000000000055, 47.580000000000055, 47.580000000000055, 47.580000000000055, 47.580000000000055, 47.580000000000055, 46.88000000000005, 46.68000000000005, 46.48000000000003, 46.48000000000002, 45.88000000000001, 45.68000000000001, 45.68000000000001, 46.18000000000002, 46.18000000000002, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.58000000000001, 46.68000000000001, 46.68000000000001, 46.68000000000001, 46.68000000000001, 46.68000000000001, 46.78, 46.780000000000015, 46.48000000000003, 46.08000000000004, 45.48000000000003, 44.28000000000003, 43.48000000000003, 42.98000000000003, 43.48000000000002, 43.880000000000024, 44.18000000000001, 44.18000000000001, 44.48000000000002, 44.48000000000002, 44.48000000000002, 44.48000000000002, 44.48000000000002, 44.48000000000002, 44.48000000000002, 44.48000000000002, 44.380000000000024, 44.58000000000003, 44.480000000000004, 44.280000000000015, 44.08000000000001, 44.58000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.28000000000003, 45.880000000000024, 45.98000000000002, 45.98000000000002, 45.98000000000002, 45.880000000000024, 45.880000000000024, 45.880000000000024, 45.880000000000024, 45.880000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.380000000000024, 46.48000000000002, 46.58000000000001, 44.58000000000001, 42.38000000000001, 40.08, 39.579999999999984, 39.77999999999997, 39.67999999999998, 40.17999999999998, 40.079999999999984, 40.47999999999999, 40.47999999999999, 40.47999999999999, 40.47999999999999, 39.77999999999999, 39.77999999999999, 39.379999999999995, 39.38000000000001, 39.97999999999999, 40.079999999999984, 39.879999999999995, 39.879999999999995, 39.480000000000004, 39.480000000000004, 38.980000000000004, 38.18000000000001, 38.280000000000015, 38.280000000000015, 38.78, 38.780000000000015, 39.08000000000001, 39.08000000000001, 39.08000000000001, 39.08000000000001, 39.08000000000001, 39.08000000000001, 39.17999999999999, 39.17999999999999, 38.47999999999999, 37.480000000000004, 36.98000000000002, 36.280000000000015, 35.780000000000015, 35.28000000000003, 35.880000000000024, 36.48000000000005, 37.08000000000004, 37.08000000000004, 37.08000000000004, 37.08000000000004, 37.08000000000004, 37.08000000000004, 36.78000000000006, 37.080000000000055, 37.48000000000006, 37.680000000000035, 37.48000000000005, 37.48000000000005, 37.48000000000005, 37.48000000000005, 37.48000000000005, 37.48000000000005, 37.48000000000005, 37.48000000000005, 37.68000000000005, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 38.580000000000055, 37.88000000000007, 38.38000000000007, 38.18000000000008, 37.980000000000075, 37.38000000000008, 37.48000000000006, 37.68000000000008, 37.7800000000001, 37.7800000000001, 37.7800000000001, 37.7800000000001, 37.7800000000001, 37.7800000000001, 37.380000000000095, 37.180000000000106, 37.080000000000084, 37.18000000000009, 36.88000000000011, 36.5800000000001, 35.880000000000095, 35.9800000000001, 36.2800000000001, 36.68000000000009, 36.5800000000001, 36.48000000000009, 36.48000000000009, 36.48000000000009, 36.48000000000009, 36.2800000000001, 36.68000000000009, 37.18000000000008, 37.74000000000008, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 38.040000000000106, 37.640000000000086, 37.04000000000009, 37.04000000000008, 37.34000000000006, 37.54000000000005, 37.54000000000005, 37.60000000000005, 37.80000000000004, 37.80000000000004, 37.80000000000004, 37.80000000000004, 37.70000000000003, 37.70000000000003, 37.80000000000001, 38.0, 38.29999999999998, 38.29999999999998, 38.29999999999998, 38.29999999999998, 38.89999999999999, 38.89999999999999, 39.3, 39.3, 39.3, 39.3, 39.3, 39.3, 39.29999999999998, 39.29999999999998, 39.69999999999999, 40.099999999999994, 40.400000000000006, 40.400000000000006, 40.400000000000006, 40.400000000000006, 40.400000000000006, 40.400000000000006, 40.39999999999999, 40.69999999999999, 40.99999999999997, 41.59999999999995, 41.59999999999995, 41.59999999999995, 41.59999999999995, 41.59999999999995, 41.59999999999995, 41.59999999999995, 41.599999999999966, 41.59999999999998, 41.79999999999998, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 42.19999999999999, 43.499999999999986, 43.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 45.499999999999986, 46.199999999999974, 46.59999999999995, 46.59999999999995, 46.59999999999995, 46.59999999999995, 46.59999999999995, 46.59999999999995, 46.199999999999946, 46.199999999999946, 46.09999999999994, 46.09999999999995, 46.09999999999995, 45.99999999999993, 46.299999999999926, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.59999999999991, 46.399999999999906, 45.9999999999999, 45.499999999999886, 45.29999999999987, 44.99999999999986, 44.99999999999986, 44.79999999999987, 44.99999999999986, 44.899999999999864, 45.19999999999986, 45.19999999999986, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.499999999999844, 45.899999999999835, 45.899999999999835, 45.899999999999835, 45.899999999999835, 45.899999999999835, 45.899999999999835, 45.139999999999844, 44.97999999999983, 44.879999999999825, 44.27999999999983, 43.97999999999985, 43.77999999999986, 44.47999999999986, 44.279999999999845, 44.57999999999983, 44.379999999999825, 44.17999999999981, 44.17999999999981, 44.17999999999981, 44.579999999999814, 44.279999999999816, 44.279999999999816, 44.0799999999998, 44.2799999999998, 44.2799999999998, 44.579999999999785, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 44.77999999999979, 45.179999999999794, 45.179999999999794, 45.179999999999794, 45.679999999999794, 46.47999999999982, 46.47999999999982, 46.47999999999982, 46.47999999999982, 46.47999999999982, 46.47999999999982, 46.17999999999982, 46.17999999999982, 46.379999999999825, 46.37999999999981, 46.079999999999814, 45.47999999999982, 45.47999999999982, 45.279999999999816, 45.27999999999983, 45.37999999999985, 45.07999999999987, 44.57999999999987, 44.37999999999987, 44.479999999999876, 44.57999999999987, 44.17999999999988, 43.87999999999987, 43.839999999999876, 44.339999999999876, 44.53999999999988, 44.679999999999865, 44.679999999999865, 44.679999999999865, 44.679999999999865, 44.679999999999865, 44.679999999999865, 44.179999999999865, 43.67999999999988, 43.27999999999986, 41.87999999999985, 40.179999999999865, 38.679999999999865, 38.27999999999986, 38.179999999999865, 38.47999999999986, 38.579999999999885, 38.679999999999865, 38.079999999999885, 38.079999999999885, 38.079999999999885, 38.479999999999876, 38.479999999999876, 37.919999999999874, 37.31999999999988, 36.919999999999874, 36.81999999999988, 36.919999999999874, 37.11999999999988, 37.5199999999999, 37.7199999999999, 37.7199999999999, 37.7199999999999, 37.7199999999999, 37.7199999999999, 37.7199999999999, 37.7199999999999, 38.11999999999992, 38.11999999999992, 38.31999999999991, 38.31999999999991, 38.31999999999991, 38.31999999999991, 38.31999999999991, 38.31999999999991, 38.31999999999991, 38.619999999999905, 38.619999999999905, 38.619999999999905, 38.619999999999905, 38.619999999999905, 38.619999999999905, 39.819999999999894, 40.2199999999999, 40.31999999999991, 40.319999999999894, 40.41999999999989, 40.41999999999989, 40.41999999999989, 40.41999999999989, 40.259999999999906, 40.45999999999991, 40.259999999999906, 39.5599999999999, 38.3599999999999, 37.3599999999999, 36.859999999999914, 36.3599999999999, 36.5599999999999, 36.759999999999906, 37.45999999999991, 37.859999999999914, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.95999999999992, 37.659999999999926, 37.35999999999993, 37.69999999999993, 38.09999999999992, 38.39999999999995, 38.39999999999995, 38.899999999999935, 38.899999999999935, 38.899999999999935, 38.899999999999935, 38.899999999999935, 38.899999999999935, 38.899999999999935, 38.89999999999995, 38.99999999999996, 38.69999999999996, 38.49999999999997, 38.39999999999995, 38.199999999999946, 37.99999999999996, 37.49999999999997, 36.999999999999986, 36.499999999999986, 36.199999999999974, 35.99999999999997, 35.799999999999955, 35.699999999999946, 35.69999999999996, 35.99999999999996, 36.39999999999995, 36.59999999999995, 36.59999999999995, 36.59999999999995, 36.59999999999995, 36.49999999999994, 36.49999999999994, 36.59999999999994, 36.79999999999994, 36.99999999999993, 37.09999999999994, 37.09999999999994, 37.09999999999994, 37.09999999999994, 37.09999999999994, 37.09999999999994, 37.09999999999994, 37.39999999999992, 37.39999999999992, 37.499999999999915, 37.499999999999915, 37.499999999999915, 37.499999999999915, 37.499999999999915, 37.499999999999915, 37.999999999999915, 38.39999999999989, 38.79999999999988, 38.79999999999988, 38.79999999999988, 38.79999999999988, 38.79999999999988, 38.79999999999988, 38.79999999999988, 40.79999999999987, 42.39999999999989, 43.79999999999988, 43.79999999999988, 43.79999999999988, 43.79999999999988, 43.79999999999988, 43.79999999999988, 43.59999999999988, 43.399999999999864, 43.399999999999864, 43.399999999999864, 43.399999999999864, 43.99999999999986, 43.99999999999986, 43.99999999999986, 43.99999999999986, 43.99999999999986, 43.99999999999986, 43.99999999999986, 43.99999999999986, 43.99999999999986, 44.09999999999984, 43.89999999999982, 43.79999999999983, 43.69999999999983, 43.899999999999835, 43.899999999999835, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.39999999999982, 44.09999999999981, 43.3599999999998, 42.95999999999981, 42.519999999999825, 42.25999999999982, 41.359999999999815, 40.19999999999979, 38.99999999999979, 37.99999999999977, 36.699999999999775, 35.59999999999978, 34.69999999999979, 33.89999999999981, 33.599999999999824, 33.399999999999835, 33.899999999999835, 34.099999999999824, 34.29999999999981, 34.39999999999981, 34.39999999999981, 34.39999999999981, 34.39999999999981, 34.39999999999981, 33.699999999999804, 33.39999999999981, 33.19999999999979, 33.09999999999977, 32.69999999999976, 32.09999999999977, 32.29999999999977, 33.39999999999978, 34.299999999999784, 34.299999999999784, 34.299999999999784, 34.299999999999784, 34.299999999999784, 32.799999999999784, 31.799999999999784, 31.299999999999784, 31.299999999999784, 31.69999999999979, 30.899999999999793, 28.899999999999807, 25.099999999999795, 21.59999999999981, 19.599999999999795, 20.399999999999807, 21.999999999999815, 22.299999999999827, 21.399999999999807, 21.299999999999798, 21.9999999999998, 21.9999999999998, 21.9999999999998, 21.299999999999798, 20.799999999999784, 20.799999999999784, 20.099999999999767, 18.999999999999773, 18.099999999999767, 17.499999999999773, 16.999999999999773, 17.199999999999775, 16.99999999999976, 17.29999999999974, 17.999999999999744, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.799999999999756, 18.299999999999756, 17.69999999999976, 17.799999999999756, 18.099999999999753, 19.49999999999976, 20.89999999999975, 20.89999999999975, 20.89999999999975, 20.89999999999975, 20.89999999999975, 20.89999999999975, 20.89999999999975, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.69999999999976, 21.39999999999975, 22.399999999999736, 23.499999999999744, 24.59999999999974, 24.59999999999974, 24.59999999999974, 24.59999999999974, 24.59999999999974, 24.59999999999974, 24.79999999999974]

    def drawdownSlowCode(prices):
    maxi = 0
    mini = 0
    for i in range(len(prices))[1:]:
    maxj = 0
    max = 0
    for j in range(i+1, len(prices)):
    if prices[i] – prices[j] > max:
    maxj = j
    max = prices[i] – prices[j]
    if max > prices[maxi] – prices[mini]:
    maxi = i
    mini = maxj
    return (prices[maxi], prices[mini])

    def drawdownfast(prices):
    prevmaxi = 0
    prevmini = 0
    maxi = 0

    for i in range(len(prices))[1:]:
    if prices[i] >= prices[maxi]:
    maxi = i
    else:
    # You can only determine the largest drawdown on a downward price!
    if (prices[maxi] – prices[i]) > (prices[prevmaxi] – prices[prevmini]):
    prevmaxi = maxi
    prevmini = i
    return (prices[prevmaxi], prices[prevmini])

    def main():
    print drawdownSlowCode(profitline)
    print drawdownfast(profitline)

    if __name__ == ‘__main__’:
    main()

Comments are closed.