Browse Source

Refactoring

pull/2948/head
Thomas Kaul 2 years ago
parent
commit
90c4779ff3
  1. 4
      apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts
  2. 19
      apps/api/src/app/portfolio/portfolio-calculator.ts
  3. 1
      libs/common/src/lib/interfaces/historical-data-item.interface.ts

4
apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts

@ -85,7 +85,7 @@ describe('PortfolioCalculator', () => {
expect(chartData[0]).toEqual({ expect(chartData[0]).toEqual({
date: '2022-03-07', date: '2022-03-07',
investment: 151.6, investmentValueWithCurrencyEffect: 151.6,
netPerformance: 0, netPerformance: 0,
netPerformanceInPercentage: 0, netPerformanceInPercentage: 0,
netPerformanceInPercentageWithCurrencyEffect: 0, netPerformanceInPercentageWithCurrencyEffect: 0,
@ -98,7 +98,7 @@ describe('PortfolioCalculator', () => {
expect(chartData[chartData.length - 1]).toEqual({ expect(chartData[chartData.length - 1]).toEqual({
date: '2022-04-11', date: '2022-04-11',
investment: 0, investmentValueWithCurrencyEffect: 0,
netPerformance: 19.86, netPerformance: 19.86,
netPerformanceInPercentage: 13.100263852242744, netPerformanceInPercentage: 13.100263852242744,
netPerformanceInPercentageWithCurrencyEffect: 13.100263852242744, netPerformanceInPercentageWithCurrencyEffect: 13.100263852242744,

19
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -3,6 +3,7 @@ import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfac
import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper'; import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper';
import { import {
DataProviderInfo, DataProviderInfo,
HistoricalDataItem,
ResponseError, ResponseError,
SymbolMetrics, SymbolMetrics,
TimelinePosition TimelinePosition
@ -173,7 +174,11 @@ export class PortfolioCalculator {
this.transactionPoints = transactionPoints; this.transactionPoints = transactionPoints;
} }
public async getChartData(start: Date, end = new Date(Date.now()), step = 1) { public async getChartData(
start: Date,
end = new Date(Date.now()),
step = 1
): Promise<HistoricalDataItem[]> {
const symbols: { [symbol: string]: boolean } = {}; const symbols: { [symbol: string]: boolean } = {};
const transactionPointsBeforeEndDate = const transactionPointsBeforeEndDate =
@ -242,7 +247,7 @@ export class PortfolioCalculator {
const accumulatedValuesByDate: { const accumulatedValuesByDate: {
[date: string]: { [date: string]: {
investmentValue: Big; investmentValueWithCurrencyEffect: Big;
totalCurrentValue: Big; totalCurrentValue: Big;
totalCurrentValueWithCurrencyEffect: Big; totalCurrentValueWithCurrencyEffect: Big;
totalInvestmentValue: Big; totalInvestmentValue: Big;
@ -344,8 +349,9 @@ export class PortfolioCalculator {
] ?? new Big(0); ] ?? new Big(0);
accumulatedValuesByDate[dateString] = { accumulatedValuesByDate[dateString] = {
investmentValue: ( investmentValueWithCurrencyEffect: (
accumulatedValuesByDate[dateString]?.investmentValue ?? new Big(0) accumulatedValuesByDate[dateString]
?.investmentValueWithCurrencyEffect ?? new Big(0)
).add(investmentValueWithCurrencyEffect), ).add(investmentValueWithCurrencyEffect),
totalCurrentValue: ( totalCurrentValue: (
accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0) accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0)
@ -384,7 +390,7 @@ export class PortfolioCalculator {
return Object.entries(accumulatedValuesByDate).map(([date, values]) => { return Object.entries(accumulatedValuesByDate).map(([date, values]) => {
const { const {
investmentValue, investmentValueWithCurrencyEffect,
totalCurrentValue, totalCurrentValue,
totalCurrentValueWithCurrencyEffect, totalCurrentValueWithCurrencyEffect,
totalInvestmentValue, totalInvestmentValue,
@ -414,7 +420,8 @@ export class PortfolioCalculator {
date, date,
netPerformanceInPercentage, netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect, netPerformanceInPercentageWithCurrencyEffect,
investment: investmentValue.toNumber(), investmentValueWithCurrencyEffect:
investmentValueWithCurrencyEffect.toNumber(),
netPerformance: totalNetPerformanceValue.toNumber(), netPerformance: totalNetPerformanceValue.toNumber(),
netPerformanceWithCurrencyEffect: netPerformanceWithCurrencyEffect:
totalNetPerformanceValueWithCurrencyEffect.toNumber(), totalNetPerformanceValueWithCurrencyEffect.toNumber(),

1
libs/common/src/lib/interfaces/historical-data-item.interface.ts

@ -2,6 +2,7 @@ export interface HistoricalDataItem {
averagePrice?: number; averagePrice?: number;
date: string; date: string;
grossPerformancePercent?: number; grossPerformancePercent?: number;
investmentValueWithCurrencyEffect?: number;
marketPrice?: number; marketPrice?: number;
netPerformance?: number; netPerformance?: number;
netPerformanceInPercentage?: number; netPerformanceInPercentage?: number;

Loading…
Cancel
Save