Calculating cumulative returns with pandas dataframe

前端 未结 3 994

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 16:46

    you just cannot simply add them all by using cumsum

    for example, if you have array [1.1, 1.1], you supposed to have 2.21, not 2.2

    import numpy as np
    
    # daily return:
    df['daily_return'] = df['close'].pct_change()
    
    # calculate cumluative return
    df['cumluative_return'] = np.exp(np.log1p(df['daily_return']).cumsum())
    

提交回复
热议问题