Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Drawdown in Pandas

Tags:

python

pandas

I have the following DataFrame:

              Profit       Cumulative
Date                            
1/6/2005    248.8500      248.85
1/12/2005    48.3500      297.20
1/13/2005    29.2900      326.49
1/13/2005  -500.0000     -173.51
1/13/2005  -250.9500     -424.46
1/14/2005   126.6600     -297.80
1/16/2005    58.7400     -239.06
1/19/2005    68.3500     -170.71
1/21/2005   140.0000      -30.71
1/23/2005   200.0000      169.29
1/26/2005  -250.6800      -81.39
1/27/2005   162.5000       81.11
1/27/2005   135.5100      216.62
1/27/2005  -650.0000     -433.38
1/28/2005    96.8800     -336.50
1/28/2005 -1000.0000    -1336.50
1/31/2005   140.0000    -1196.50
2/1/2005    140.0000    -1056.50

The first column are the dollar profits of my portfolio. I have calculated the second column with the following formula:

df['Cumulative'] = df.Profit.cumsum().round(2)

So, is there a formula where I can calculate the dollar (not %) drawdowns of my portfolio? The column should look like this:

Drawdown

 0.00
 0.00
 0.00
-500.00
-750.95
-624.29
-565.55
-497.20
-357.20
-157.20
-407.88
-245.38
-109.87
-759.87
-662.99
-1,662.99
-1,522.99
-1,382.99
-1,382.99
like image 502
user8491020 Avatar asked Nov 27 '22 07:11

user8491020


1 Answers

df['Cumulative'] = df.Profit.cumsum().round(2)
df['HighValue'] = df['Cumulative'].cummax()

df['Drawdown'] = df['Cumulative'] - df['HighValue']

That is the simplest solution I found.

like image 51
user8491020 Avatar answered Dec 29 '22 01:12

user8491020