Skip to content

Commit

Permalink
average price is now computed as weighted mean instead of simple mean
Browse files Browse the repository at this point in the history
  • Loading branch information
jessy-bgl committed Jul 28, 2022
1 parent 059309f commit 6855e06
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { sumBy, meanBy, round } from 'lodash';
import { sumBy, sum, round } from 'lodash';

import { IDcaRepository } from '../../domain/repositories/dca.repository.interface';
import { ICoinRepository } from '../../domain/repositories/coin.repository.interface';
Expand Down Expand Up @@ -32,7 +32,8 @@ class DcaTransactionsSummaryUseCase {
2,
);
stats.avgPrice = round(
meanBy(transactions, (t) => t.price),
sum(transactions.map((tr) => tr.size * tr.price)) /
sumBy(transactions, (tr) => tr.size),
2,
);
stats.currentValue = round(stats.totalSize * currentPrice, 2);
Expand Down

0 comments on commit 6855e06

Please sign in to comment.