Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

fix single buy test

Valentin Zickner136563c9492a
diff --git a/apps/api/src/app/core/portfolio-calculator.spec.ts b/apps/api/src/app/core/portfolio-calculator.spec.ts
index 61c6666117..47a8a20d53 100644
--- a/apps/api/src/app/core/portfolio-calculator.spec.ts
+++ b/apps/api/src/app/core/portfolio-calculator.spec.ts
@@ -626,7 +626,7 @@ describe('PortfolioCalculator', () => {
.spyOn(Date, 'now')
.mockImplementation(() => new Date(Date.UTC(2021, 6, 26)).getTime()); // 2021-07-26
const currentPositions = await portfolioCalculator.getCurrentPositions(
- parse('2021-07-26', 'yyyy-MM-dd', new Date())
+ parse('2020-01-21', 'yyyy-MM-dd', new Date())
);
spy.mockRestore();
@@ -638,7 +638,7 @@ describe('PortfolioCalculator', () => {
currency: 'USD',
firstBuyDate: '2021-01-01',
grossPerformance: new Big('-61.84'), // 657.62-719.46=-61.84
- grossPerformancePercentage: new Big('-0.0859533539043171'), // (657.62-719.46)/719.46=-0.085953353904317
+ grossPerformancePercentage: new Big('-0.08595335390431712673'), // (657.62-719.46)/719.46=-0.085953353904317
investment: new Big('719.46'),
marketPrice: 657.62,
name: 'Tesla, Inc.',
@@ -673,9 +673,9 @@ describe('PortfolioCalculator', () => {
currency: 'USD',
firstBuyDate: '2019-02-01',
// see next test for details about how to calculate this
- grossPerformance: new Big('265.2'),
+ grossPerformance: new Big('240.4'),
grossPerformancePercentage: new Big(
- '0.37322057787174066244232522865731355471028555367747465860626740684417274277219590953836818016777856'
+ '0.349632913145865078264579821060810370805662039085569533288730749607797361322474717934042420125015808'
),
investment: new Big('4460.95'),
marketPrice: 194.86,
diff --git a/apps/api/src/app/core/portfolio-calculator.ts b/apps/api/src/app/core/portfolio-calculator.ts
index af2d5c9bec..9d59b6282c 100644
--- a/apps/api/src/app/core/portfolio-calculator.ts
+++ b/apps/api/src/app/core/portfolio-calculator.ts
@@ -212,7 +212,11 @@ export class PortfolioCalculator {
);
continue;
}
- if (!marketSymbolMap[currentDate]?.[item.symbol]) {
+ let currentValue = marketSymbolMap[currentDate]?.[item.symbol];
+ if (!isAfter(parseDate(currentDate), parseDate(item.firstBuyDate))) {
+ currentValue = item.investment.div(item.quantity);
+ }
+ if (!currentValue) {
invalidSymbols.push(item.symbol);
hasErrors = true;
console.error(
@@ -221,9 +225,7 @@ export class PortfolioCalculator {
continue;
}
holdingPeriodReturns[item.symbol] = oldHoldingPeriodReturn.mul(
- marketSymbolMap[nextDate][item.symbol].div(
- marketSymbolMap[currentDate][item.symbol]
- )
+ marketSymbolMap[nextDate][item.symbol].div(currentValue)
);
let oldGrossPerformance = grossPerformance[item.symbol];
if (!oldGrossPerformance) {
@@ -231,7 +233,7 @@ export class PortfolioCalculator {
}
grossPerformance[item.symbol] = oldGrossPerformance.plus(
marketSymbolMap[nextDate][item.symbol]
- .minus(marketSymbolMap[currentDate][item.symbol])
+ .minus(currentValue)
.mul(item.quantity)
);
}