From 83e9a6a6c7ecb2faf357ec7965fbe57855f9f733 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Mon, 9 Jan 2023 20:16:08 +0100 Subject: [PATCH] Fix exception with missing marketPrice --- .../src/app/portfolio/portfolio-calculator.ts | 54 ++++++++++--------- .../src/app/portfolio/portfolio.service.ts | 2 +- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 48f1e7507..cb07acc1a 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -422,33 +422,35 @@ export class PortfolioCalculator { symbol: item.symbol }); - hasAnySymbolMetricsErrors = hasAnySymbolMetricsErrors || hasErrors; - initialValues[item.symbol] = initialValue; - - positions.push({ - averagePrice: item.quantity.eq(0) - ? new Big(0) - : item.investment.div(item.quantity), - currency: item.currency, - dataSource: item.dataSource, - firstBuyDate: item.firstBuyDate, - grossPerformance: !hasErrors ? grossPerformance ?? null : null, - grossPerformancePercentage: !hasErrors - ? grossPerformancePercentage ?? null - : null, - investment: item.investment, - marketPrice: marketValue?.toNumber() ?? null, - netPerformance: !hasErrors ? netPerformance ?? null : null, - netPerformancePercentage: !hasErrors - ? netPerformancePercentage ?? null - : null, - quantity: item.quantity, - symbol: item.symbol, - transactionCount: item.transactionCount - }); + if (item.quantity.gt(0)) { + hasAnySymbolMetricsErrors = hasAnySymbolMetricsErrors || hasErrors; + initialValues[item.symbol] = initialValue; + + positions.push({ + averagePrice: item.quantity.eq(0) + ? new Big(0) + : item.investment.div(item.quantity), + currency: item.currency, + dataSource: item.dataSource, + firstBuyDate: item.firstBuyDate, + grossPerformance: !hasErrors ? grossPerformance ?? null : null, + grossPerformancePercentage: !hasErrors + ? grossPerformancePercentage ?? null + : null, + investment: item.investment, + marketPrice: marketValue?.toNumber() ?? null, + netPerformance: !hasErrors ? netPerformance ?? null : null, + netPerformancePercentage: !hasErrors + ? netPerformancePercentage ?? null + : null, + quantity: item.quantity, + symbol: item.symbol, + transactionCount: item.transactionCount + }); - if (hasErrors) { - errors.push({ dataSource: item.dataSource, symbol: item.symbol }); + if (hasErrors) { + errors.push({ dataSource: item.dataSource, symbol: item.symbol }); + } } } diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 37fae84d7..49a5e26e9 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1682,7 +1682,7 @@ export class PortfolioService { for (const order of ordersByAccount) { let currentValueOfSymbolInBaseCurrency = order.quantity * - portfolioItemsNow[order.SymbolProfile.symbol].marketPrice; + portfolioItemsNow[order.SymbolProfile.symbol]?.marketPrice ?? 0; let originalValueOfSymbolInBaseCurrency = this.exchangeRateDataService.toCurrency( order.quantity * order.unitPrice,