Browse Source

Add end date parameter (#1224)

* Add end date parameter
pull/1227/head
Thomas Kaul 2 years ago
committed by GitHub
parent
commit
0cc42ffd7c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      apps/api/src/app/portfolio/portfolio-calculator.ts

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

@ -16,6 +16,7 @@ import {
isBefore, isBefore,
isSameMonth, isSameMonth,
isSameYear, isSameYear,
isWithinInterval,
max, max,
min, min,
set set
@ -167,13 +168,24 @@ export class PortfolioCalculator {
this.transactionPoints = transactionPoints; this.transactionPoints = transactionPoints;
} }
public async getCurrentPositions(start: Date): Promise<CurrentPositions> { public async getCurrentPositions(
if (!this.transactionPoints?.length) { start: Date,
end = new Date(Date.now())
): Promise<CurrentPositions> {
const transactionPointsInRange =
this.transactionPoints?.filter((transactionPoint) => {
return isWithinInterval(parseDate(transactionPoint.date), {
start,
end
});
}) ?? [];
if (!transactionPointsInRange.length) {
return { return {
currentValue: new Big(0), currentValue: new Big(0),
hasErrors: false,
grossPerformance: new Big(0), grossPerformance: new Big(0),
grossPerformancePercentage: new Big(0), grossPerformancePercentage: new Big(0),
hasErrors: false,
netPerformance: new Big(0), netPerformance: new Big(0),
netPerformancePercentage: new Big(0), netPerformancePercentage: new Big(0),
positions: [], positions: [],
@ -182,39 +194,36 @@ export class PortfolioCalculator {
} }
const lastTransactionPoint = const lastTransactionPoint =
this.transactionPoints[this.transactionPoints.length - 1]; transactionPointsInRange[transactionPointsInRange.length - 1];
// use Date.now() to use the mock for today
const today = new Date(Date.now());
let firstTransactionPoint: TransactionPoint = null; let firstTransactionPoint: TransactionPoint = null;
let firstIndex = this.transactionPoints.length; let firstIndex = transactionPointsInRange.length;
const dates = []; const dates = [];
const dataGatheringItems: IDataGatheringItem[] = []; const dataGatheringItems: IDataGatheringItem[] = [];
const currencies: { [symbol: string]: string } = {}; const currencies: { [symbol: string]: string } = {};
dates.push(resetHours(start)); dates.push(resetHours(start));
for (const item of this.transactionPoints[firstIndex - 1].items) { for (const item of transactionPointsInRange[firstIndex - 1].items) {
dataGatheringItems.push({ dataGatheringItems.push({
dataSource: item.dataSource, dataSource: item.dataSource,
symbol: item.symbol symbol: item.symbol
}); });
currencies[item.symbol] = item.currency; currencies[item.symbol] = item.currency;
} }
for (let i = 0; i < this.transactionPoints.length; i++) { for (let i = 0; i < transactionPointsInRange.length; i++) {
if ( if (
!isBefore(parseDate(this.transactionPoints[i].date), start) && !isBefore(parseDate(transactionPointsInRange[i].date), start) &&
firstTransactionPoint === null firstTransactionPoint === null
) { ) {
firstTransactionPoint = this.transactionPoints[i]; firstTransactionPoint = transactionPointsInRange[i];
firstIndex = i; firstIndex = i;
} }
if (firstTransactionPoint !== null) { if (firstTransactionPoint !== null) {
dates.push(resetHours(parseDate(this.transactionPoints[i].date))); dates.push(resetHours(parseDate(transactionPointsInRange[i].date)));
} }
} }
dates.push(resetHours(today)); dates.push(resetHours(end));
const marketSymbols = await this.currentRateService.getValues({ const marketSymbols = await this.currentRateService.getValues({
currencies, currencies,
@ -241,7 +250,7 @@ export class PortfolioCalculator {
} }
} }
const todayString = format(today, DATE_FORMAT); const endDateString = format(end, DATE_FORMAT);
if (firstIndex > 0) { if (firstIndex > 0) {
firstIndex--; firstIndex--;
@ -254,7 +263,7 @@ export class PortfolioCalculator {
const errors: ResponseError['errors'] = []; const errors: ResponseError['errors'] = [];
for (const item of lastTransactionPoint.items) { for (const item of lastTransactionPoint.items) {
const marketValue = marketSymbolMap[todayString]?.[item.symbol]; const marketValue = marketSymbolMap[endDateString]?.[item.symbol];
const { const {
grossPerformance, grossPerformance,
@ -264,6 +273,7 @@ export class PortfolioCalculator {
netPerformance, netPerformance,
netPerformancePercentage netPerformancePercentage
} = this.getSymbolMetrics({ } = this.getSymbolMetrics({
end,
marketSymbolMap, marketSymbolMap,
start, start,
symbol: item.symbol symbol: item.symbol
@ -700,10 +710,12 @@ export class PortfolioCalculator {
} }
private getSymbolMetrics({ private getSymbolMetrics({
end,
marketSymbolMap, marketSymbolMap,
start, start,
symbol symbol
}: { }: {
end: Date;
marketSymbolMap: { marketSymbolMap: {
[date: string]: { [symbol: string]: Big }; [date: string]: { [symbol: string]: Big };
}; };
@ -726,13 +738,12 @@ export class PortfolioCalculator {
} }
const dateOfFirstTransaction = new Date(first(orders).date); const dateOfFirstTransaction = new Date(first(orders).date);
const endDate = new Date(Date.now());
const unitPriceAtStartDate = const unitPriceAtStartDate =
marketSymbolMap[format(start, DATE_FORMAT)]?.[symbol]; marketSymbolMap[format(start, DATE_FORMAT)]?.[symbol];
const unitPriceAtEndDate = const unitPriceAtEndDate =
marketSymbolMap[format(endDate, DATE_FORMAT)]?.[symbol]; marketSymbolMap[format(end, DATE_FORMAT)]?.[symbol];
if ( if (
!unitPriceAtEndDate || !unitPriceAtEndDate ||
@ -785,7 +796,7 @@ export class PortfolioCalculator {
orders.push({ orders.push({
symbol, symbol,
currency: null, currency: null,
date: format(endDate, DATE_FORMAT), date: format(end, DATE_FORMAT),
dataSource: null, dataSource: null,
fee: new Big(0), fee: new Big(0),
itemType: 'end', itemType: 'end',

Loading…
Cancel
Save