Browse Source

Fix net performance percentage of date ranges

pull/7911/head
Thomas Kaul 4 weeks ago
parent
commit
2d59e0d10c
  1. 28
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  2. 6
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts

28
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

@ -794,9 +794,11 @@ export abstract class PortfolioCalculator {
const chart: HistoricalDataItem[] = [];
let grossPerformanceAtStartDate: number;
let grossPerformanceWithCurrencyEffectAtStartDate: number;
let netPerformanceAtStartDate: number;
let netPerformanceWithCurrencyEffectAtStartDate: number;
const timeWeightedInvestmentValues: number[] = [];
const timeWeightedInvestmentValuesWithCurrencyEffect: number[] = [];
for (const historicalDataItem of historicalData) {
@ -804,6 +806,9 @@ export abstract class PortfolioCalculator {
if (!isBefore(date, start) && !isAfter(date, end)) {
if (!isNumber(netPerformanceAtStartDate)) {
grossPerformanceAtStartDate =
historicalDataItem.value - historicalDataItem.totalInvestment;
grossPerformanceWithCurrencyEffectAtStartDate =
historicalDataItem.valueWithCurrencyEffect -
historicalDataItem.totalInvestmentValueWithCurrencyEffect;
@ -821,11 +826,16 @@ export abstract class PortfolioCalculator {
historicalDataItem.netPerformanceWithCurrencyEffect -
netPerformanceWithCurrencyEffectAtStartDate;
// Add the gross performance at the start date of the range to the
// investment of each day. Thus the range starts with the value of its
// first day, and subsequent buy and sell activities stay included.
if (historicalDataItem.totalInvestment > 0) {
timeWeightedInvestmentValues.push(
historicalDataItem.totalInvestment + grossPerformanceAtStartDate
);
}
if (historicalDataItem.totalInvestmentValueWithCurrencyEffect > 0) {
// Add the gross performance at the start date of the range to the
// investment of each day. Thus the range starts with the value of
// its first day, and subsequent buy and sell activities stay
// included.
timeWeightedInvestmentValuesWithCurrencyEffect.push(
historicalDataItem.totalInvestmentValueWithCurrencyEffect +
grossPerformanceWithCurrencyEffectAtStartDate
@ -833,6 +843,12 @@ export abstract class PortfolioCalculator {
}
const timeWeightedInvestmentValue =
timeWeightedInvestmentValues.length > 0
? sum(timeWeightedInvestmentValues) /
timeWeightedInvestmentValues.length
: 0;
const timeWeightedInvestmentValueWithCurrencyEffect =
timeWeightedInvestmentValuesWithCurrencyEffect.length > 0
? sum(timeWeightedInvestmentValuesWithCurrencyEffect) /
timeWeightedInvestmentValuesWithCurrencyEffect.length
@ -848,9 +864,9 @@ export abstract class PortfolioCalculator {
? netPerformanceSinceStartDate / timeWeightedInvestmentValue
: 0,
netPerformanceInPercentageWithCurrencyEffect:
timeWeightedInvestmentValue > 0
timeWeightedInvestmentValueWithCurrencyEffect > 0
? netPerformanceWithCurrencyEffectSinceStartDate /
timeWeightedInvestmentValue
timeWeightedInvestmentValueWithCurrencyEffect
: 0
});
}

6
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts

@ -285,7 +285,7 @@ describe('PortfolioCalculator', () => {
'1d': {
date: '2018-01-01',
netPerformance: -486.0860160000011,
netPerformanceInPercentage: -0.035084130151423504,
netPerformanceInPercentage: -0.035263202509112565,
netPerformanceInPercentageWithCurrencyEffect: -0.040162295064062624,
netPerformanceWithCurrencyEffect: -556.4433239999998,
totalInvestmentValueWithCurrencyEffect: 318.54266729999995,
@ -294,7 +294,7 @@ describe('PortfolioCalculator', () => {
max: {
date: '2018-01-01',
netPerformance: 26458.9121202,
netPerformanceInPercentage: 41.63298219956282,
netPerformanceInPercentage: 42.50435329547954,
netPerformanceInPercentageWithCurrencyEffect: 41.72313811883715,
netPerformanceWithCurrencyEffect: 26516.2087014,
totalInvestmentValueWithCurrencyEffect: 318.54266729999995,
@ -303,7 +303,7 @@ describe('PortfolioCalculator', () => {
ytd: {
date: '2018-01-01',
netPerformance: -486.0860160000011,
netPerformanceInPercentage: -0.035084130151423504,
netPerformanceInPercentage: -0.035263202509112565,
netPerformanceInPercentageWithCurrencyEffect: -0.040162295064062624,
netPerformanceWithCurrencyEffect: -556.4433239999998,
totalInvestmentValueWithCurrencyEffect: 318.54266729999995,

Loading…
Cancel
Save