@@ -81,9 +81,12 @@ def get_wealth_indexes_fv_with_cashflow(
8181 else :
8282 # Slow Calculation
8383 pandas_frequency = cashflow_parameters ._pandas_frequency
84+ months_in_full_period = settings ._MONTHS_PER_YEAR / cashflow_parameters .periods_per_year
8485 wealth_df = pd .DataFrame (dtype = float , columns = [portfolio_symbol ])
8586 for n , x in enumerate (ror_cashflow_df .resample (rule = pandas_frequency , convention = "start" )):
8687 ror_ts = x [1 ].iloc [:, portfolio_position ] # select ror part of the grouped data
88+ months_local = ror_ts .shape [0 ]
89+ period_fraction = months_local / months_in_full_period # 1 for a full period
8790 cashflow_ts_local = x [1 ].loc [:, "cashflow_ts" ].copy ()
8891 # CashFlow inside period (Extra withdrawals/contributions)
8992 if (cashflow_ts_local != 0 ).any ():
@@ -109,6 +112,7 @@ def get_wealth_indexes_fv_with_cashflow(
109112 )
110113 else :
111114 raise ValueError ("Wrong cashflow_method value." )
115+ cashflow_value *= period_fraction # adjust cash flow to the period length (months)
112116 period_final_balance = period_wealth_index .iloc [- 1 ] + cashflow_value
113117 period_wealth_index .iloc [- 1 ] = period_final_balance
114118 period_initial_amount = period_final_balance
@@ -199,8 +203,11 @@ def get_cash_flow_fv(
199203 else :
200204 # Slow Calculation
201205 pandas_frequency = settings .frequency_mapping [cashflow_parameters .frequency ]
206+ months_in_full_period = settings ._MONTHS_PER_YEAR / cashflow_parameters .periods_per_year
202207 for n , x in enumerate (ror_cashflow_df .resample (rule = pandas_frequency , convention = "start" )):
203208 ror_ts = x [1 ].iloc [:, portfolio_position ] # select ror part of the grouped data
209+ months_local = ror_ts .shape [0 ]
210+ period_fraction = months_local / months_in_full_period # 1 for a full period
204211 cashflow_ts_local = x [1 ].loc [:, "cashflow_ts" ].copy ()
205212 # CashFlow inside period (Extra cash flow)
206213 if (cashflow_ts_local != 0 ).any ():
@@ -226,6 +233,7 @@ def get_cash_flow_fv(
226233 )
227234 else :
228235 raise ValueError ("Wrong cashflow_method value." )
236+ cashflow_value *= period_fraction # adjust cash flow to the period length (months)
229237 period_final_balance = period_wealth_index .iloc [- 1 ] + cashflow_value
230238 period_wealth_index .iloc [- 1 ] = period_final_balance
231239 period_initial_amount = period_final_balance
0 commit comments