使用 .expanding() Python 计算投资组合回撤

我正在尝试使用下面的代码计算投资组合随时间的回撤。我尝试使用 .expanding() 函数,但似乎无法获得所需的输出。如果有人能让我知道我哪里出错了,我真的很感激。


def drawdown_2(arr):

    tot_return = arr.add(1).cumprod()

    max_return = tot_return.add(1).cummax()

    return (tot_return / max_return) - 1


df['Drawdown'] = df.groupby(df.portfolio)['performance'].expanding().apply(drawdown_2)


输入数据的格式如下


portfolio   period  performance

port1   201501  0.003718

port1   201502  -0.004890

port1   201503  -0.004171

port1   201504  -0.006922

port1   201505  0.003545

port1   201506  0.003545

port1   201507  0.006901

port1   201508  0.000101

port1   201509  0.009081

port1   201510  0.003062

port1   201511  -0.008425

port1   201512  0.002580

port2   201501  0.009135

port2   201502  0.009149

port2   201503  -0.004252

port2   201504  -0.008788

port2   201505  -0.006210

port2   201506  0.006020

port2   201507  0.002983

port2   201508  0.008498

port2   201509  0.008080

port2   201510  0.000138

port2   201511  -0.008425

port2   201512  0.002580

所需的输出是一个数组,它是投资组合先前的最大值与投资组合的当前值之间的差。上述投入的回撤数字如下所示,采用所需格式:


portfolio   period  performance Drawdown

port1   201501  0.003718    0.00000

port1   201502  -0.004890   -0.00490

port1   201503  -0.004171   -0.00900

port1   201504  -0.006922   -0.01590

port1   201505  0.003545    -0.01240

port1   201506  0.003545    -0.00890

port1   201507  0.006901    -0.00210

port1   201508  0.000101    -0.00200

port1   201509  0.009081    0.00000

port1   201510  0.003062    0.00000

port1   201511  -0.008425   -0.00842

port1   201512  0.002580    -0.00587

port2   201501  0.009135    0.00000

port2   201502  0.009149    0.00000

port2   201503  -0.004252   -0.00430

port2   201504  -0.008788   -0.01300

port2   201505  -0.006210   -0.01910

port2   201506  0.006020    -0.01320

port2   201507  0.002983    -0.01030

port2   201508  0.008498    -0.00190

port2   201509  0.008080    0.00000

port2   201510  0.000138    0.00000

port2   201511  -0.008425   -0.00860

port2   201512  0.002580    -0.00605

提前感谢一百万的帮助。


蝴蝶不菲
浏览 102回答 1
1回答

UYOU

我正在使用 yfinance 的数据:import yfnance as yfdf = yf.download('aapl', start='2020-01-01')[['Close']]df['Chg'] = df['Close'].pct_change()    CloseDate    2019-12-31  73.4124982020-01-02  75.0875022020-01-03  74.3574982020-01-06  74.9499972020-01-07  74.597504... ...2020-09-03  120.8799972020-09-04  120.9599992020-09-08  112.8200002020-09-09  117.3200002020-09-10  118.930000计算累积回报、滚动最大峰值以及尾随峰值的回撤:df['Cum_ret'] = (1+ df['Chg']).cumprod()  # cumulative returndf['Peaks'] = df['Cum_ret'].cummax()      # cumulative peaksdf['Drawdown'] = (df['Cum_ret'] - df['Peaks']) / df['Peaks']  # drawdown from trailing peak累积回报和峰值:回撤:编辑:刚刚注意到您正在处理 2 个投资组合回报,所以这并没有真正回答您的问题......我认为这会做你想要的:df['Drawdown'] = df.groupby('portfolio')['performance'].apply(drawdown_2)    portfolio   period  performance Drawdown0   port1   201501  0.003718    0.0000001   port1   201502  -0.004890   -0.0048902   port1   201503  -0.004171   -0.0090413   port1   201504  -0.006922   -0.0159004   port1   201505  0.003545    -0.0124115   port1   201506  0.003545    -0.0089106   port1   201507  0.006901    -0.0020717   port1   201508  0.000101    -0.0019708   port1   201509  0.009081    0.0000009   port1   201510  0.003062    0.00000010  port1   201511  -0.008425   -0.00842511  port1   201512  0.002580    -0.00586712  port2   201501  0.009135    0.00000013  port2   201502  0.009149    0.00000014  port2   201503  -0.004252   -0.00425215  port2   201504  -0.008788   -0.01300316  port2   201505  -0.006210   -0.01913217  port2   201506  0.006020    -0.01322718  port2   201507  0.002983    -0.01028419  port2   201508  0.008498    -0.00187320  port2   201509  0.008080    0.00000021  port2   201510  0.000138    0.00000022  port2   201511  -0.008425   -0.00842523  port2   201512  0.002580    -0.005867
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python