From f403807f2d5bea5d47b7ab590ed04ad2cd9498ad Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 10 Jul 2021 18:16:46 +0200 Subject: [PATCH] Bugfix/fix average buy price calculation (#204) * Fix average buy price calculation * Update changelog --- CHANGELOG.md | 1 + .../src/app/portfolio/portfolio.service.ts | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff12bfe..4ec4f47a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed rendering of currency and platform in dialogs (account and transaction) +- Fixed an issue in the calculation of the average buy prices in the position detail chart ## 1.24.0 - 07.07.2021 diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 71fcadd1..6493bf08 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -18,6 +18,7 @@ import { REQUEST } from '@nestjs/core'; import { DataSource } from '@prisma/client'; import { add, + addMonths, endOfToday, format, getDate, @@ -227,19 +228,18 @@ export class PortfolioService { impersonationUserId || this.request.user.id ); - const positions = portfolio.getPositions(new Date())[aSymbol]; + const position = portfolio.getPositions(new Date())[aSymbol]; - if (positions) { - let { + if (position) { + const { averagePrice, currency, firstBuyDate, investment, - marketPrice, quantity, transactionCount - } = portfolio.getPositions(new Date())[aSymbol]; - + } = position; + let marketPrice = position.marketPrice; const orders = portfolio.getOrders(aSymbol); const historicalData = await this.dataProviderService.getHistorical( @@ -267,13 +267,14 @@ export class PortfolioService { isSameDay(currentDate, parseISO(orders[0]?.getDate())) || isAfter(currentDate, parseISO(orders[0]?.getDate())) ) { - // Get snapshot of first day of month - const snapshot = portfolio.get(setDate(currentDate, 1))[0] - .positions[aSymbol]; + // Get snapshot of first day of next month + const snapshot = portfolio.get( + addMonths(setDate(currentDate, 1), 1) + )?.[0]?.positions[aSymbol]; orders.shift(); if (snapshot?.averagePrice) { - currentAveragePrice = snapshot?.averagePrice; + currentAveragePrice = snapshot.averagePrice; } }