Skip to content

Commit

Permalink
Round sumWeights (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv authored Aug 13, 2024
1 parent 5241b4e commit e91ee1a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/portfolios/domain/portfolio-states.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TimeRange } from './entities/time-range.enum';
import { portfolioFactory } from './entities/__tests__/portfolio.factory';
import { positionFactory } from './entities/__tests__/position.factory';
import { PortfolioStatesService } from './portfolio-states.service';
import { round } from 'lodash';

describe('PortfolioStatesService', () => {
const portfolioStatesRepository = jest.mocked({
Expand All @@ -29,9 +30,9 @@ describe('PortfolioStatesService', () => {
(sum, pos) => sum + pos.value,
portfolio.cash,
);
const sumWeights = positions.reduce(
(acc, pos) => acc + pos.targetWeight,
0,
const sumWeights = round(
positions.reduce((acc, pos) => acc + pos.targetWeight, 0),
2,
);

await service.createPortfolioState(portfolio, positions);
Expand Down
6 changes: 5 additions & 1 deletion src/portfolios/domain/portfolio-states.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import { PortfolioState } from './entities/portfolio-state.entity';
import { Portfolio } from './entities/portfolio.entity';
import { Position } from './entities/position.entity';
import { TimeRange } from './entities/time-range.enum';
import { round } from 'lodash';

@Injectable()
export class PortfolioStatesService {
constructor(private readonly repository: PortfolioStatesRepository) {}

async createPortfolioState(portfolio: Portfolio, positions: Position[]) {
const sumWeights = positions.reduce((acc, p) => acc + p.targetWeight, 0);
const sumWeights = round(
positions.reduce((acc, p) => acc + p.targetWeight, 0),
2,
);
const cash = portfolio.cash ?? 0;
const totalValueEUR = positions.reduce((sum, pos) => sum + pos.value, cash);
const contributionsAmount = portfolio.contributions
Expand Down

0 comments on commit e91ee1a

Please sign in to comment.