Browse Source

Fix currency conversion in investment timeline

pull/2945/head
Thomas Kaul 2 years ago
parent
commit
456ec72fa3
  1. 30
      apps/api/src/app/portfolio/portfolio-calculator.ts
  2. 17
      apps/api/src/app/portfolio/portfolio.service.ts

30
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -685,9 +685,9 @@ export class PortfolioCalculator {
}); });
} }
public getInvestmentsByGroup( public async getInvestmentsByGroup(
groupBy: GroupBy groupBy: GroupBy
): { date: string; investment: Big }[] { ): Promise<{ date: string; investment: Big }[]> {
if (this.orders.length === 0) { if (this.orders.length === 0) {
return []; return [];
} }
@ -696,6 +696,18 @@ export class PortfolioCalculator {
let currentDate: Date; let currentDate: Date;
let investmentByGroup = new Big(0); let investmentByGroup = new Big(0);
const exchangeRatesByCurrency =
await this.exchangeRateDataService.getExchangeRatesByCurrency({
currencies: uniq(
this.orders.map(({ currency }) => {
return currency;
})
),
endDate: endOfDay(parseDate(last(this.transactionPoints).date)),
startDate: parseDate(first(this.transactionPoints).date),
targetCurrency: this.currency
});
for (const [index, order] of this.orders.entries()) { for (const [index, order] of this.orders.entries()) {
if ( if (
isSameYear(parseDate(order.date), currentDate) && isSameYear(parseDate(order.date), currentDate) &&
@ -703,7 +715,14 @@ export class PortfolioCalculator {
) { ) {
// Same group: Add up investments // Same group: Add up investments
investmentByGroup = investmentByGroup.plus( investmentByGroup = investmentByGroup.plus(
order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type)) order.quantity
.mul(order.unitPrice)
.mul(
exchangeRatesByCurrency[`${order.currency}${this.currency}`][
order.date
]
)
.mul(this.getFactor(order.type))
); );
} else { } else {
// New group: Store previous group and reset // New group: Store previous group and reset
@ -723,6 +742,11 @@ export class PortfolioCalculator {
currentDate = parseDate(order.date); currentDate = parseDate(order.date);
investmentByGroup = order.quantity investmentByGroup = order.quantity
.mul(order.unitPrice) .mul(order.unitPrice)
.mul(
exchangeRatesByCurrency[`${order.currency}${this.currency}`][
order.date
]
)
.mul(this.getFactor(order.type)); .mul(this.getFactor(order.type));
} }

17
apps/api/src/app/portfolio/portfolio.service.ts

@ -296,14 +296,15 @@ export class PortfolioService {
let investments: InvestmentItem[]; let investments: InvestmentItem[];
if (groupBy) { if (groupBy) {
investments = portfolioCalculator const investmentsByGroup =
.getInvestmentsByGroup(groupBy) await portfolioCalculator.getInvestmentsByGroup(groupBy);
.map((item) => {
return { investments = investmentsByGroup.map(({ date, investment }) => {
date: item.date, return {
investment: item.investment.toNumber() date,
}; investment: investment.toNumber()
}); };
});
// Add investment of current group // Add investment of current group
const dateOfCurrentGroup = format( const dateOfCurrentGroup = format(

Loading…
Cancel
Save