Browse Source

Readded UnitPrice calculation and fixed Type error

pull/5027/head
Daniel Devaud 1 year ago
parent
commit
550dc7f250
  1. 55
      apps/api/src/app/portfolio/portfolio-calculator.ts

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

@ -1193,7 +1193,9 @@ export class PortfolioCalculator {
maxInvestmentValues, maxInvestmentValues,
timeWeightedInvestmentValues, timeWeightedInvestmentValues,
unitPriceAtEndDate, unitPriceAtEndDate,
symbol symbol,
exchangeRates,
currentExchangeRate
); );
return { return {
@ -1267,7 +1269,9 @@ export class PortfolioCalculator {
maxInvestmentValues: { [date: string]: Big }, maxInvestmentValues: { [date: string]: Big },
timeWeightedInvestmentValues: WithCurrencyEffect<{ [date: string]: Big }>, timeWeightedInvestmentValues: WithCurrencyEffect<{ [date: string]: Big }>,
unitPriceAtEndDate: Big, unitPriceAtEndDate: Big,
symbol: string symbol: string,
exchangeRates: { [dateString: string]: number },
currentExchangeRate: number
) { ) {
let totalInvestmentDays = 0; let totalInvestmentDays = 0;
let sumOfTimeWeightedInvestments = { let sumOfTimeWeightedInvestments = {
@ -1318,7 +1322,9 @@ export class PortfolioCalculator {
investmentValuesAccumulated, investmentValuesAccumulated,
totalInvestmentDays, totalInvestmentDays,
sumOfTimeWeightedInvestments, sumOfTimeWeightedInvestments,
timeWeightedInvestmentValues timeWeightedInvestmentValues,
exchangeRates,
currentExchangeRate
)); ));
const totalGrossPerformance = { const totalGrossPerformance = {
@ -1461,7 +1467,9 @@ export class PortfolioCalculator {
investmentValuesAccumulated: WithCurrencyEffect<{ [date: string]: Big }>, investmentValuesAccumulated: WithCurrencyEffect<{ [date: string]: Big }>,
totalInvestmentDays: number, totalInvestmentDays: number,
sumOfTimeWeightedInvestments: WithCurrencyEffect<Big>, sumOfTimeWeightedInvestments: WithCurrencyEffect<Big>,
timeWeightedInvestmentValues: WithCurrencyEffect<{ [date: string]: Big }> timeWeightedInvestmentValues: WithCurrencyEffect<{ [date: string]: Big }>,
exchangeRates: { [dateString: string]: number },
currentExchangeRate: number
) { ) {
for (let i = 0; i < orders.length; i += 1) { for (let i = 0; i < orders.length; i += 1) {
const order = orders[i]; const order = orders[i];
@ -1488,10 +1496,16 @@ export class PortfolioCalculator {
unitPriceAtStartDate unitPriceAtStartDate
); );
const exchangeRateAtOrderDate = exchangeRates[order.date];
this.handleFeeAndUnitPriceOfOrder(
order,
currentExchangeRate,
exchangeRateAtOrderDate
);
// Calculate the average start price as soon as any units are held // Calculate the average start price as soon as any units are held
let transactionInvestment: WithCurrencyEffect<Big>; let transactionInvestment: WithCurrencyEffect<Big>;
let valueOfInvestmentWithCurrencyEffect: WithCurrencyEffect<Big>;
let totalInvestmentBeforeTransaction: WithCurrencyEffect<Big>; let totalInvestmentBeforeTransaction: WithCurrencyEffect<Big>;
let valueOfInvestment; let valueOfInvestment;
@ -1565,9 +1579,9 @@ export class PortfolioCalculator {
); );
} }
const newGrossPerformance = valueOfInvestment const newGrossPerformance = valueOfInvestment.Value.minus(
.minus(totalInvestment.Value) totalInvestment.Value
.plus(grossPerformanceFromSells.Value); ).plus(grossPerformanceFromSells.Value);
const newGrossPerformanceWithCurrencyEffect = const newGrossPerformanceWithCurrencyEffect =
valueOfInvestment.WithCurrencyEffect.minus( valueOfInvestment.WithCurrencyEffect.minus(
@ -1646,6 +1660,29 @@ export class PortfolioCalculator {
}; };
} }
private handleFeeAndUnitPriceOfOrder(
order: PortfolioOrderItem,
currentExchangeRate: number,
exchangeRateAtOrderDate: number
) {
if (order.fee) {
order.feeInBaseCurrency = order.fee.mul(currentExchangeRate ?? 1);
order.feeInBaseCurrencyWithCurrencyEffect = order.fee.mul(
exchangeRateAtOrderDate ?? 1
);
}
if (order.unitPrice) {
order.unitPriceInBaseCurrency = order.unitPrice.mul(
currentExchangeRate ?? 1
);
order.unitPriceInBaseCurrencyWithCurrencyEffect = order.unitPrice.mul(
exchangeRateAtOrderDate ?? 1
);
}
}
private calculateNetPerformancePercentageForDateAndSymbol( private calculateNetPerformancePercentageForDateAndSymbol(
i: number, i: number,
orders: PortfolioOrderItem[], orders: PortfolioOrderItem[],

Loading…
Cancel
Save