在python中使用数据帧计算指示符

2024-04-29 14:53:07 发布

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

您能否帮助我计算数据帧的指标:

下面的代码适用于一个系列(索引日期和资产返回的一列):但不适用于数据帧

“”r是我在函数中使用的数据集(索引是日期,列是资产的名称和返回)

def drawdown(r):

    wealth_index = 1000*(1+r).cumprod()

    previous_peaks = wealth_index.cummax()

    drawdown = (wealth_index - previous_peaks)/previous_peaks

    amount_drawdown = wealth_index - previous_peaks

    return pd.DataFrame({"Wealth": wealth_index,

    "Previous Peak": previous_peaks,

    "Max Drawdown in %": drawdown*100,

    "Drawdown Amount": amount_drawdown})

对于DataFrame:我曾尝试为输出DataFrame编制索引,但我弄错了

def drawdown(r):

    wealth_index = 1000*(1+r).cumprod()

    previous_peaks = wealth_index.cummax()

    drawdown = (wealth_index - previous_peaks)/previous_peaks

    amount_drawdown = wealth_index - previous_peaks

    return pd.DataFrame({"Wealth": wealth_index,

    "Previous Peak": previous_peaks,

    "Max Drawdown in %": drawdown*100,

    "Drawdown Amount": amount_drawdown}, index=r.columns)

你能帮忙吗


Tags: 数据dataframeindexreturndef资产amountpd
1条回答
网友
1楼 · 发布于 2024-04-29 14:53:07

抱歉,我错过了你最后的评论,看起来你想要一个多索引数据帧作为最终输出。我相信这符合你的要求 (显然,在def ind()中填写CSV文件的文件路径和名称)

def ind():
    df = pd.read_csv('/home/mre/Desktop/test.csv', parse_dates=True) 
    return drawdownDF(df)

def drawdownSeries(r, dates, col):

    wealth_index = 1000*(1+r).cumprod()

    previous_peaks = wealth_index.cummax()

    drawdown = (wealth_index - previous_peaks)/previous_peaks

    amount_drawdown = wealth_index - previous_peaks

    df = pd.DataFrame({"Group": col, "date" : dates, "Wealth": wealth_index,

    "Previous Peak": previous_peaks,

    "Max Drawdown in %": drawdown*100,

    "Drawdown Amount": amount_drawdown}) 

    return df


def drawdownDF(r):
#    dfs = {}
    dfs = []
    for col in r.columns[1:]:
        collist = [col]*len(r['date'])
        dfs.append(drawdownSeries(r[col], r['date'], collist))
    finalPD = pd.concat(dfs)
    finalPD.set_index(['Group', 'date'], inplace=True)
    return finalPD

输出:

In [104]: ind()
Out[104]: 
                                   Drawdown Amount  Max Drawdown in %  \
Group                  date                                             
Convertible Arbitrage  31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997          0.00000             0.0000   
                       30/04/1997          0.00000             0.0000   
CTA Global             31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997      -4120.49400           -21.0000   
                       30/04/1997     -30472.03420          -155.3000   
Distressed Securities  31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997       -740.59200           -12.0000   
                       30/04/1997          0.00000             0.0000   
Emerging Markets       31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997     -66825.00000          -120.0000   
                       30/04/1997     -80078.62500          -143.8000   
Equity Market Neutral  31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997          0.00000             0.0000   
                       30/04/1997          0.00000             0.0000   
Event Driven           31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997      -1324.61600           -23.0000   
                       30/04/1997      -1546.34520           -26.8500   
Fixed Income Arbitrage 31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997          0.00000             0.0000   
                       30/04/1997          0.00000             0.0000   
Global Macro           31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997     -22023.92500          -119.0000   
                       30/04/1997     -28072.17600          -151.6800   
Long/Short Equity      31/01/1997          0.00000             0.0000   
                       28/02/1997       -228.60000            -6.0000   
                       31/03/1997      -3236.97600           -84.9600   
                       30/04/1997      -2755.63584           -72.3264   
Merger Arbitrage       31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997          0.00000             0.0000   
                       30/04/1997        -53.60000            -1.0000   
Relative Value         31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997          0.00000             0.0000   
                       30/04/1997          0.00000             0.0000   
Short Selling          31/01/1997          0.00000            -0.0000   
                       28/02/1997      -2811.60000           426.0000   
                       31/03/1997     -29820.64800          4518.2800   
                       30/04/1997          0.00000             0.0000   
Funds Of Funds         31/01/1997          0.00000             0.0000   
                       28/02/1997          0.00000             0.0000   
                       31/03/1997      -6614.45400           -77.0000   
                       30/04/1997      -6436.63686           -74.9300   

                                   Previous Peak       Wealth  
Group                  date                                    
Convertible Arbitrage  31/01/1997     2190.00000   2190.00000  
                       28/02/1997     4883.70000   4883.70000  
                       31/03/1997     8692.98600   8692.98600  
                       30/04/1997    16168.95396  16168.95396  
CTA Global             31/01/1997     4930.00000   4930.00000  
                       28/02/1997    19621.40000  19621.40000  
                       31/03/1997    19621.40000  15500.90600  
                       30/04/1997    19621.40000 -10850.63420  
Distressed Securities  31/01/1997     2780.00000   2780.00000  
                       28/02/1997     6171.60000   6171.60000  
                       31/03/1997     6171.60000   5431.00800  
                       30/04/1997     7060.31040   7060.31040  
Emerging Markets       31/01/1997     8910.00000   8910.00000  
                       28/02/1997    55687.50000  55687.50000  
                       31/03/1997    55687.50000 -11137.50000  
                       30/04/1997    55687.50000 -24391.12500  
Equity Market Neutral  31/01/1997     2890.00000   2890.00000  
                       28/02/1997     5808.90000   5808.90000  
                       31/03/1997     6738.32400   6738.32400  
                       30/04/1997    14756.92956  14756.92956  
Event Driven           31/01/1997     3130.00000   3130.00000  
                       28/02/1997     5759.20000   5759.20000  
                       31/03/1997     5759.20000   4434.58400  
                       30/04/1997     5759.20000   4212.85480  
Fixed Income Arbitrage 31/01/1997     2910.00000   2910.00000  
                       28/02/1997     6460.20000   6460.20000  
                       31/03/1997    13501.81800  13501.81800  
                       30/04/1997    31054.18140  31054.18140  
Global Macro           31/01/1997     6730.00000   6730.00000  
                       28/02/1997    18507.50000  18507.50000  
                       31/03/1997    18507.50000  -3516.42500  
                       30/04/1997    18507.50000  -9564.67600  
Long/Short Equity      31/01/1997     3810.00000   3810.00000  
                       28/02/1997     3810.00000   3581.40000  
                       31/03/1997     3810.00000    573.02400  
                       30/04/1997     3810.00000   1054.36416  
Merger Arbitrage       31/01/1997     2500.00000   2500.00000  
                       28/02/1997     3350.00000   3350.00000  
                       31/03/1997     5360.00000   5360.00000  
                       30/04/1997     5360.00000   5306.40000  
Relative Value         31/01/1997     2800.00000   2800.00000  
                       28/02/1997     6104.00000   6104.00000  
                       31/03/1997     6714.40000   6714.40000  
                       30/04/1997    14905.96800  14905.96800  
Short Selling          31/01/1997     -660.00000   -660.00000  
                       28/02/1997     -660.00000  -3471.60000  
                       31/03/1997     -660.00000 -30480.64800  
                       30/04/1997     8839.38792   8839.38792  
Funds Of Funds         31/01/1997     4170.00000   4170.00000  
                       28/02/1997     8590.20000   8590.20000  
                       31/03/1997     8590.20000   1975.74600  
                       30/04/1997     8590.20000   2153.56314  

相关问题 更多 >