Calculating cumulative returns with pandas dataframe

前端 未结 3 992

I have this dataframe

Poloniex_DOGE_BTC   Poloniex_XMR_BTC    Daily_rets  perc_ret
172 0.006085    -0.000839   0.003309    0
173 0.006229    0.002111    0.005135         


        
3条回答
  •  春和景丽
    2021-02-02 17:11

    If they are daily simple returns and you want a cumulative return, surely you must want a daily compounded number?

    df['perc_ret'] = (1 + df.Daily_rets).cumprod() - 1  # Or df.Daily_rets.add(1).cumprod().sub(1)
    
    >>> df
         Poloniex_DOGE_BTC  Poloniex_XMR_BTC  Daily_rets  perc_ret
    172           0.006085         -0.000839    0.003309  0.003309
    173           0.006229          0.002111    0.005135  0.008461
    174           0.000000         -0.001651    0.004203  0.012700
    175           0.000000          0.007743    0.005313  0.018080
    176           0.000000         -0.001013   -0.003466  0.014551
    177           0.000000         -0.000550    0.000772  0.015335
    178           0.000000         -0.009864    0.001764  0.017126
    

    If they are log returns, then you could just use cumsum.

提交回复
热议问题