Browse Source

Fix test

pull/1399/head
Thomas 3 years ago
parent
commit
f0d173a03f
  1. 8
      apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts
  2. 31
      apps/api/src/app/portfolio/portfolio-calculator.ts

8
apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts

@ -94,8 +94,8 @@ describe('PortfolioCalculator', () => {
grossPerformance: new Big('19.86'), grossPerformance: new Big('19.86'),
grossPerformancePercentage: new Big('0.13100263852242744063'), grossPerformancePercentage: new Big('0.13100263852242744063'),
hasErrors: false, hasErrors: false,
netPerformance: new Big('15.61'), netPerformance: new Big('19.86'),
netPerformancePercentage: new Big('0.1029683377308707124'), netPerformancePercentage: new Big('0.13100263852242744063'),
positions: [ positions: [
{ {
averagePrice: new Big('0'), averagePrice: new Big('0'),
@ -105,8 +105,8 @@ describe('PortfolioCalculator', () => {
grossPerformance: new Big('19.86'), grossPerformance: new Big('19.86'),
grossPerformancePercentage: new Big('0.13100263852242744063'), grossPerformancePercentage: new Big('0.13100263852242744063'),
investment: new Big('0'), investment: new Big('0'),
netPerformance: new Big('15.61'), netPerformance: new Big('19.86'),
netPerformancePercentage: new Big('0.1029683377308707124'), netPerformancePercentage: new Big('0.13100263852242744063'),
marketPrice: 87.8, marketPrice: 87.8,
quantity: new Big('0'), quantity: new Big('0'),
symbol: 'NOVN.SW', symbol: 'NOVN.SW',

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

@ -40,7 +40,7 @@ export class PortfolioCalculator {
private static readonly CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT = private static readonly CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT =
true; true;
private static readonly ENABLE_LOGGING = true; private static readonly ENABLE_LOGGING = false;
private currency: string; private currency: string;
private currentRateService: CurrentRateService; private currentRateService: CurrentRateService;
@ -234,11 +234,17 @@ export class PortfolioCalculator {
[symbol: string]: { [date: string]: Big }; [symbol: string]: { [date: string]: Big };
} = {}; } = {};
const maxInvestmentValuesBySymbol: {
[symbol: string]: { [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 } = {};
for (const symbol of Object.keys(symbols)) { for (const symbol of Object.keys(symbols)) {
const { netPerformanceValues, investmentValues } = this.getSymbolMetrics({ const { investmentValues, maxInvestmentValues, netPerformanceValues } =
this.getSymbolMetrics({
end, end,
marketSymbolMap, marketSymbolMap,
start, start,
@ -249,6 +255,7 @@ export class PortfolioCalculator {
netPerformanceValuesBySymbol[symbol] = netPerformanceValues; netPerformanceValuesBySymbol[symbol] = netPerformanceValues;
investmentValuesBySymbol[symbol] = investmentValues; investmentValuesBySymbol[symbol] = investmentValues;
maxInvestmentValuesBySymbol[symbol] = maxInvestmentValues;
} }
for (const currentDate of dates) { for (const currentDate of dates) {
@ -267,19 +274,28 @@ 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]);
} }
if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) {
maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[
dateString
].add(maxInvestmentValuesBySymbol[symbol][dateString]);
}
} }
} }
return Object.keys(totalNetPerformanceValues).map((date) => { return Object.keys(totalNetPerformanceValues).map((date) => {
const netPerformanceInPercentage = totalInvestmentValues[date].eq(0) const netPerformanceInPercentage = maxTotalInvestmentValues[date].eq(0)
? 0 ? 0
: totalNetPerformanceValues[date] : totalNetPerformanceValues[date]
.div(totalInvestmentValues[date]) .div(maxTotalInvestmentValues[date])
.mul(100) .mul(100)
.toNumber(); .toNumber();
@ -287,9 +303,7 @@ export class PortfolioCalculator {
date, date,
netPerformanceInPercentage, netPerformanceInPercentage,
netPerformance: totalNetPerformanceValues[date].toNumber(), netPerformance: totalNetPerformanceValues[date].toNumber(),
// TODO
totalInvestment: totalInvestmentValues[date].toNumber(), totalInvestment: totalInvestmentValues[date].toNumber(),
// TODO
value: totalInvestmentValues[date] value: totalInvestmentValues[date]
.plus(totalNetPerformanceValues[date]) .plus(totalNetPerformanceValues[date])
.toNumber() .toNumber()
@ -901,6 +915,7 @@ export class PortfolioCalculator {
let initialValue: Big; let initialValue: Big;
let investmentAtStartDate: Big; let investmentAtStartDate: Big;
const investmentValues: { [date: string]: Big } = {}; const investmentValues: { [date: string]: Big } = {};
const maxInvestmentValues: { [date: string]: Big } = {};
let lastAveragePrice = new Big(0); let lastAveragePrice = new Big(0);
// let lastTransactionInvestment = new Big(0); // let lastTransactionInvestment = new Big(0);
// let lastValueOfInvestmentBeforeTransaction = new Big(0); // let lastValueOfInvestmentBeforeTransaction = new Big(0);
@ -1172,7 +1187,8 @@ export class PortfolioCalculator {
.minus(grossPerformanceAtStartDate) .minus(grossPerformanceAtStartDate)
.minus(fees.minus(feesAtStartDate)); .minus(fees.minus(feesAtStartDate));
investmentValues[order.date] = maxTotalInvestment; investmentValues[order.date] = totalInvestment;
maxInvestmentValues[order.date] = maxTotalInvestment;
} }
if (PortfolioCalculator.ENABLE_LOGGING) { if (PortfolioCalculator.ENABLE_LOGGING) {
@ -1274,6 +1290,7 @@ export class PortfolioCalculator {
initialValue, initialValue,
grossPerformancePercentage, grossPerformancePercentage,
investmentValues, investmentValues,
maxInvestmentValues,
netPerformancePercentage, netPerformancePercentage,
netPerformanceValues, netPerformanceValues,
hasErrors: totalUnits.gt(0) && (!initialValue || !unitPriceAtEndDate), hasErrors: totalUnits.gt(0) && (!initialValue || !unitPriceAtEndDate),

Loading…
Cancel
Save