Compare commits
2 Commits
main
...
feature/ch
Author | SHA1 | Date | |
---|---|---|---|
|
5799b9e71c | ||
|
188389d26c |
@ -1,6 +1,11 @@
|
|||||||
import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/timeline-info.interface';
|
import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/timeline-info.interface';
|
||||||
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
|
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
|
||||||
import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper';
|
import {
|
||||||
|
DATE_FORMAT,
|
||||||
|
getAverage,
|
||||||
|
parseDate,
|
||||||
|
resetHours
|
||||||
|
} from '@ghostfolio/common/helper';
|
||||||
import {
|
import {
|
||||||
DataProviderInfo,
|
DataProviderInfo,
|
||||||
ResponseError,
|
ResponseError,
|
||||||
@ -43,9 +48,6 @@ import { TransactionPointSymbol } from './interfaces/transaction-point-symbol.in
|
|||||||
import { TransactionPoint } from './interfaces/transaction-point.interface';
|
import { TransactionPoint } from './interfaces/transaction-point.interface';
|
||||||
|
|
||||||
export class PortfolioCalculator {
|
export class PortfolioCalculator {
|
||||||
private static readonly CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT =
|
|
||||||
true;
|
|
||||||
|
|
||||||
private static readonly ENABLE_LOGGING = false;
|
private static readonly ENABLE_LOGGING = false;
|
||||||
|
|
||||||
private currency: string;
|
private currency: string;
|
||||||
@ -1162,11 +1164,11 @@ export class PortfolioCalculator {
|
|||||||
order.type === 'BUY'
|
order.type === 'BUY'
|
||||||
? order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type))
|
? order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type))
|
||||||
: totalUnits.gt(0)
|
: totalUnits.gt(0)
|
||||||
? totalInvestment
|
? totalInvestment
|
||||||
.div(totalUnits)
|
.div(totalUnits)
|
||||||
.mul(order.quantity)
|
.mul(order.quantity)
|
||||||
.mul(this.getFactor(order.type))
|
.mul(this.getFactor(order.type))
|
||||||
: new Big(0);
|
: new Big(0);
|
||||||
|
|
||||||
if (PortfolioCalculator.ENABLE_LOGGING) {
|
if (PortfolioCalculator.ENABLE_LOGGING) {
|
||||||
console.log('totalInvestment', totalInvestment.toNumber());
|
console.log('totalInvestment', totalInvestment.toNumber());
|
||||||
@ -1266,6 +1268,10 @@ export class PortfolioCalculator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const averageInvestmentBetweenStartAndEndDate = getAverage(
|
||||||
|
Object.values(investmentValues)
|
||||||
|
);
|
||||||
|
|
||||||
const totalGrossPerformance = grossPerformance.minus(
|
const totalGrossPerformance = grossPerformance.minus(
|
||||||
grossPerformanceAtStartDate
|
grossPerformanceAtStartDate
|
||||||
);
|
);
|
||||||
@ -1274,17 +1280,12 @@ export class PortfolioCalculator {
|
|||||||
.minus(grossPerformanceAtStartDate)
|
.minus(grossPerformanceAtStartDate)
|
||||||
.minus(fees.minus(feesAtStartDate));
|
.minus(fees.minus(feesAtStartDate));
|
||||||
|
|
||||||
const maxInvestmentBetweenStartAndEndDate = valueAtStartDate.plus(
|
|
||||||
maxTotalInvestment.minus(investmentAtStartDate)
|
|
||||||
);
|
|
||||||
|
|
||||||
const grossPerformancePercentage =
|
const grossPerformancePercentage =
|
||||||
PortfolioCalculator.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT ||
|
|
||||||
averagePriceAtStartDate.eq(0) ||
|
averagePriceAtStartDate.eq(0) ||
|
||||||
averagePriceAtEndDate.eq(0) ||
|
averagePriceAtEndDate.eq(0) ||
|
||||||
orders[indexOfStartOrder].unitPrice.eq(0)
|
orders[indexOfStartOrder].unitPrice.eq(0)
|
||||||
? maxInvestmentBetweenStartAndEndDate.gt(0)
|
? averageInvestmentBetweenStartAndEndDate.gt(0)
|
||||||
? totalGrossPerformance.div(maxInvestmentBetweenStartAndEndDate)
|
? totalGrossPerformance.div(averageInvestmentBetweenStartAndEndDate)
|
||||||
: new Big(0)
|
: new Big(0)
|
||||||
: // This formula has the issue that buying more units with a price
|
: // This formula has the issue that buying more units with a price
|
||||||
// lower than the average buying price results in a positive
|
// lower than the average buying price results in a positive
|
||||||
@ -1301,12 +1302,11 @@ export class PortfolioCalculator {
|
|||||||
: new Big(0);
|
: new Big(0);
|
||||||
|
|
||||||
const netPerformancePercentage =
|
const netPerformancePercentage =
|
||||||
PortfolioCalculator.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT ||
|
|
||||||
averagePriceAtStartDate.eq(0) ||
|
averagePriceAtStartDate.eq(0) ||
|
||||||
averagePriceAtEndDate.eq(0) ||
|
averagePriceAtEndDate.eq(0) ||
|
||||||
orders[indexOfStartOrder].unitPrice.eq(0)
|
orders[indexOfStartOrder].unitPrice.eq(0)
|
||||||
? maxInvestmentBetweenStartAndEndDate.gt(0)
|
? averageInvestmentBetweenStartAndEndDate.gt(0)
|
||||||
? totalNetPerformance.div(maxInvestmentBetweenStartAndEndDate)
|
? totalNetPerformance.div(averageInvestmentBetweenStartAndEndDate)
|
||||||
: new Big(0)
|
: new Big(0)
|
||||||
: // This formula has the issue that buying more units with a price
|
: // This formula has the issue that buying more units with a price
|
||||||
// lower than the average buying price results in a positive
|
// lower than the average buying price results in a positive
|
||||||
|
@ -133,6 +133,14 @@ export function getAssetProfileIdentifier({ dataSource, symbol }: UniqueAsset) {
|
|||||||
return `${dataSource}-${symbol}`;
|
return `${dataSource}-${symbol}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAverage(aArray: Big[]) {
|
||||||
|
if (aArray?.length > 0) {
|
||||||
|
return aArray.reduce((a, b) => a.plus(b), new Big(0)).div(aArray.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Big(0);
|
||||||
|
}
|
||||||
|
|
||||||
export function getBackgroundColor(aColorScheme: ColorScheme) {
|
export function getBackgroundColor(aColorScheme: ColorScheme) {
|
||||||
return getCssVariable(
|
return getCssVariable(
|
||||||
aColorScheme === 'DARK' ||
|
aColorScheme === 'DARK' ||
|
||||||
|
Loading…
x
Reference in New Issue
Block a user