Bugfix/fix total amount calculation in portfolio evolution chart (#1799)
* Fix total amount calculation * Update changelog
This commit is contained in:
parent
bd1963ec26
commit
bc6e9a8b68
@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- Updated the URL of the Ghostfolio Slack channel
|
- Updated the URL of the Ghostfolio Slack channel
|
||||||
- Upgraded `prisma` from version `4.10.1` to `4.11.0`
|
- Upgraded `prisma` from version `4.10.1` to `4.11.0`
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Fixed the total amount calculation in the portfolio evolution chart
|
||||||
|
|
||||||
## 1.246.0 - 2023-03-18
|
## 1.246.0 - 2023-03-18
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@ -86,7 +86,7 @@ describe('PortfolioCalculator', () => {
|
|||||||
netPerformanceInPercentage: 13.100263852242744,
|
netPerformanceInPercentage: 13.100263852242744,
|
||||||
netPerformance: 19.86,
|
netPerformance: 19.86,
|
||||||
totalInvestment: 0,
|
totalInvestment: 0,
|
||||||
value: 19.86
|
value: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(currentPositions).toEqual({
|
expect(currentPositions).toEqual({
|
||||||
|
@ -235,7 +235,7 @@ export class PortfolioCalculator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const netPerformanceValuesBySymbol: {
|
const currentValuesBySymbol: {
|
||||||
[symbol: string]: { [date: string]: Big };
|
[symbol: string]: { [date: string]: Big };
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
@ -247,21 +247,31 @@ export class PortfolioCalculator {
|
|||||||
[symbol: string]: { [date: string]: Big };
|
[symbol: string]: { [date: string]: Big };
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
|
const netPerformanceValuesBySymbol: {
|
||||||
|
[symbol: string]: { [date: string]: Big };
|
||||||
|
} = {};
|
||||||
|
|
||||||
|
const totalCurrentValues: { [date: string]: Big } = {};
|
||||||
const totalNetPerformanceValues: { [date: string]: Big } = {};
|
const totalNetPerformanceValues: { [date: string]: Big } = {};
|
||||||
const totalInvestmentValues: { [date: string]: Big } = {};
|
const totalInvestmentValues: { [date: string]: Big } = {};
|
||||||
const maxTotalInvestmentValues: { [date: string]: Big } = {};
|
const maxTotalInvestmentValues: { [date: string]: Big } = {};
|
||||||
|
|
||||||
for (const symbol of Object.keys(symbols)) {
|
for (const symbol of Object.keys(symbols)) {
|
||||||
const { investmentValues, maxInvestmentValues, netPerformanceValues } =
|
const {
|
||||||
this.getSymbolMetrics({
|
currentValues,
|
||||||
end,
|
investmentValues,
|
||||||
marketSymbolMap,
|
maxInvestmentValues,
|
||||||
start,
|
netPerformanceValues
|
||||||
step,
|
} = this.getSymbolMetrics({
|
||||||
symbol,
|
end,
|
||||||
isChartMode: true
|
marketSymbolMap,
|
||||||
});
|
start,
|
||||||
|
step,
|
||||||
|
symbol,
|
||||||
|
isChartMode: true
|
||||||
|
});
|
||||||
|
|
||||||
|
currentValuesBySymbol[symbol] = currentValues;
|
||||||
netPerformanceValuesBySymbol[symbol] = netPerformanceValues;
|
netPerformanceValuesBySymbol[symbol] = netPerformanceValues;
|
||||||
investmentValuesBySymbol[symbol] = investmentValues;
|
investmentValuesBySymbol[symbol] = investmentValues;
|
||||||
maxInvestmentValuesBySymbol[symbol] = maxInvestmentValues;
|
maxInvestmentValuesBySymbol[symbol] = maxInvestmentValues;
|
||||||
@ -271,6 +281,15 @@ export class PortfolioCalculator {
|
|||||||
const dateString = format(currentDate, DATE_FORMAT);
|
const dateString = format(currentDate, DATE_FORMAT);
|
||||||
|
|
||||||
for (const symbol of Object.keys(netPerformanceValuesBySymbol)) {
|
for (const symbol of Object.keys(netPerformanceValuesBySymbol)) {
|
||||||
|
totalCurrentValues[dateString] =
|
||||||
|
totalCurrentValues[dateString] ?? new Big(0);
|
||||||
|
|
||||||
|
if (currentValuesBySymbol[symbol]?.[dateString]) {
|
||||||
|
totalCurrentValues[dateString] = totalCurrentValues[dateString].add(
|
||||||
|
currentValuesBySymbol[symbol][dateString]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
totalNetPerformanceValues[dateString] =
|
totalNetPerformanceValues[dateString] =
|
||||||
totalNetPerformanceValues[dateString] ?? new Big(0);
|
totalNetPerformanceValues[dateString] ?? new Big(0);
|
||||||
|
|
||||||
@ -283,15 +302,15 @@ export class PortfolioCalculator {
|
|||||||
totalInvestmentValues[dateString] =
|
totalInvestmentValues[dateString] =
|
||||||
totalInvestmentValues[dateString] ?? new Big(0);
|
totalInvestmentValues[dateString] ?? new Big(0);
|
||||||
|
|
||||||
maxTotalInvestmentValues[dateString] =
|
|
||||||
maxTotalInvestmentValues[dateString] ?? new Big(0);
|
|
||||||
|
|
||||||
if (investmentValuesBySymbol[symbol]?.[dateString]) {
|
if (investmentValuesBySymbol[symbol]?.[dateString]) {
|
||||||
totalInvestmentValues[dateString] = totalInvestmentValues[
|
totalInvestmentValues[dateString] = totalInvestmentValues[
|
||||||
dateString
|
dateString
|
||||||
].add(investmentValuesBySymbol[symbol][dateString]);
|
].add(investmentValuesBySymbol[symbol][dateString]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maxTotalInvestmentValues[dateString] =
|
||||||
|
maxTotalInvestmentValues[dateString] ?? new Big(0);
|
||||||
|
|
||||||
if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) {
|
if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) {
|
||||||
maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[
|
maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[
|
||||||
dateString
|
dateString
|
||||||
@ -313,9 +332,7 @@ export class PortfolioCalculator {
|
|||||||
netPerformanceInPercentage,
|
netPerformanceInPercentage,
|
||||||
netPerformance: totalNetPerformanceValues[date].toNumber(),
|
netPerformance: totalNetPerformanceValues[date].toNumber(),
|
||||||
totalInvestment: totalInvestmentValues[date].toNumber(),
|
totalInvestment: totalInvestmentValues[date].toNumber(),
|
||||||
value: totalInvestmentValues[date]
|
value: totalCurrentValues[date].toNumber()
|
||||||
.plus(totalNetPerformanceValues[date])
|
|
||||||
.toNumber()
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -906,12 +923,16 @@ export class PortfolioCalculator {
|
|||||||
|
|
||||||
if (orders.length <= 0) {
|
if (orders.length <= 0) {
|
||||||
return {
|
return {
|
||||||
|
currentValues: {},
|
||||||
|
grossPerformance: new Big(0),
|
||||||
|
grossPerformancePercentage: new Big(0),
|
||||||
hasErrors: false,
|
hasErrors: false,
|
||||||
initialValue: new Big(0),
|
initialValue: new Big(0),
|
||||||
|
investmentValues: {},
|
||||||
|
maxInvestmentValues: {},
|
||||||
netPerformance: new Big(0),
|
netPerformance: new Big(0),
|
||||||
netPerformancePercentage: new Big(0),
|
netPerformancePercentage: new Big(0),
|
||||||
grossPerformance: new Big(0),
|
netPerformanceValues: {}
|
||||||
grossPerformancePercentage: new Big(0)
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -946,6 +967,7 @@ export class PortfolioCalculator {
|
|||||||
let grossPerformanceFromSells = new Big(0);
|
let grossPerformanceFromSells = new Big(0);
|
||||||
let initialValue: Big;
|
let initialValue: Big;
|
||||||
let investmentAtStartDate: Big;
|
let investmentAtStartDate: Big;
|
||||||
|
const currentValues: { [date: string]: Big } = {};
|
||||||
const investmentValues: { [date: string]: Big } = {};
|
const investmentValues: { [date: string]: Big } = {};
|
||||||
const maxInvestmentValues: { [date: string]: Big } = {};
|
const maxInvestmentValues: { [date: string]: Big } = {};
|
||||||
let lastAveragePrice = new Big(0);
|
let lastAveragePrice = new Big(0);
|
||||||
@ -1164,6 +1186,7 @@ export class PortfolioCalculator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isChartMode && i > indexOfStartOrder) {
|
if (isChartMode && i > indexOfStartOrder) {
|
||||||
|
currentValues[order.date] = valueOfInvestment;
|
||||||
netPerformanceValues[order.date] = grossPerformance
|
netPerformanceValues[order.date] = grossPerformance
|
||||||
.minus(grossPerformanceAtStartDate)
|
.minus(grossPerformanceAtStartDate)
|
||||||
.minus(fees.minus(feesAtStartDate));
|
.minus(fees.minus(feesAtStartDate));
|
||||||
@ -1261,15 +1284,16 @@ export class PortfolioCalculator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
initialValue,
|
currentValues,
|
||||||
grossPerformancePercentage,
|
grossPerformancePercentage,
|
||||||
|
initialValue,
|
||||||
investmentValues,
|
investmentValues,
|
||||||
maxInvestmentValues,
|
maxInvestmentValues,
|
||||||
netPerformancePercentage,
|
netPerformancePercentage,
|
||||||
netPerformanceValues,
|
netPerformanceValues,
|
||||||
|
grossPerformance: totalGrossPerformance,
|
||||||
hasErrors: totalUnits.gt(0) && (!initialValue || !unitPriceAtEndDate),
|
hasErrors: totalUnits.gt(0) && (!initialValue || !unitPriceAtEndDate),
|
||||||
netPerformance: totalNetPerformance,
|
netPerformance: totalNetPerformance
|
||||||
grossPerformance: totalGrossPerformance
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user