Browse Source

Fix net performance percentage of date ranges

pull/7911/head
Thomas Kaul 4 weeks ago
parent
commit
8fca83cfa0
  1. 20
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  2. 16
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts
  3. 8
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-with-dividend.spec.ts

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

@ -794,15 +794,20 @@ export abstract class PortfolioCalculator {
const chart: HistoricalDataItem[] = [];
let grossPerformanceWithCurrencyEffectAtStartDate: number;
let netPerformanceAtStartDate: number;
let netPerformanceWithCurrencyEffectAtStartDate: number;
const totalInvestmentValuesWithCurrencyEffect: number[] = [];
const timeWeightedInvestmentValuesWithCurrencyEffect: number[] = [];
for (const historicalDataItem of historicalData) {
const date = resetHours(parseDate(historicalDataItem.date));
if (!isBefore(date, start) && !isAfter(date, end)) {
if (!isNumber(netPerformanceAtStartDate)) {
grossPerformanceWithCurrencyEffectAtStartDate =
historicalDataItem.valueWithCurrencyEffect -
historicalDataItem.totalInvestmentValueWithCurrencyEffect;
netPerformanceAtStartDate = historicalDataItem.netPerformance;
netPerformanceWithCurrencyEffectAtStartDate =
@ -817,15 +822,18 @@ export abstract class PortfolioCalculator {
netPerformanceWithCurrencyEffectAtStartDate;
if (historicalDataItem.totalInvestmentValueWithCurrencyEffect > 0) {
totalInvestmentValuesWithCurrencyEffect.push(
historicalDataItem.totalInvestmentValueWithCurrencyEffect
// The invested capital in the date range is the value at its start
// date: the investment plus the gross performance up to that date
timeWeightedInvestmentValuesWithCurrencyEffect.push(
historicalDataItem.totalInvestmentValueWithCurrencyEffect +
grossPerformanceWithCurrencyEffectAtStartDate
);
}
const timeWeightedInvestmentValue =
totalInvestmentValuesWithCurrencyEffect.length > 0
? sum(totalInvestmentValuesWithCurrencyEffect) /
totalInvestmentValuesWithCurrencyEffect.length
timeWeightedInvestmentValuesWithCurrencyEffect.length > 0
? sum(timeWeightedInvestmentValuesWithCurrencyEffect) /
timeWeightedInvestmentValuesWithCurrencyEffect.length
: 0;
chart.push({

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

@ -190,10 +190,14 @@ describe('PortfolioCalculator', () => {
netPerformance: new Big('27172.74').mul(0.97373),
netPerformancePercentage: new Big('42.41978276196153750666'),
netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('41.72313811883729606471')
'1d': new Big('-0.04016229506406263535'),
max: new Big('41.72313811883729606471'),
ytd: new Big('-0.04016229506406263535')
},
netPerformanceWithCurrencyEffectMap: {
max: new Big('26516.208701400000064086')
'1d': new Big('-556.443324'),
max: new Big('26516.208701400000064086'),
ytd: new Big('-556.443324')
},
quantity: new Big('1'),
symbol: 'BTCUSD',
@ -281,8 +285,8 @@ describe('PortfolioCalculator', () => {
'1d': {
date: '2018-01-01',
netPerformance: -486.0860160000011,
netPerformanceInPercentage: -1.5259683109961866,
netPerformanceInPercentageWithCurrencyEffect: -1.7468407881319952,
netPerformanceInPercentage: -0.035084130151423504,
netPerformanceInPercentageWithCurrencyEffect: -0.040162295064062624,
netPerformanceWithCurrencyEffect: -556.4433239999998,
totalInvestmentValueWithCurrencyEffect: 318.54266729999995,
valueWithCurrencyEffect: 13298.425356
@ -299,8 +303,8 @@ describe('PortfolioCalculator', () => {
ytd: {
date: '2018-01-01',
netPerformance: -486.0860160000011,
netPerformanceInPercentage: -1.5259683109961866,
netPerformanceInPercentageWithCurrencyEffect: -1.7468407881319952,
netPerformanceInPercentage: -0.035084130151423504,
netPerformanceInPercentageWithCurrencyEffect: -0.040162295064062624,
netPerformanceWithCurrencyEffect: -556.4433239999998,
totalInvestmentValueWithCurrencyEffect: 318.54266729999995,
valueWithCurrencyEffect: 13298.425356

8
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-with-dividend.spec.ts

@ -193,8 +193,8 @@ describe('PortfolioCalculator', () => {
'1d': {
date: '2023-07-10',
netPerformance: -5.390000000000001,
netPerformanceInPercentage: -0.018052113336459244,
netPerformanceInPercentageWithCurrencyEffect: -0.018052113336459244,
netPerformanceInPercentage: -0.015983630864124312,
netPerformanceInPercentageWithCurrencyEffect: -0.015983630864124312,
netPerformanceWithCurrencyEffect: -5.390000000000001,
totalInvestmentValueWithCurrencyEffect: 298.58,
valueWithCurrencyEffect: 331.83
@ -211,8 +211,8 @@ describe('PortfolioCalculator', () => {
ytd: {
date: '2023-07-10',
netPerformance: -7.68,
netPerformanceInPercentage: -0.025721749614843515,
netPerformanceInPercentageWithCurrencyEffect: -0.025721749614843515,
netPerformanceInPercentage: -0.022620835910577012,
netPerformanceInPercentageWithCurrencyEffect: -0.022620835910577012,
netPerformanceWithCurrencyEffect: -7.68,
totalInvestmentValueWithCurrencyEffect: 298.58,
valueWithCurrencyEffect: 331.83

Loading…
Cancel
Save