Skip to content

Commit

Permalink
include-cash-in-total-value-calculation (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv authored Aug 31, 2023
1 parent 6fff8c1 commit 47d5c3f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/portfolios/domain/portfolio-states.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ describe('PortfolioStatesService', () => {
positionFactory(),
positionFactory(),
];
const totalValueEUR = positions.reduce((sum, pos) => sum + pos.value, 0);
const totalValueEUR = positions.reduce(
(sum, pos) => sum + pos.value,
portfolio.cash,
);
const sumWeights = positions.reduce(
(acc, pos) => acc + pos.targetWeight,
0,
Expand All @@ -41,8 +44,7 @@ describe('PortfolioStatesService', () => {
totalValueEUR,
cash: portfolio.cash,
roicEUR:
totalValueEUR +
portfolio.cash -
totalValueEUR -
portfolio.contributions.reduce((sum, i) => sum + i.amountEUR, 0),
}),
);
Expand Down
4 changes: 2 additions & 2 deletions src/portfolios/domain/portfolio-states.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class PortfolioStatesService {

async createPortfolioState(portfolio: Portfolio, positions: Position[]) {
const sumWeights = positions.reduce((acc, p) => acc + p.targetWeight, 0);
const totalValueEUR = positions.reduce((sum, pos) => sum + pos.value, 0);
const cash = portfolio.cash ?? 0;
const totalValueEUR = positions.reduce((sum, pos) => sum + pos.value, cash);
const contributionsAmount = portfolio.contributions
? portfolio.contributions.reduce(
(sum, contribution) => sum + contribution.amountEUR,
Expand All @@ -31,7 +31,7 @@ export class PortfolioStatesService {
sumWeights,
cash,
totalValueEUR,
roicEUR: totalValueEUR + cash - contributionsAmount,
roicEUR: totalValueEUR - contributionsAmount,
});
}

Expand Down

0 comments on commit 47d5c3f

Please sign in to comment.