Browse Source

Merge pull request #3 from gizmodus/feature/fix-snapshot-average

Do not count days with 0 investment when calculating average investment
pull/3393/head
gizmodus 1 year ago
committed by GitHub
parent
commit
ae008a05d0
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts
  2. 2
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts
  3. 2
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts
  4. 35
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts

2
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts

@ -178,7 +178,7 @@ describe('PortfolioCalculator', () => {
'-0.05528341497550734703'
),
netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('-0.18658152554233729881') // TODO: Different
max: new Big('-0.0552834149755073478')
},
netPerformanceWithCurrencyEffect: new Big('-15.8'),
netPerformanceWithCurrencyEffectMap: {

2
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts

@ -163,7 +163,7 @@ describe('PortfolioCalculator', () => {
'-0.0552834149755073478'
),
netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('-0.18658152554233729881') // TODO: Different
max: new Big('-0.0552834149755073478')
},
netPerformanceWithCurrencyEffect: new Big('-15.8'),
netPerformanceWithCurrencyEffectMap: {

2
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts

@ -193,7 +193,7 @@ describe('PortfolioCalculator', () => {
'0.13100263852242744063'
),
netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('0.14737796833773087071')
max: new Big('0.13100263852242744063')
},
netPerformanceWithCurrencyEffect: new Big('19.86'),
netPerformanceWithCurrencyEffectMap: {

35
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts

@ -873,8 +873,6 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
startDate = addDays(start, 1);
}
let average = new Big(0);
const currentValuesAtStartDateWithCurrencyEffect =
currentValuesWithCurrencyEffect[format(startDate, DATE_FORMAT)] ??
new Big(0);
@ -897,11 +895,13 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
return format(date, DATE_FORMAT);
});
let average = new Big(0);
let dayCount = 0;
for (const date of dates) {
if (
investmentValuesAccumulatedWithCurrencyEffect[date] instanceof Big
investmentValuesAccumulatedWithCurrencyEffect[date] instanceof Big &&
investmentValuesAccumulatedWithCurrencyEffect[date].gt(0)
) {
average = average.add(
investmentValuesAccumulatedWithCurrencyEffect[date].add(
@ -913,18 +913,23 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
}
}
average = average.div(dayCount);
// TODO: Why without minus?
netPerformanceWithCurrencyEffectMap[dateRange] = average.gt(0)
? netPerformanceValuesWithCurrencyEffect[
format(endDate, DATE_FORMAT)
] /*.minus(
netPerformanceValuesWithCurrencyEffect[
format(startDate, DATE_FORMAT)
]
)*/
: new Big(0);
if (dayCount > 0) {
average = average.div(dayCount);
}
netPerformanceWithCurrencyEffectMap[dateRange] =
netPerformanceValuesWithCurrencyEffect[
format(endDate, DATE_FORMAT)
]?.minus(
// If the date range is 'max', take 0 as a start value. Otherwise,
// the value of the end of the day of the start date is taken which
// differs from the buying price.
dateRange === 'max'
? new Big(0)
: (netPerformanceValuesWithCurrencyEffect[
format(startDate, DATE_FORMAT)
] ?? new Big(0))
) ?? new Big(0);
netPerformancePercentageWithCurrencyEffectMap[dateRange] = average.gt(0)
? netPerformanceWithCurrencyEffectMap[dateRange].div(average)

Loading…
Cancel
Save