Closed MislavSag closed 1 year ago
The "jump" is implied by the logic of unit_prices: it assumes that the timestamp of the cashflow is after valuation, i.e. you cannot use it on the timestamp (think of "after the close of the market").
In your case:
timestamp NAV
2023-01-26 2521.62
2023-01-27 1090107.32
Your inflow:
timestamp NAV
2023-01-27 1089500
The function computes the NAV before the inflow, which is 1090107.32 - 1089500 = 607.32. But then 607.32/2521.62 - 1 = -0.76, i.e. your unit price drops by 76%.
This reflected in the output of unit_prices:
timestamp NAV price units
## ....
52 2023-01-26 2521.62 100.86480 25.00
53 2023-01-27 1090107.32 24.29280 44873.68
## ....
Should I than lag inflows?
The function works for some other strategies.
Yes, since your convention for timestamps/cashflows (i.e. cashflow is available at start of period) is not directly supported. But you could get results by slightly changing your data.
## YOUR DATA
> head(NAV)
timestamp NAV
1: 2022-11-16 970.00
2: 2022-11-17 2500.00
3: 2022-11-18 2500.00
4: 2022-11-21 2494.56
5: 2022-11-22 2526.42
6: 2022-11-23 2541.54
> NAV[51:53, ]
timestamp NAV
1: 2023-01-25 2495.61
2: 2023-01-26 2521.62
3: 2023-01-27 1090107.32
> cf
timestamp cashflow
1: 2022-11-16 970
2: 2022-11-17 1530
3: 2023-01-27 1089500
In the first variation, the cashflow appears always t+1 in the NAV.
## VARIATION 1
> head(NAV)
timestamp NAV
1: 2022-11-16 0.00 ## << NAV changed
2: 2022-11-17 970.00 ## << NAV changed
3: 2022-11-18 2500.00
4: 2022-11-21 2494.56
5: 2022-11-22 2526.42
6: 2022-11-23 2541.54
> cf
timestamp cashflow
1: 2022-11-16 970
2: 2022-11-17 1530
3: 2023-01-26 1089500 ## << date changed
unit_prices(as.data.frame(NAV),
as.data.frame(cf),
initial.price = 100,
cf.included = FALSE)$price
## ^^^^^^^^^^^^^^^^^^^ ## setting
## VARIATION 2
> NAV[51:53, ]
timestamp NAV
1: 2023-01-25 2495.61
2: 2023-01-26 1092021.62 ## <<2521.62 + 1089500
3: 2023-01-27 1090107.32
> cf
timestamp cashflow
1: 2022-11-16 970
2: 2022-11-17 1530
3: 2023-01-26 1089500 ## << date changed
unit_prices(as.data.frame(NAV),
as.data.frame(cf),
initial.price = 100)$price
Yet one more variation: if the cashflows have been used on the timestamp on which they show up in the NAV series, set the cashflow timestamps to t - 1 and use cf.included = FALSE
. But this may require that you include a timestamp before the first date in the NAV series.
I choosed 2 option and it works. Thanks.
I suggest to close this issue. The PMwR manual has a brief section on external cashflows and return calculations: http://enricoschumann.net/R/packages/PMwR/manual/PMwR.html#returns-with-external-cashflows . Feel free to reopen it if unexpected behaviour occurs.
I have following NAv and cf object:
I tried to use
unit_prices
function on this 2 objects (following example):buy as you can see on the plot, results are worng, there is a big break that shouldn't exists.