From 0a326b440457d2f181eb7a06ffed1be25f61ede1 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Fri, 22 Jul 2022 14:40:01 +0200 Subject: [PATCH] Fix investments by month --- .../src/app/portfolio/portfolio-calculator.ts | 21 +++++++++----- package.json | 2 +- .../ok-novn-buy-and-sell-partially.json | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 test/import/ok-novn-buy-and-sell-partially.json diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 5b21fe146..cafc65b8b 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -332,7 +332,7 @@ export class PortfolioCalculator { } const investments = []; - let currentDate = parseDate(this.orders[0].date); + let currentDate: Date; let investmentByMonth = new Big(0); for (const [index, order] of this.orders.entries()) { @@ -340,27 +340,34 @@ export class PortfolioCalculator { isSameMonth(parseDate(order.date), currentDate) && isSameYear(parseDate(order.date), currentDate) ) { + // Same month: Add up investments + investmentByMonth = investmentByMonth.plus( order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type)) ); + } else { + // New month: Store previous month and reset - if (index === this.orders.length - 1) { + if (currentDate) { investments.push({ date: format(set(currentDate, { date: 1 }), DATE_FORMAT), investment: investmentByMonth }); } - } else { - investments.push({ - date: format(set(currentDate, { date: 1 }), DATE_FORMAT), - investment: investmentByMonth - }); currentDate = parseDate(order.date); investmentByMonth = order.quantity .mul(order.unitPrice) .mul(this.getFactor(order.type)); } + + if (index === this.orders.length - 1) { + // Store current month (latest order) + investments.push({ + date: format(set(currentDate, { date: 1 }), DATE_FORMAT), + investment: investmentByMonth + }); + } } return investments; diff --git a/package.json b/package.json index 091ca3662..e6a9d92cf 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "start:server": "nx serve api --watch", "start:storybook": "nx run ui:storybook", "test": "nx test", - "test:single": "nx test --test-file portfolio-calculator-new.spec.ts", + "test:single": "nx test --test-file portfolio-calculator-novn-buy-and-sell-partially.spec.ts", "ts-node": "ts-node", "update": "nx migrate latest", "watch:server": "nx build api --watch", diff --git a/test/import/ok-novn-buy-and-sell-partially.json b/test/import/ok-novn-buy-and-sell-partially.json new file mode 100644 index 000000000..c184b4ba4 --- /dev/null +++ b/test/import/ok-novn-buy-and-sell-partially.json @@ -0,0 +1,28 @@ +{ + "meta": { + "date": "2022-07-21T21:28:05.857Z", + "version": "dev" + }, + "activities": [ + { + "fee": 0, + "quantity": 1, + "type": "SELL", + "unitPrice": 85.73, + "currency": "CHF", + "dataSource": "YAHOO", + "date": "2022-04-07T22:00:00.000Z", + "symbol": "NOVN.SW" + }, + { + "fee": 0, + "quantity": 2, + "type": "BUY", + "unitPrice": 75.8, + "currency": "CHF", + "dataSource": "YAHOO", + "date": "2022-03-06T23:00:00.000Z", + "symbol": "NOVN.SW" + } + ] +}