Browse Source

Feature/refactor markets calculation in details of portfolio service (#3871)

* Refactor markets calculation
pull/3876/head
Thomas Kaul 4 months ago
committed by GitHub
parent
commit
f3e2091ff4
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 151
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 14
      libs/common/src/lib/interfaces/portfolio-details.interface.ts

151
apps/api/src/app/portfolio/portfolio.service.ts

@ -45,7 +45,6 @@ import type {
AccountWithValue, AccountWithValue,
DateRange, DateRange,
GroupBy, GroupBy,
Market,
RequestWithUser, RequestWithUser,
UserWithSettings UserWithSettings
} from '@ghostfolio/common/types'; } from '@ghostfolio/common/types';
@ -582,15 +581,11 @@ export class PortfolioService {
}; };
} }
let markets: { let markets: PortfolioDetails['markets'];
[key in Market]: { let marketsAdvanced: PortfolioDetails['marketsAdvanced'];
name: string;
value: number;
};
};
if (withMarkets) { if (withMarkets) {
markets = this.getAggregatedMarkets(holdings); ({ markets, marketsAdvanced } = this.getAggregatedMarkets(holdings));
} }
let summary: PortfolioSummary; let summary: PortfolioSummary;
@ -615,6 +610,7 @@ export class PortfolioService {
hasErrors, hasErrors,
holdings, holdings,
markets, markets,
marketsAdvanced,
platforms, platforms,
summary summary
}; };
@ -1248,24 +1244,67 @@ export class PortfolioService {
private getAggregatedMarkets(holdings: { private getAggregatedMarkets(holdings: {
[symbol: string]: PortfolioPosition; [symbol: string]: PortfolioPosition;
}): { }): {
[key in Market]: { name: string; value: number }; markets: PortfolioDetails['markets'];
marketsAdvanced: PortfolioDetails['marketsAdvanced'];
} { } {
const markets = { const markets: PortfolioDetails['markets'] = {
[UNKNOWN_KEY]: { [UNKNOWN_KEY]: {
name: UNKNOWN_KEY, id: UNKNOWN_KEY,
value: 0 valueInBaseCurrency: 0,
valueInPercentage: 0
}, },
developedMarkets: { developedMarkets: {
name: 'developedMarkets', id: 'developedMarkets',
value: 0 valueInBaseCurrency: 0,
valueInPercentage: 0
}, },
emergingMarkets: { emergingMarkets: {
name: 'emergingMarkets', id: 'emergingMarkets',
value: 0 valueInBaseCurrency: 0,
valueInPercentage: 0
}, },
otherMarkets: { otherMarkets: {
name: 'otherMarkets', id: 'otherMarkets',
value: 0 valueInBaseCurrency: 0,
valueInPercentage: 0
}
};
const marketsAdvanced: PortfolioDetails['marketsAdvanced'] = {
[UNKNOWN_KEY]: {
id: UNKNOWN_KEY,
valueInBaseCurrency: 0,
valueInPercentage: 0
},
asiaPacific: {
id: 'asiaPacific',
valueInBaseCurrency: 0,
valueInPercentage: 0
},
emergingMarkets: {
id: 'emergingMarkets',
valueInBaseCurrency: 0,
valueInPercentage: 0
},
europe: {
id: 'europe',
valueInBaseCurrency: 0,
valueInPercentage: 0
},
japan: {
id: 'japan',
valueInBaseCurrency: 0,
valueInPercentage: 0
},
northAmerica: {
id: 'northAmerica',
valueInBaseCurrency: 0,
valueInPercentage: 0
},
otherMarkets: {
id: 'otherMarkets',
valueInBaseCurrency: 0,
valueInPercentage: 0
} }
}; };
@ -1274,31 +1313,73 @@ export class PortfolioService {
if (position.assetClass !== AssetClass.LIQUIDITY) { if (position.assetClass !== AssetClass.LIQUIDITY) {
if (position.countries.length > 0) { if (position.countries.length > 0) {
markets.developedMarkets.value += markets.developedMarkets.valueInBaseCurrency +=
position.markets.developedMarkets * value; position.markets.developedMarkets * value;
markets.emergingMarkets.value += markets.emergingMarkets.valueInBaseCurrency +=
position.markets.emergingMarkets * value; position.markets.emergingMarkets * value;
markets.otherMarkets.value += position.markets.otherMarkets * value; markets.otherMarkets.valueInBaseCurrency +=
position.markets.otherMarkets * value;
marketsAdvanced.asiaPacific.valueInBaseCurrency +=
position.marketsAdvanced.asiaPacific * value;
marketsAdvanced.emergingMarkets.valueInBaseCurrency +=
position.marketsAdvanced.emergingMarkets * value;
marketsAdvanced.europe.valueInBaseCurrency +=
position.marketsAdvanced.europe * value;
marketsAdvanced.japan.valueInBaseCurrency +=
position.marketsAdvanced.japan * value;
marketsAdvanced.northAmerica.valueInBaseCurrency +=
position.marketsAdvanced.northAmerica * value;
marketsAdvanced.otherMarkets.valueInBaseCurrency +=
position.marketsAdvanced.otherMarkets * value;
} else { } else {
markets[UNKNOWN_KEY].value += value; markets[UNKNOWN_KEY].valueInBaseCurrency += value;
marketsAdvanced[UNKNOWN_KEY].valueInBaseCurrency += value;
} }
} }
} }
const marketsTotal = const marketsTotal =
markets.developedMarkets.value + markets.developedMarkets.valueInBaseCurrency +
markets.emergingMarkets.value + markets.emergingMarkets.valueInBaseCurrency +
markets.otherMarkets.value + markets.otherMarkets.valueInBaseCurrency +
markets[UNKNOWN_KEY].value; markets[UNKNOWN_KEY].valueInBaseCurrency;
markets.developedMarkets.value = markets.developedMarkets.valueInPercentage =
markets.developedMarkets.value / marketsTotal; markets.developedMarkets.valueInBaseCurrency / marketsTotal;
markets.emergingMarkets.value = markets.emergingMarkets.valueInPercentage =
markets.emergingMarkets.value / marketsTotal; markets.emergingMarkets.valueInBaseCurrency / marketsTotal;
markets.otherMarkets.value = markets.otherMarkets.value / marketsTotal; markets.otherMarkets.valueInPercentage =
markets[UNKNOWN_KEY].value = markets[UNKNOWN_KEY].value / marketsTotal; markets.otherMarkets.valueInBaseCurrency / marketsTotal;
markets[UNKNOWN_KEY].valueInPercentage =
return markets; markets[UNKNOWN_KEY].valueInBaseCurrency / marketsTotal;
const marketsAdvancedTotal =
marketsAdvanced.asiaPacific.valueInBaseCurrency +
marketsAdvanced.emergingMarkets.valueInBaseCurrency +
marketsAdvanced.europe.valueInBaseCurrency +
marketsAdvanced.japan.valueInBaseCurrency +
marketsAdvanced.northAmerica.valueInBaseCurrency +
marketsAdvanced.otherMarkets.valueInBaseCurrency +
marketsAdvanced[UNKNOWN_KEY].valueInBaseCurrency;
marketsAdvanced.asiaPacific.valueInPercentage =
marketsAdvanced.asiaPacific.valueInBaseCurrency / marketsAdvancedTotal;
marketsAdvanced.emergingMarkets.valueInPercentage =
marketsAdvanced.emergingMarkets.valueInBaseCurrency /
marketsAdvancedTotal;
marketsAdvanced.europe.valueInPercentage =
marketsAdvanced.europe.valueInBaseCurrency / marketsAdvancedTotal;
marketsAdvanced.japan.valueInPercentage =
marketsAdvanced.japan.valueInBaseCurrency / marketsAdvancedTotal;
marketsAdvanced.northAmerica.valueInPercentage =
marketsAdvanced.northAmerica.valueInBaseCurrency / marketsAdvancedTotal;
marketsAdvanced.otherMarkets.valueInPercentage =
marketsAdvanced.otherMarkets.valueInBaseCurrency / marketsAdvancedTotal;
marketsAdvanced[UNKNOWN_KEY].valueInPercentage =
marketsAdvanced[UNKNOWN_KEY].valueInBaseCurrency / marketsAdvancedTotal;
return { markets, marketsAdvanced };
} }
private async getCashPositions({ private async getCashPositions({

14
libs/common/src/lib/interfaces/portfolio-details.interface.ts

@ -2,7 +2,7 @@ import {
PortfolioPosition, PortfolioPosition,
PortfolioSummary PortfolioSummary
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { Market } from '@ghostfolio/common/types'; import { Market, MarketAdvanced } from '@ghostfolio/common/types';
export interface PortfolioDetails { export interface PortfolioDetails {
accounts: { accounts: {
@ -17,8 +17,16 @@ export interface PortfolioDetails {
holdings: { [symbol: string]: PortfolioPosition }; holdings: { [symbol: string]: PortfolioPosition };
markets?: { markets?: {
[key in Market]: { [key in Market]: {
name: string; id: Market;
value: number; valueInBaseCurrency: number;
valueInPercentage: number;
};
};
marketsAdvanced?: {
[key in MarketAdvanced]: {
id: MarketAdvanced;
valueInBaseCurrency: number;
valueInPercentage: number;
}; };
}; };
platforms: { platforms: {

Loading…
Cancel
Save