

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. The purist in me likes that fact that the O(N) solution looks like easier code to understand than the O(N^2) solution. We set the function’s value argument to 10000. Then we implement the risk management rule: // Limit the maximum drawdown to 10k (value10000, typestrategy.cash) This has us execute the () function. Any time the cumulative returns dips below the maximum cumulative returns, it's a drawdown. 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. Then we calculate two moving average with the sma () function. Description To find the maximum drawdown in a return series, we need to first calculate the cumulative returns and the maximum cumulative return to that point. However, the second part of the ‘if’ is where the magic happens. We move through the prices and the first part of the ‘if’ will find the highest part of the peak so far. This solution is a bit harder to explain. If (prices - prices) > (prices - prices): # You can only determine the largest drawdown on a downward price!

The challenge then is to find an O(N) solution to the problem and to save-those-much-needed-cycles for something really important: For small n this doesn’t really matter, but for any decently sized data-series this baby will be slow-as-molasses. 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). The problem with this solution is that it has quadratic complexity. 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. At the end you just return the two points you found. If this difference is also the largest seen in the data-set so far then make it the largest positive difference of all points. The deal is relatively straight-forward: compare the fist item against every item after it in the list and store the largest positive difference. 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. Even though drawdown is not a robust metric to describe the distribution of returns of a given asset, it has a strong psychological appeal. It’s what I have come to know as a ‘between’ analysis. This led to the first solution (in Python): 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. Part of the problem is coming up with a consistent natural language of what you want your peak and trough to be. To do it reliably (and quickly) on a computer, is not that straight forward. Well, finding these two babys by eye is trivial. The upper one is the ‘peak’ and the lower one is the ‘trough’.

Clearly past prices are not an indicator of future losses. 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. 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. Look at the following random graph of pretend asset prices: Sometimes investors want to be able to judge what the absolute worst case scenario would have been if they’d invested in something. However, in the docs for Statistics.DrawdownValue, (a related method) it refers to "equityOverTime" as "Array of portfolio value over time", suggesting it is a literal array (ie: a list) of floats (doubles).Ok, so this is a little bit technical but it’s an intriguing puzzle that got me thinking quite hard. Looking at C#'s definition of a 'SortedDictionary', it seems this is a dictionary where the key is a datetime. The authors show that drawdown is calculated by comparing the value of a cumulative return with a previous peak that is the maximum cumulative return. In the docs for Statistics.DrawdownPercent, i see the following method signature:īut there's no details of what is expected for 'equityOverTime'. So far i'm able to easily calculate % return and sharpe ratio (there's actually an indicator for sharpe), but I need a little guidance in calculating Drawdown percent for a stock over a period of time, using the in-built statistics method: I'm exploring the use of portfolio metrics to evaluate asset performance (after all, portfolio equity over time and asset price over time are both time series).
