Browse Source

Readd time weighted performance

pull/5027/head
Dan 10 months ago
parent
commit
85857aabce
  1. 83
      apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts
  2. 1
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  3. 10
      apps/api/src/app/portfolio/portfolio.service.ts

83
apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts

@ -72,59 +72,38 @@ export class CPRPortfolioCalculator extends TWRPortfolioCalculator {
} }
@LogPerformance @LogPerformance
public async getChart({ public async getPerformanceWithTimeWeightedReturn({
dateRange = 'max', start,
withDataDecimation = true, end
withTimeWeightedReturn = false
}: { }: {
dateRange?: DateRange; start: Date;
withDataDecimation?: boolean; end: Date;
withTimeWeightedReturn?: boolean; }): Promise<{ chart: HistoricalDataItem[] }> {
}): Promise<HistoricalDataItem[]> {
const { endDate, startDate } = getIntervalFromDateRange(
dateRange,
this.getStartDate()
);
const daysInMarket = differenceInDays(endDate, startDate) + 1;
const step = withDataDecimation
? Math.round(
daysInMarket /
Math.min(
daysInMarket,
this.configurationService.get('MAX_CHART_ITEMS')
)
)
: 1;
let item = await super.getPerformance({ let item = await super.getPerformance({
end: endDate, end,
start: startDate start
}); });
if (!withTimeWeightedReturn) { let itemResult = item.chart;
return item.chart; let dates = itemResult.map((item) => parseDate(item.date));
} else { let timeWeighted = await this.getTimeWeightedChartData({
let itemResult = item.chart; dates
let dates = itemResult.map((item) => parseDate(item.date)); });
let timeWeighted = await this.getTimeWeightedChartData({
dates
});
return itemResult.map((item) => { item.chart = itemResult.map((itemInt) => {
let timeWeightedItem = timeWeighted.find( let timeWeightedItem = timeWeighted.find(
(timeWeightedItem) => timeWeightedItem.date === item.date (timeWeightedItem) => timeWeightedItem.date === itemInt.date
); );
if (timeWeightedItem) { if (timeWeightedItem) {
item.timeWeightedPerformance = itemInt.timeWeightedPerformance =
timeWeightedItem.netPerformanceInPercentage; timeWeightedItem.netPerformanceInPercentage;
item.timeWeightedPerformanceWithCurrencyEffect = itemInt.timeWeightedPerformanceWithCurrencyEffect =
timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect; timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect;
} }
return item; return itemInt;
}); });
} return item;
} }
@LogPerformance @LogPerformance
@ -314,8 +293,14 @@ export class CPRPortfolioCalculator extends TWRPortfolioCalculator {
netPerformanceInPercentageWithCurrencyEffect: Big, netPerformanceInPercentageWithCurrencyEffect: Big,
newTotalInvestment: Big newTotalInvestment: Big
) { ) {
const previousPrice = this.marketMap[previousDate][holding]; const previousPrice =
const currentPrice = this.marketMap[date][holding] ?? previousPrice; Object.keys(this.marketMap).indexOf(previousDate) > 0
? this.marketMap[previousDate][holding]
: undefined;
const priceDictionary = this.marketMap[date];
let currentPrice =
priceDictionary !== undefined ? priceDictionary[holding] : previousPrice;
currentPrice ??= previousPrice;
const previousHolding = timelineHoldings[previousDate][holding]; const previousHolding = timelineHoldings[previousDate][holding];
const priceInBaseCurrency = currentPrice const priceInBaseCurrency = currentPrice

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

@ -674,6 +674,7 @@ export abstract class PortfolioCalculator {
return this.snapshot.totalLiabilitiesWithCurrencyEffect; return this.snapshot.totalLiabilitiesWithCurrencyEffect;
} }
@LogPerformance
public async getPerformance({ end, start }) { public async getPerformance({ end, start }) {
await this.snapshotPromise; await this.snapshotPromise;

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

@ -1237,11 +1237,13 @@ export class PortfolioService {
}); });
const { endDate, startDate } = getIntervalFromDateRange(dateRange); const { endDate, startDate } = getIntervalFromDateRange(dateRange);
const range = { end: endDate, start: startDate };
const { chart } = await portfolioCalculator.getPerformance({ const { chart } = await (calculateTimeWeightedPerformance
end: endDate, ? (
start: startDate portfolioCalculator as CPRPortfolioCalculator
}); ).getPerformanceWithTimeWeightedReturn(range)
: portfolioCalculator.getPerformance(range));
const { const {
netPerformance, netPerformance,

Loading…
Cancel
Save