Browse Source

Merge pull request #109 from dandevaud/bugfix/fix-some-bugs

Readd time weighted performance
pull/5027/head
dandevaud 10 months ago
committed by GitHub
parent
commit
3b0c022a2d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  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
public async getChart({
dateRange = 'max',
withDataDecimation = true,
withTimeWeightedReturn = false
public async getPerformanceWithTimeWeightedReturn({
start,
end
}: {
dateRange?: DateRange;
withDataDecimation?: boolean;
withTimeWeightedReturn?: boolean;
}): 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;
start: Date;
end: Date;
}): Promise<{ chart: HistoricalDataItem[] }> {
let item = await super.getPerformance({
end: endDate,
start: startDate
end,
start
});
if (!withTimeWeightedReturn) {
return item.chart;
} else {
let itemResult = item.chart;
let dates = itemResult.map((item) => parseDate(item.date));
let timeWeighted = await this.getTimeWeightedChartData({
dates
});
let itemResult = item.chart;
let dates = itemResult.map((item) => parseDate(item.date));
let timeWeighted = await this.getTimeWeightedChartData({
dates
});
return itemResult.map((item) => {
let timeWeightedItem = timeWeighted.find(
(timeWeightedItem) => timeWeightedItem.date === item.date
);
if (timeWeightedItem) {
item.timeWeightedPerformance =
timeWeightedItem.netPerformanceInPercentage;
item.timeWeightedPerformanceWithCurrencyEffect =
timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect;
}
item.chart = itemResult.map((itemInt) => {
let timeWeightedItem = timeWeighted.find(
(timeWeightedItem) => timeWeightedItem.date === itemInt.date
);
if (timeWeightedItem) {
itemInt.timeWeightedPerformance =
timeWeightedItem.netPerformanceInPercentage;
itemInt.timeWeightedPerformanceWithCurrencyEffect =
timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect;
}
return item;
});
}
return itemInt;
});
return item;
}
@LogPerformance
@ -314,8 +293,14 @@ export class CPRPortfolioCalculator extends TWRPortfolioCalculator {
netPerformanceInPercentageWithCurrencyEffect: Big,
newTotalInvestment: Big
) {
const previousPrice = this.marketMap[previousDate][holding];
const currentPrice = this.marketMap[date][holding] ?? previousPrice;
const 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 priceInBaseCurrency = currentPrice

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

@ -674,6 +674,7 @@ export abstract class PortfolioCalculator {
return this.snapshot.totalLiabilitiesWithCurrencyEffect;
}
@LogPerformance
public async getPerformance({ end, start }) {
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 range = { end: endDate, start: startDate };
const { chart } = await portfolioCalculator.getPerformance({
end: endDate,
start: startDate
});
const { chart } = await (calculateTimeWeightedPerformance
? (
portfolioCalculator as CPRPortfolioCalculator
).getPerformanceWithTimeWeightedReturn(range)
: portfolioCalculator.getPerformance(range));
const {
netPerformance,

Loading…
Cancel
Save