pub fn twr(
values: &[(Decimal, Decimal)],
annualization_period: Option<Decimal>,
) -> Decimal
Expand description
Time Weighted Return (TWR)
This function calculates the Time Weighted Rate of Return (TWR) for a series of asset values and cash flows.
The TWR is a measure of the compound growth rate of an investment portfolio over time, eliminating the impact of cash flows (deposits and withdrawals) on the return. It is useful as a performance measure on the underlying investment strategy of the portfolio.
§Arguments
values
- A slice of tuples containing the asset value at the end of the period and the net cash flow during the period. Cash flows are from the perspective of the asset, i.e. net contibutions are positive and withdrawals/distributions are negative. For example, at the end of the year the asset value is $1000 and there was a net contribution of $100, the tuple would be (1000, 100).annualization_period
(optional) - The number of annual periods to annualize the TWR. IfNone
, the TWR is not annualized. For example, if you have 6 months of data, you would passSome(0.5)
to annualize the TWR. If you have 6 quarters of data, you would passSome(1.5)
to annualize the TWR.
§Returns
- The Time Weighted Rate of Return (TWR) as a
Decimal
.
§Example
use rust_decimal_macros::dec;
use rust_finprim::rate::twr;
let values = vec![
(dec!(1000), dec!(1000)), // Initially we bought an asset for $1000
(dec!(1600), dec!(400)), // In Q1'24 we had a net contribution of $400, ending value is $1600
(dec!(1450), dec!(-200)), // In Q2'24 we had a net withdrawal of $200, ending value is $1450
(dec!(1700), dec!(200)), // In Q3'24 we had a net contribution of $200, ending value is $1700
(dec!(2200), dec!(300)), // In Q4'24 we had a net contribution of $300, ending value is $2200
(dec!(2500), dec!(0)), // In Q1'25 we had no cash flows, ending value is $2500
(dec!(3000), dec!(-300)), // In Q2'25 we had a net withdrawal of $300, ending value is $3000
(dec!(1700), dec!(-1500)), // In Q3'25 we had a net withdrawal of $1500, ending value is $1700
(dec!(0), dec!(2000)), // In Q4'25 the asset was liquidated and we had a net withdrawal of $2000
];
let twr = twr(&values, Some(dec!(2))); // Annualize over 2 years