calculating "Max Draw Down" in SQL

Viewed 3920

edit: it's worth reviewing the comments section of the first answer to get a clearer idea of the problem.

edit: I'm using SQLServer 2005

something similar to this was posted before but I don't think enough information was given by the poster to truly explain what max draw down is. All my definitions of max draw down come from (the first two pages of) this paper: http://www.stat.columbia.edu/~vecer/maxdrawdown3.pdf

effectively, you have a few terms defined mathematically:

Running Maximum, Mt

Mt = maxu in [0,t] (Su)
where St is the price of a Stock, S, at time, t.

Drawdown, Dt

Dt = Mt - St

Max Draw Down, MDDt

MDDt = maxu in [0,t] (Du)

so, effectively what needs to be determined is the local maximums and minimums from a set of hi and low prices for a given stock over a period of time. I have a historical quote table with the following (relevant) columns:

stockid int  
day date  
hi  int --this is in pennies  
low int --also in pennies  

so for a given date range, you'll see the same stockid every day for that date range.

EDIT:
hi and low are high for the day and low for each day.

once the local max's and min's are determined, you can pair every max with every min that comes after it and calculate the difference. From that set, the maximum difference would be the "Max Draw Down".

The hard part though, is finding those max's and min's.

edit: it should be noted: max drawdown is defined as the value of the hypothetical loss if the stock is bought at it's highest buy point and sold at it's lows sell point. A stock can't be sold at a minval that came before a maxval. so, if the global minval comes before the global maxval, those two values do not provide enough information to determine the max-drawdown.

6 Answers

I have encounter this problem recently, My solution is like this: let data: 3,5,7,3,-1,3,-8,-3,0,10 add the sum one by one, if the sum is great than 0, set it 0, else get the sum, the result would be like this 0,0,0,0,-1,0,-8,-11,-11,-1 The Maximum draw down is the lowest value in the data, -11.

Related