From ef4a75d1f0c0714ccff734fa7288e39e5c01bb64 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Tue, 9 May 2023 20:33:15 +0200 Subject: [PATCH] Feature/improve market price of first buy date in chart of position detail dialog (#1956) * Improve market price on first buy date: market price or average price * Update changelog --- CHANGELOG.md | 6 ++++ .../src/app/portfolio/portfolio.service.ts | 30 +++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e4f7a72c..be0e0135a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Improved the market price on the first buy date in the chart of the position detail dialog + ## 1.268.0 - 2023-05-08 ### Added diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 830a4bbe9..7755146f1 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -794,16 +794,6 @@ export class PortfolioService { let maxPrice = Math.max(orders[0].unitPrice, marketPrice); let minPrice = Math.min(orders[0].unitPrice, marketPrice); - if (!historicalData?.[aSymbol]?.[firstBuyDate]) { - // Add historical entry for buy date, if no historical data available - historicalDataArray.push({ - averagePrice: orders[0].unitPrice, - date: firstBuyDate, - marketPrice: orders[0].unitPrice, - quantity: orders[0].quantity - }); - } - if (historicalData[aSymbol]) { let j = -1; for (const [date, { marketPrice }] of Object.entries( @@ -815,11 +805,16 @@ export class PortfolioService { ) { j++; } + let currentAveragePrice = 0; let currentQuantity = 0; + const currentSymbol = transactionPoints[j].items.find( - (item) => item.symbol === aSymbol + ({ symbol }) => { + return symbol === aSymbol; + } ); + if (currentSymbol) { currentAveragePrice = currentSymbol.quantity.eq(0) ? 0 @@ -829,14 +824,25 @@ export class PortfolioService { historicalDataArray.push({ date, - marketPrice, averagePrice: currentAveragePrice, + marketPrice: + historicalDataArray.length > 0 + ? marketPrice + : currentAveragePrice, quantity: currentQuantity }); maxPrice = Math.max(marketPrice ?? 0, maxPrice); minPrice = Math.min(marketPrice ?? Number.MAX_SAFE_INTEGER, minPrice); } + } else { + // Add historical entry for buy date, if no historical data available + historicalDataArray.push({ + averagePrice: orders[0].unitPrice, + date: firstBuyDate, + marketPrice: orders[0].unitPrice, + quantity: orders[0].quantity + }); } return {