I'm trying to run backtest in a vectorized way using Python Pandas
and need to calculate a portfolio cumulative return from price data and weight of asset data.
I have two Dataframes
:
price of each individual assets (https://www.dropbox.com/s/ve9ll3t1j5owfuc/test_price.csv?dl=0)
weight of each individual assets (https://www.dropbox.com/s/hto9kq2g2wwfpm8/test_weight.csv?dl=0)
Both Dataframe has same shape
Weights of each assets change only at the end of month
- Weights of the rest of days are filled by 'ffill' method, so weights are all same during the each month
What I have found out:
portfolio_cum_rtn_df = (price_df.pct_change().fillna(0) + 1).multiply(weight_df).sum(axis=1)
portfolio_rtn_df = price_df.pct_change().fillna(0).multiply(weight_df).sum(axis=1)
portfolio_cum_rtn_df = (portfolio_rtn_df + 1).cumprod()
Both are not correct way to calculate portfolio cumulative return.
Need some helps