计算大Pandas的水位下降

2024-10-01 17:36:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

              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

第一列是我投资组合的美元利润。我用以下公式计算了第二列:

^{pr2}$

那么,有没有一个公式可以计算出我投资组合的美元(而不是%)支取额? 列应该如下所示:

Drawdonwn

 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

Tags: 数据date公式利润profitpr2cumulativedrawdonwn
2条回答

根据我的记忆,提款是指你的投资组合利润低于最高水平的数额。(将来,你应该明确地定义你想要的输出,不要假设别人知道你在问什么。)

可能有更好的方法,但是您可以使用itertuples()pandas中计算:

import pandas as pd
from StringIO import StringIO

# read the data
df = pd.DataFrame.from_csv(StringIO("""Date               Profit       Cumulative
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"""), sep="\s+").reset_index()

# calculate drawdown
prev_high = 0
for i, date, profit, cumulative in df.itertuples():
    prev_high = max(prev_high, cumulative)
    dd = cumulative - prev_high
    df.loc[i, 'Drawdown'] = dd if dd < 0 else 0

生成的数据帧:

^{pr2}$

Investopedia对drawdown的定义。在

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

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

这是我能找到的最简单的解决办法。在

相关问题 更多 >

    热门问题