From e75ad5b4953265608a2a32f4dced5d067dd31855 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Mon, 9 Jan 2023 21:45:13 +0100 Subject: [PATCH] Fix exception with missing marketPrice --- .../src/app/portfolio/portfolio-calculator.ts | 54 +++++++++---------- .../src/app/portfolio/portfolio.service.ts | 23 +++++--- .../base-currency-current-investment.ts | 7 ++- .../base-currency-initial-investment.ts | 7 ++- .../current-investment.ts | 9 ++-- .../initial-investment.ts | 7 ++- 6 files changed, 55 insertions(+), 52 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index cb07acc1a..48f1e7507 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -422,35 +422,33 @@ export class PortfolioCalculator { symbol: item.symbol }); - 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 - }); + 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 49a5e26e9..f752010b3 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -910,12 +910,14 @@ export class PortfolioService { const positions = currentPositions.positions.filter( (item) => !item.quantity.eq(0) ); + const dataGatheringItem = positions.map((position) => { return { dataSource: position.dataSource, symbol: position.symbol }; }); + const symbols = positions.map((position) => position.symbol); const [dataProviderResponses, symbolProfiles] = await Promise.all([ @@ -1103,16 +1105,23 @@ export class PortfolioService { portfolioStart ); + const positions = currentPositions.positions.filter( + (item) => !item.quantity.eq(0) + ); + const portfolioItemsNow: { [symbol: string]: TimelinePosition } = {}; - for (const position of currentPositions.positions) { + + for (const position of positions) { portfolioItemsNow[position.symbol] = position; } + const accounts = await this.getValueOfAccounts({ orders, portfolioItemsNow, - userId, - userCurrency + userCurrency, + userId }); + return { rules: { accountClusterRisk: await this.rulesService.evaluate( @@ -1136,19 +1145,19 @@ export class PortfolioService { [ new CurrencyClusterRiskBaseCurrencyInitialInvestment( this.exchangeRateDataService, - currentPositions + positions ), new CurrencyClusterRiskBaseCurrencyCurrentInvestment( this.exchangeRateDataService, - currentPositions + positions ), new CurrencyClusterRiskInitialInvestment( this.exchangeRateDataService, - currentPositions + positions ), new CurrencyClusterRiskCurrentInvestment( this.exchangeRateDataService, - currentPositions + positions ) ], this.request.user.Settings.settings diff --git a/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts b/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts index 5f1f4cf93..1d584b04f 100644 --- a/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts +++ b/apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts @@ -1,14 +1,13 @@ -import { CurrentPositions } from '@ghostfolio/api/app/portfolio/interfaces/current-positions.interface'; import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.interface'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; -import { UserSettings } from '@ghostfolio/common/interfaces'; +import { TimelinePosition, UserSettings } from '@ghostfolio/common/interfaces'; import { Rule } from '../../rule'; export class CurrencyClusterRiskBaseCurrencyCurrentInvestment extends Rule { public constructor( protected exchangeRateDataService: ExchangeRateDataService, - private currentPositions: CurrentPositions + private positions: TimelinePosition[] ) { super(exchangeRateDataService, { name: 'Current Investment: Base Currency' @@ -17,7 +16,7 @@ export class CurrencyClusterRiskBaseCurrencyCurrentInvestment extends Rule { public constructor( protected exchangeRateDataService: ExchangeRateDataService, - private currentPositions: CurrentPositions + private positions: TimelinePosition[] ) { super(exchangeRateDataService, { name: 'Initial Investment: Base Currency' @@ -17,7 +16,7 @@ export class CurrencyClusterRiskBaseCurrencyInitialInvestment extends Rule { public constructor( - public exchangeRateDataService: ExchangeRateDataService, - private currentPositions: CurrentPositions + protected exchangeRateDataService: ExchangeRateDataService, + private positions: TimelinePosition[] ) { super(exchangeRateDataService, { name: 'Current Investment' @@ -17,7 +16,7 @@ export class CurrencyClusterRiskCurrentInvestment extends Rule { public evaluate(ruleSettings: Settings) { const positionsGroupedByCurrency = this.groupCurrentPositionsByAttribute( - this.currentPositions.positions, + this.positions, 'currency', ruleSettings.baseCurrency ); diff --git a/apps/api/src/models/rules/currency-cluster-risk/initial-investment.ts b/apps/api/src/models/rules/currency-cluster-risk/initial-investment.ts index 331074f16..18477f6f7 100644 --- a/apps/api/src/models/rules/currency-cluster-risk/initial-investment.ts +++ b/apps/api/src/models/rules/currency-cluster-risk/initial-investment.ts @@ -1,14 +1,13 @@ -import { CurrentPositions } from '@ghostfolio/api/app/portfolio/interfaces/current-positions.interface'; import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.interface'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; -import { UserSettings } from '@ghostfolio/common/interfaces'; +import { TimelinePosition, UserSettings } from '@ghostfolio/common/interfaces'; import { Rule } from '../../rule'; export class CurrencyClusterRiskInitialInvestment extends Rule { public constructor( protected exchangeRateDataService: ExchangeRateDataService, - private currentPositions: CurrentPositions + private positions: TimelinePosition[] ) { super(exchangeRateDataService, { name: 'Initial Investment' @@ -17,7 +16,7 @@ export class CurrencyClusterRiskInitialInvestment extends Rule { public evaluate(ruleSettings: Settings) { const positionsGroupedByCurrency = this.groupCurrentPositionsByAttribute( - this.currentPositions.positions, + this.positions, 'currency', ruleSettings.baseCurrency );