Merge branch 'feature/integrate-chart-to-snapshot-calculation' of https://github.com/gizmodus/ghostfolio into pr/3271

pull/3383/head
Thomas Kaul 4 weeks ago
commit 10ed5d07a0

@ -91,6 +91,7 @@ export abstract class PortfolioCalculator {
userId: string;
}) {
console.time('--- PortfolioCalculator.constructor - 1');
this.accountBalanceItems = accountBalanceItems;
this.configurationService = configurationService;
this.currency = currency;
@ -273,7 +274,8 @@ export abstract class PortfolioCalculator {
} = await this.currentRateService.getValues({
dataGatheringItems,
dateQuery: {
in: dates
gte: parseDate(firstTransactionPoint?.date),
lt: end
}
});
@ -302,7 +304,7 @@ export abstract class PortfolioCalculator {
const chartStartDate = this.getStartDate();
const daysInMarket = differenceInDays(endDate, chartStartDate) + 1;
const step = true /*withDataDecimation*/
const step = false /*withDataDecimation*/
? Math.round(daysInMarket / Math.min(daysInMarket, MAX_CHART_ITEMS))
: 1;
@ -581,11 +583,6 @@ export abstract class PortfolioCalculator {
totalTimeWeightedInvestmentValueWithCurrencyEffect
} = values;
console.log(
'Chart: totalTimeWeightedInvestmentValue',
totalTimeWeightedInvestmentValue.toFixed()
);
const netPerformanceInPercentage = totalTimeWeightedInvestmentValue.eq(0)
? 0
: totalNetPerformanceValue
@ -712,7 +709,8 @@ export abstract class PortfolioCalculator {
await this.currentRateService.getValues({
dataGatheringItems,
dateQuery: {
in: dates
gte: start,
lt: end
}
});

@ -337,18 +337,23 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
let lastUnitPrice: Big;
if (isChartMode) {
const datesWithOrders = {};
const ordersByDate: { [date: string]: PortfolioOrderItem[] } = {};
for (const { date, type } of orders) {
if (['BUY', 'SELL'].includes(type)) {
datesWithOrders[date] = true;
for (const order of orders) {
if (['BUY', 'SELL'].includes(order.type)) {
ordersByDate[order.date] = ordersByDate[order.date] ?? [];
ordersByDate[order.date].push(order);
}
}
while (isBefore(day, end)) {
const hasDate = datesWithOrders[format(day, DATE_FORMAT)];
if (!hasDate) {
if (ordersByDate[format(day, DATE_FORMAT)]?.length > 0) {
for (let order of ordersByDate[format(day, DATE_FORMAT)]) {
order.unitPriceFromMarketData =
marketSymbolMap[format(day, DATE_FORMAT)]?.[symbol] ??
lastUnitPrice;
}
} else {
orders.push({
date: format(day, DATE_FORMAT),
fee: new Big(0),
@ -360,12 +365,18 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
},
type: 'BUY',
unitPrice:
marketSymbolMap[format(day, DATE_FORMAT)]?.[symbol] ??
lastUnitPrice,
unitPriceFromMarketData:
marketSymbolMap[format(day, DATE_FORMAT)]?.[symbol] ??
lastUnitPrice
});
}
lastUnitPrice = last(orders).unitPrice;
const lastOrder = last(orders);
lastUnitPrice =
lastOrder.unitPriceFromMarketData ?? lastOrder.unitPrice;
day = addDays(day, step);
}
@ -459,12 +470,14 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
);
}
if (order.unitPrice) {
order.unitPriceInBaseCurrency = order.unitPrice.mul(
currentExchangeRate ?? 1
);
const unitPrice = ['BUY', 'SELL'].includes(order.type)
? order.unitPrice
: order.unitPriceFromMarketData;
if (unitPrice) {
order.unitPriceInBaseCurrency = unitPrice.mul(currentExchangeRate ?? 1);
order.unitPriceInBaseCurrencyWithCurrencyEffect = order.unitPrice.mul(
order.unitPriceInBaseCurrencyWithCurrencyEffect = unitPrice.mul(
exchangeRateAtOrderDate ?? 1
);
}
@ -648,10 +661,13 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
grossPerformanceWithCurrencyEffect;
}
if (i > indexOfStartOrder && ['BUY', 'SELL'].includes(order.type)) {
if (i > indexOfStartOrder) {
// Only consider periods with an investment for the calculation of
// the time weighted investment
if (valueOfInvestmentBeforeTransaction.gt(0)) {
if (
valueOfInvestmentBeforeTransaction.gt(0) &&
['BUY', 'SELL'].includes(order.type)
) {
// Calculate the number of days since the previous order
const orderDate = new Date(order.date);
const previousOrderDate = new Date(orders[i - 1].date);

@ -6,6 +6,7 @@ export interface PortfolioOrderItem extends PortfolioOrder {
feeInBaseCurrency?: Big;
feeInBaseCurrencyWithCurrencyEffect?: Big;
itemType?: 'end' | 'start';
unitPriceFromMarketData?: Big;
unitPriceInBaseCurrency?: Big;
unitPriceInBaseCurrencyWithCurrencyEffect?: Big;
}

Loading…
Cancel
Save