Browse Source

Fix investments by month

pull/1089/head
Thomas 3 years ago
parent
commit
0a326b4404
  1. 21
      apps/api/src/app/portfolio/portfolio-calculator.ts
  2. 2
      package.json
  3. 28
      test/import/ok-novn-buy-and-sell-partially.json

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

@ -332,7 +332,7 @@ export class PortfolioCalculator {
} }
const investments = []; const investments = [];
let currentDate = parseDate(this.orders[0].date); let currentDate: Date;
let investmentByMonth = new Big(0); let investmentByMonth = new Big(0);
for (const [index, order] of this.orders.entries()) { for (const [index, order] of this.orders.entries()) {
@ -340,27 +340,34 @@ export class PortfolioCalculator {
isSameMonth(parseDate(order.date), currentDate) && isSameMonth(parseDate(order.date), currentDate) &&
isSameYear(parseDate(order.date), currentDate) isSameYear(parseDate(order.date), currentDate)
) { ) {
// Same month: Add up investments
investmentByMonth = investmentByMonth.plus( investmentByMonth = investmentByMonth.plus(
order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type)) 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({ investments.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), date: format(set(currentDate, { date: 1 }), DATE_FORMAT),
investment: investmentByMonth investment: investmentByMonth
}); });
} }
} else {
investments.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT),
investment: investmentByMonth
});
currentDate = parseDate(order.date); currentDate = parseDate(order.date);
investmentByMonth = order.quantity investmentByMonth = order.quantity
.mul(order.unitPrice) .mul(order.unitPrice)
.mul(this.getFactor(order.type)); .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; return investments;

2
package.json

@ -43,7 +43,7 @@
"start:server": "nx serve api --watch", "start:server": "nx serve api --watch",
"start:storybook": "nx run ui:storybook", "start:storybook": "nx run ui:storybook",
"test": "nx test", "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", "ts-node": "ts-node",
"update": "nx migrate latest", "update": "nx migrate latest",
"watch:server": "nx build api --watch", "watch:server": "nx build api --watch",

28
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"
}
]
}
Loading…
Cancel
Save