From ef272360fbf4728e7ddd5171272bb4b727089a3a Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 5 Jun 2021 17:17:53 +0200 Subject: [PATCH] Feature/render average prices in position detail chart (#144) * Render average buy prices * Update changelog --- CHANGELOG.md | 1 + .../src/app/portfolio/portfolio.service.ts | 21 ++++++++++++++++++- apps/api/src/models/portfolio.ts | 8 ++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72c39a37a..3e26d4c86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added a dedicated page for the account registration +- Rendered the average buy prices in the position detail chart (useful for recurring transactions) ### Changed diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index e45fae214..44c055af0 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -20,6 +20,7 @@ import { getYear, isAfter, isSameDay, + parse, parseISO, setDate, setMonth, @@ -215,6 +216,8 @@ export class PortfolioService { transactionCount } = portfolio.getPositions(new Date())[aSymbol]; + const orders = portfolio.getOrders(aSymbol); + const historicalData = await this.dataProviderService.getHistorical( [aSymbol], 'day', @@ -227,6 +230,7 @@ export class PortfolioService { } const historicalDataArray: HistoricalDataItem[] = []; + let currentAveragePrice: number; let maxPrice = marketPrice; let minPrice = marketPrice; @@ -234,9 +238,24 @@ export class PortfolioService { for (const [date, { marketPrice }] of Object.entries( historicalData[aSymbol] )) { + const currentDate = parse(date, 'yyyy-MM-dd', new Date()); + if ( + 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]; + orders.shift(); + + if (snapshot?.averagePrice) { + currentAveragePrice = snapshot?.averagePrice; + } + } + historicalDataArray.push({ - averagePrice, date, + averagePrice: currentAveragePrice, value: marketPrice }); diff --git a/apps/api/src/models/portfolio.ts b/apps/api/src/models/portfolio.ts index 1176fa945..5cd26ba29 100644 --- a/apps/api/src/models/portfolio.ts +++ b/apps/api/src/models/portfolio.ts @@ -486,7 +486,13 @@ export class Portfolio implements PortfolioInterface { .reduce((previous, current) => previous + current, 0); } - public getOrders() { + public getOrders(aSymbol?: string) { + if (aSymbol) { + return this.orders.filter((order) => { + return order.getSymbol() === aSymbol; + }); + } + return this.orders; }