Browse Source

Refactor value to valueInBaseCurrency (#2160)

pull/2164/head
Thomas Kaul 2 years ago
committed by GitHub
parent
commit
455a2d2e92
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      apps/api/src/app/portfolio/portfolio.controller.ts
  2. 12
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 42
      apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts
  4. 48
      apps/client/src/app/pages/public/public-page.component.ts
  5. 6
      apps/client/src/app/services/data.service.ts
  6. 2
      libs/common/src/lib/interfaces/portfolio-position.interface.ts
  7. 2
      libs/common/src/lib/interfaces/portfolio-public-details.interface.ts

7
apps/api/src/app/portfolio/portfolio.controller.ts

@ -134,7 +134,7 @@ export class PortfolioController {
portfolioPosition.netPerformance = null; portfolioPosition.netPerformance = null;
portfolioPosition.quantity = null; portfolioPosition.quantity = null;
portfolioPosition.valueInPercentage = portfolioPosition.valueInPercentage =
portfolioPosition.value / totalValue; portfolioPosition.valueInBaseCurrency / totalValue;
} }
for (const [name, { valueInBaseCurrency }] of Object.entries(accounts)) { for (const [name, { valueInBaseCurrency }] of Object.entries(accounts)) {
@ -445,7 +445,8 @@ export class PortfolioController {
for (const [symbol, portfolioPosition] of Object.entries(holdings)) { for (const [symbol, portfolioPosition] of Object.entries(holdings)) {
portfolioPublicDetails.holdings[symbol] = { portfolioPublicDetails.holdings[symbol] = {
allocationInPercentage: portfolioPosition.value / totalValue, allocationInPercentage:
portfolioPosition.valueInBaseCurrency / totalValue,
countries: hasDetails ? portfolioPosition.countries : [], countries: hasDetails ? portfolioPosition.countries : [],
currency: hasDetails ? portfolioPosition.currency : undefined, currency: hasDetails ? portfolioPosition.currency : undefined,
dataSource: portfolioPosition.dataSource, dataSource: portfolioPosition.dataSource,
@ -456,7 +457,7 @@ export class PortfolioController {
sectors: hasDetails ? portfolioPosition.sectors : [], sectors: hasDetails ? portfolioPosition.sectors : [],
symbol: portfolioPosition.symbol, symbol: portfolioPosition.symbol,
url: portfolioPosition.url, url: portfolioPosition.url,
valueInPercentage: portfolioPosition.value / totalValue valueInPercentage: portfolioPosition.valueInBaseCurrency / totalValue
}; };
} }

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

@ -585,7 +585,7 @@ export class PortfolioService {
symbol: item.symbol, symbol: item.symbol,
transactionCount: item.transactionCount, transactionCount: item.transactionCount,
url: symbolProfile.url, url: symbolProfile.url,
value: value.toNumber() valueInBaseCurrency: value.toNumber()
}; };
} }
@ -645,7 +645,7 @@ export class PortfolioService {
holdings[userCurrency] = { holdings[userCurrency] = {
...emergencyFundCashPositions[userCurrency], ...emergencyFundCashPositions[userCurrency],
investment: emergencyFundInCash, investment: emergencyFundInCash,
value: emergencyFundInCash valueInBaseCurrency: emergencyFundInCash
}; };
} }
@ -1278,7 +1278,7 @@ export class PortfolioService {
if (cashPositions[account.currency]) { if (cashPositions[account.currency]) {
cashPositions[account.currency].investment += convertedBalance; cashPositions[account.currency].investment += convertedBalance;
cashPositions[account.currency].value += convertedBalance; cashPositions[account.currency].valueInBaseCurrency += convertedBalance;
} else { } else {
cashPositions[account.currency] = this.getInitialCashPosition({ cashPositions[account.currency] = this.getInitialCashPosition({
balance: convertedBalance, balance: convertedBalance,
@ -1290,7 +1290,9 @@ export class PortfolioService {
for (const symbol of Object.keys(cashPositions)) { for (const symbol of Object.keys(cashPositions)) {
// Calculate allocations for each currency // Calculate allocations for each currency
cashPositions[symbol].allocationInPercentage = value.gt(0) cashPositions[symbol].allocationInPercentage = value.gt(0)
? new Big(cashPositions[symbol].value).div(value).toNumber() ? new Big(cashPositions[symbol].valueInBaseCurrency)
.div(value)
.toNumber()
: 0; : 0;
} }
@ -1475,7 +1477,7 @@ export class PortfolioService {
sectors: [], sectors: [],
symbol: currency, symbol: currency,
transactionCount: 0, transactionCount: 0,
value: balance valueInBaseCurrency: balance
}; };
} }

42
apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts

@ -70,7 +70,7 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
| 'currency' | 'currency'
| 'exchange' | 'exchange'
| 'name' | 'name'
| 'value' | 'valueInBaseCurrency'
> & { etfProvider: string }; > & { etfProvider: string };
}; };
public sectors: { public sectors: {
@ -292,11 +292,11 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
if (this.hasImpersonationId) { if (this.hasImpersonationId) {
value = position.allocationInPercentage; value = position.allocationInPercentage;
} else { } else {
value = position.value; value = position.valueInBaseCurrency;
} }
this.positions[symbol] = { this.positions[symbol] = {
value, valueInBaseCurrency: value,
assetClass: position.assetClass, assetClass: position.assetClass,
assetSubClass: position.assetSubClass, assetSubClass: position.assetSubClass,
currency: position.currency, currency: position.currency,
@ -323,39 +323,45 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
} }
this.markets.developedMarkets.value += this.markets.developedMarkets.value +=
position.markets.developedMarkets * position.value; position.markets.developedMarkets * position.valueInBaseCurrency;
this.markets.emergingMarkets.value += this.markets.emergingMarkets.value +=
position.markets.emergingMarkets * position.value; position.markets.emergingMarkets * position.valueInBaseCurrency;
this.markets.otherMarkets.value += this.markets.otherMarkets.value +=
position.markets.otherMarkets * position.value; position.markets.otherMarkets * position.valueInBaseCurrency;
for (const country of position.countries) { for (const country of position.countries) {
const { code, continent, name, weight } = country; const { code, continent, name, weight } = country;
if (this.continents[continent]?.value) { if (this.continents[continent]?.value) {
this.continents[continent].value += weight * position.value; this.continents[continent].value +=
weight * position.valueInBaseCurrency;
} else { } else {
this.continents[continent] = { this.continents[continent] = {
name: continent, name: continent,
value: weight * this.portfolioDetails.holdings[symbol].value value:
weight *
this.portfolioDetails.holdings[symbol].valueInBaseCurrency
}; };
} }
if (this.countries[code]?.value) { if (this.countries[code]?.value) {
this.countries[code].value += weight * position.value; this.countries[code].value +=
weight * position.valueInBaseCurrency;
} else { } else {
this.countries[code] = { this.countries[code] = {
name, name,
value: weight * this.portfolioDetails.holdings[symbol].value value:
weight *
this.portfolioDetails.holdings[symbol].valueInBaseCurrency
}; };
} }
} }
} else { } else {
this.continents[UNKNOWN_KEY].value += this.continents[UNKNOWN_KEY].value +=
this.portfolioDetails.holdings[symbol].value; this.portfolioDetails.holdings[symbol].valueInBaseCurrency;
this.countries[UNKNOWN_KEY].value += this.countries[UNKNOWN_KEY].value +=
this.portfolioDetails.holdings[symbol].value; this.portfolioDetails.holdings[symbol].valueInBaseCurrency;
} }
if (position.sectors.length > 0) { if (position.sectors.length > 0) {
@ -363,17 +369,19 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
const { name, weight } = sector; const { name, weight } = sector;
if (this.sectors[name]?.value) { if (this.sectors[name]?.value) {
this.sectors[name].value += weight * position.value; this.sectors[name].value += weight * position.valueInBaseCurrency;
} else { } else {
this.sectors[name] = { this.sectors[name] = {
name, name,
value: weight * this.portfolioDetails.holdings[symbol].value value:
weight *
this.portfolioDetails.holdings[symbol].valueInBaseCurrency
}; };
} }
} }
} else { } else {
this.sectors[UNKNOWN_KEY].value += this.sectors[UNKNOWN_KEY].value +=
this.portfolioDetails.holdings[symbol].value; this.portfolioDetails.holdings[symbol].valueInBaseCurrency;
} }
} }
@ -381,8 +389,8 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
dataSource: position.dataSource, dataSource: position.dataSource,
name: position.name, name: position.name,
symbol: prettifySymbol(symbol), symbol: prettifySymbol(symbol),
value: isNumber(position.value) value: isNumber(position.valueInBaseCurrency)
? position.value ? position.valueInBaseCurrency
: position.valueInPercentage : position.valueInPercentage
}; };
} }

48
apps/client/src/app/pages/public/public-page.component.ts

@ -33,11 +33,18 @@ export class PublicPageComponent implements OnInit {
}; };
public portfolioPublicDetails: PortfolioPublicDetails; public portfolioPublicDetails: PortfolioPublicDetails;
public positions: { public positions: {
[symbol: string]: Pick<PortfolioPosition, 'currency' | 'name' | 'value'>; [symbol: string]: Pick<
PortfolioPosition,
'currency' | 'name' | 'valueInBaseCurrency'
>;
}; };
public positionsArray: Pick< public positionsArray: Pick<
PortfolioPosition, PortfolioPosition,
'currency' | 'name' | 'netPerformancePercent' | 'symbol' | 'value' | 'currency'
| 'name'
| 'netPerformancePercent'
| 'symbol'
| 'valueInBaseCurrency'
>[]; >[];
public sectors: { public sectors: {
[name: string]: { name: string; value: number }; [name: string]: { name: string; value: number };
@ -135,7 +142,7 @@ export class PublicPageComponent implements OnInit {
const value = position.allocationInPercentage; const value = position.allocationInPercentage;
this.positions[symbol] = { this.positions[symbol] = {
value, valueInBaseCurrency: value,
currency: position.currency, currency: position.currency,
name: position.name name: position.name
}; };
@ -143,39 +150,44 @@ export class PublicPageComponent implements OnInit {
if (position.countries.length > 0) { if (position.countries.length > 0) {
this.markets.developedMarkets.value += this.markets.developedMarkets.value +=
position.markets.developedMarkets * position.value; position.markets.developedMarkets * position.valueInBaseCurrency;
this.markets.emergingMarkets.value += this.markets.emergingMarkets.value +=
position.markets.emergingMarkets * position.value; position.markets.emergingMarkets * position.valueInBaseCurrency;
this.markets.otherMarkets.value += this.markets.otherMarkets.value +=
position.markets.otherMarkets * position.value; position.markets.otherMarkets * position.valueInBaseCurrency;
for (const country of position.countries) { for (const country of position.countries) {
const { code, continent, name, weight } = country; const { code, continent, name, weight } = country;
if (this.continents[continent]?.value) { if (this.continents[continent]?.value) {
this.continents[continent].value += weight * position.value; this.continents[continent].value +=
weight * position.valueInBaseCurrency;
} else { } else {
this.continents[continent] = { this.continents[continent] = {
name: continent, name: continent,
value: weight * this.portfolioPublicDetails.holdings[symbol].value value:
weight *
this.portfolioPublicDetails.holdings[symbol].valueInBaseCurrency
}; };
} }
if (this.countries[code]?.value) { if (this.countries[code]?.value) {
this.countries[code].value += weight * position.value; this.countries[code].value += weight * position.valueInBaseCurrency;
} else { } else {
this.countries[code] = { this.countries[code] = {
name, name,
value: weight * this.portfolioPublicDetails.holdings[symbol].value value:
weight *
this.portfolioPublicDetails.holdings[symbol].valueInBaseCurrency
}; };
} }
} }
} else { } else {
this.continents[UNKNOWN_KEY].value += this.continents[UNKNOWN_KEY].value +=
this.portfolioPublicDetails.holdings[symbol].value; this.portfolioPublicDetails.holdings[symbol].valueInBaseCurrency;
this.countries[UNKNOWN_KEY].value += this.countries[UNKNOWN_KEY].value +=
this.portfolioPublicDetails.holdings[symbol].value; this.portfolioPublicDetails.holdings[symbol].valueInBaseCurrency;
} }
if (position.sectors.length > 0) { if (position.sectors.length > 0) {
@ -183,24 +195,26 @@ export class PublicPageComponent implements OnInit {
const { name, weight } = sector; const { name, weight } = sector;
if (this.sectors[name]?.value) { if (this.sectors[name]?.value) {
this.sectors[name].value += weight * position.value; this.sectors[name].value += weight * position.valueInBaseCurrency;
} else { } else {
this.sectors[name] = { this.sectors[name] = {
name, name,
value: weight * this.portfolioPublicDetails.holdings[symbol].value value:
weight *
this.portfolioPublicDetails.holdings[symbol].valueInBaseCurrency
}; };
} }
} }
} else { } else {
this.sectors[UNKNOWN_KEY].value += this.sectors[UNKNOWN_KEY].value +=
this.portfolioPublicDetails.holdings[symbol].value; this.portfolioPublicDetails.holdings[symbol].valueInBaseCurrency;
} }
this.symbols[prettifySymbol(symbol)] = { this.symbols[prettifySymbol(symbol)] = {
name: position.name, name: position.name,
symbol: prettifySymbol(symbol), symbol: prettifySymbol(symbol),
value: isNumber(position.value) value: isNumber(position.valueInBaseCurrency)
? position.value ? position.valueInBaseCurrency
: position.valueInPercentage : position.valueInPercentage
}; };
} }

6
apps/client/src/app/services/data.service.ts

@ -415,10 +415,10 @@ export class DataService {
map((response) => { map((response) => {
if (response.holdings) { if (response.holdings) {
for (const symbol of Object.keys(response.holdings)) { for (const symbol of Object.keys(response.holdings)) {
response.holdings[symbol].value = isNumber( response.holdings[symbol].valueInBaseCurrency = isNumber(
response.holdings[symbol].value response.holdings[symbol].valueInBaseCurrency
) )
? response.holdings[symbol].value ? response.holdings[symbol].valueInBaseCurrency
: response.holdings[symbol].valueInPercentage; : response.holdings[symbol].valueInPercentage;
} }
} }

2
libs/common/src/lib/interfaces/portfolio-position.interface.ts

@ -30,6 +30,6 @@ export interface PortfolioPosition {
symbol: string; symbol: string;
type?: string; type?: string;
url?: string; url?: string;
value?: number; valueInBaseCurrency?: number;
valueInPercentage?: number; valueInPercentage?: number;
} }

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

@ -17,7 +17,7 @@ export interface PortfolioPublicDetails {
| 'sectors' | 'sectors'
| 'symbol' | 'symbol'
| 'url' | 'url'
| 'value' | 'valueInBaseCurrency'
| 'valueInPercentage' | 'valueInPercentage'
>; >;
}; };

Loading…
Cancel
Save