alexx0186
alexx0186

Reputation: 1567

How to calculate cumulative return with pandas?

How can I calculate cumulative return of portfolio, with daily return?

                    Return  Portfolio
1999-01-04             NaN        100
1999-01-05        0.011430        100
1999-01-06        0.024109        100

I tried this but it doesn't work:

df['Portfolio'] = df.shift(1)['Portfolio'] * (1 + df['Return'])

I want to get 100, 101.14, 103.5 etc...

Upvotes: 0

Views: 92

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150815

I think you are looking for cumprod, but you would need to add 1 to the returns:

df.Return.fillna(0).add(1).cumprod() * df['Portfolio']

Output:

1999-01-04    100.000000
1999-01-05    101.143000
1999-01-06    103.581457
dtype: float64

Upvotes: 1

Related Questions