Feature/improve usage of portfolio calculator in holding endpoint (#3727)
* Improve usage of portfolio calculator
This commit is contained in:
parent
a6d9f5dd69
commit
c48e4ec4c6
@ -602,14 +602,7 @@ export class PortfolioService {
|
|||||||
userId
|
userId
|
||||||
});
|
});
|
||||||
|
|
||||||
const orders = activities.filter(({ SymbolProfile }) => {
|
if (activities.length === 0) {
|
||||||
return (
|
|
||||||
SymbolProfile.dataSource === aDataSource &&
|
|
||||||
SymbolProfile.symbol === aSymbol
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (orders.length <= 0) {
|
|
||||||
return {
|
return {
|
||||||
accounts: [],
|
accounts: [],
|
||||||
averagePrice: undefined,
|
averagePrice: undefined,
|
||||||
@ -646,10 +639,8 @@ export class PortfolioService {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const portfolioCalculator = this.calculatorFactory.createCalculator({
|
const portfolioCalculator = this.calculatorFactory.createCalculator({
|
||||||
|
activities,
|
||||||
userId,
|
userId,
|
||||||
activities: orders.filter((order) => {
|
|
||||||
return ['BUY', 'DIVIDEND', 'ITEM', 'SELL'].includes(order.type);
|
|
||||||
}),
|
|
||||||
calculationType: PerformanceCalculationType.TWR,
|
calculationType: PerformanceCalculationType.TWR,
|
||||||
currency: userCurrency
|
currency: userCurrency
|
||||||
});
|
});
|
||||||
@ -659,8 +650,8 @@ export class PortfolioService {
|
|||||||
|
|
||||||
const { positions } = await portfolioCalculator.getSnapshot();
|
const { positions } = await portfolioCalculator.getSnapshot();
|
||||||
|
|
||||||
const position = positions.find(({ symbol }) => {
|
const position = positions.find(({ dataSource, symbol }) => {
|
||||||
return symbol === aSymbol;
|
return dataSource === aDataSource && symbol === aSymbol;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (position) {
|
if (position) {
|
||||||
@ -673,14 +664,22 @@ export class PortfolioService {
|
|||||||
firstBuyDate,
|
firstBuyDate,
|
||||||
marketPrice,
|
marketPrice,
|
||||||
quantity,
|
quantity,
|
||||||
|
symbol,
|
||||||
tags,
|
tags,
|
||||||
timeWeightedInvestment,
|
timeWeightedInvestment,
|
||||||
timeWeightedInvestmentWithCurrencyEffect,
|
timeWeightedInvestmentWithCurrencyEffect,
|
||||||
transactionCount
|
transactionCount
|
||||||
} = position;
|
} = position;
|
||||||
|
|
||||||
|
const activitiesOfPosition = activities.filter(({ SymbolProfile }) => {
|
||||||
|
return (
|
||||||
|
SymbolProfile.dataSource === dataSource &&
|
||||||
|
SymbolProfile.symbol === symbol
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
const accounts: PortfolioHoldingDetail['accounts'] = uniqBy(
|
const accounts: PortfolioHoldingDetail['accounts'] = uniqBy(
|
||||||
orders.filter(({ Account }) => {
|
activitiesOfPosition.filter(({ Account }) => {
|
||||||
return Account;
|
return Account;
|
||||||
}),
|
}),
|
||||||
'Account.id'
|
'Account.id'
|
||||||
@ -715,8 +714,8 @@ export class PortfolioService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const historicalDataArray: HistoricalDataItem[] = [];
|
const historicalDataArray: HistoricalDataItem[] = [];
|
||||||
let maxPrice = Math.max(orders[0].unitPrice, marketPrice);
|
let maxPrice = Math.max(activitiesOfPosition[0].unitPrice, marketPrice);
|
||||||
let minPrice = Math.min(orders[0].unitPrice, marketPrice);
|
let minPrice = Math.min(activitiesOfPosition[0].unitPrice, marketPrice);
|
||||||
|
|
||||||
if (historicalData[aSymbol]) {
|
if (historicalData[aSymbol]) {
|
||||||
let j = -1;
|
let j = -1;
|
||||||
@ -760,10 +759,10 @@ export class PortfolioService {
|
|||||||
} else {
|
} else {
|
||||||
// Add historical entry for buy date, if no historical data available
|
// Add historical entry for buy date, if no historical data available
|
||||||
historicalDataArray.push({
|
historicalDataArray.push({
|
||||||
averagePrice: orders[0].unitPrice,
|
averagePrice: activitiesOfPosition[0].unitPrice,
|
||||||
date: firstBuyDate,
|
date: firstBuyDate,
|
||||||
marketPrice: orders[0].unitPrice,
|
marketPrice: activitiesOfPosition[0].unitPrice,
|
||||||
quantity: orders[0].quantity
|
quantity: activitiesOfPosition[0].quantity
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -773,7 +772,6 @@ export class PortfolioService {
|
|||||||
marketPrice,
|
marketPrice,
|
||||||
maxPrice,
|
maxPrice,
|
||||||
minPrice,
|
minPrice,
|
||||||
orders,
|
|
||||||
SymbolProfile,
|
SymbolProfile,
|
||||||
tags,
|
tags,
|
||||||
transactionCount,
|
transactionCount,
|
||||||
@ -805,6 +803,7 @@ export class PortfolioService {
|
|||||||
]?.toNumber(),
|
]?.toNumber(),
|
||||||
netPerformanceWithCurrencyEffect:
|
netPerformanceWithCurrencyEffect:
|
||||||
position.netPerformanceWithCurrencyEffectMap?.['max']?.toNumber(),
|
position.netPerformanceWithCurrencyEffectMap?.['max']?.toNumber(),
|
||||||
|
orders: activitiesOfPosition,
|
||||||
quantity: quantity.toNumber(),
|
quantity: quantity.toNumber(),
|
||||||
value: this.exchangeRateDataService.toCurrency(
|
value: this.exchangeRateDataService.toCurrency(
|
||||||
quantity.mul(marketPrice ?? 0).toNumber(),
|
quantity.mul(marketPrice ?? 0).toNumber(),
|
||||||
@ -862,7 +861,6 @@ export class PortfolioService {
|
|||||||
marketPrice,
|
marketPrice,
|
||||||
maxPrice,
|
maxPrice,
|
||||||
minPrice,
|
minPrice,
|
||||||
orders,
|
|
||||||
SymbolProfile,
|
SymbolProfile,
|
||||||
accounts: [],
|
accounts: [],
|
||||||
averagePrice: 0,
|
averagePrice: 0,
|
||||||
@ -882,6 +880,7 @@ export class PortfolioService {
|
|||||||
netPerformancePercent: undefined,
|
netPerformancePercent: undefined,
|
||||||
netPerformancePercentWithCurrencyEffect: undefined,
|
netPerformancePercentWithCurrencyEffect: undefined,
|
||||||
netPerformanceWithCurrencyEffect: undefined,
|
netPerformanceWithCurrencyEffect: undefined,
|
||||||
|
orders: [],
|
||||||
quantity: 0,
|
quantity: 0,
|
||||||
tags: [],
|
tags: [],
|
||||||
transactionCount: undefined,
|
transactionCount: undefined,
|
||||||
@ -912,7 +911,7 @@ export class PortfolioService {
|
|||||||
userCurrency: this.getUserCurrency()
|
userCurrency: this.getUserCurrency()
|
||||||
});
|
});
|
||||||
|
|
||||||
if (activities?.length <= 0) {
|
if (activities.length === 0) {
|
||||||
return {
|
return {
|
||||||
hasErrors: false,
|
hasErrors: false,
|
||||||
positions: []
|
positions: []
|
||||||
@ -1037,14 +1036,12 @@ export class PortfolioService {
|
|||||||
dateRange = 'max',
|
dateRange = 'max',
|
||||||
filters,
|
filters,
|
||||||
impersonationId,
|
impersonationId,
|
||||||
portfolioCalculator,
|
|
||||||
userId,
|
userId,
|
||||||
withExcludedAccounts = false
|
withExcludedAccounts = false
|
||||||
}: {
|
}: {
|
||||||
dateRange?: DateRange;
|
dateRange?: DateRange;
|
||||||
filters?: Filter[];
|
filters?: Filter[];
|
||||||
impersonationId: string;
|
impersonationId: string;
|
||||||
portfolioCalculator?: PortfolioCalculator;
|
|
||||||
userId: string;
|
userId: string;
|
||||||
withExcludedAccounts?: boolean;
|
withExcludedAccounts?: boolean;
|
||||||
}): Promise<PortfolioPerformanceResponse> {
|
}): Promise<PortfolioPerformanceResponse> {
|
||||||
@ -1089,7 +1086,7 @@ export class PortfolioService {
|
|||||||
userId
|
userId
|
||||||
});
|
});
|
||||||
|
|
||||||
if (accountBalanceItems?.length <= 0 && activities?.length <= 0) {
|
if (accountBalanceItems.length === 0 && activities.length === 0) {
|
||||||
return {
|
return {
|
||||||
chart: [],
|
chart: [],
|
||||||
firstOrderDate: undefined,
|
firstOrderDate: undefined,
|
||||||
@ -1106,16 +1103,14 @@ export class PortfolioService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
portfolioCalculator =
|
const portfolioCalculator = this.calculatorFactory.createCalculator({
|
||||||
portfolioCalculator ??
|
accountBalanceItems,
|
||||||
this.calculatorFactory.createCalculator({
|
activities,
|
||||||
accountBalanceItems,
|
filters,
|
||||||
activities,
|
userId,
|
||||||
filters,
|
calculationType: PerformanceCalculationType.TWR,
|
||||||
userId,
|
currency: userCurrency
|
||||||
calculationType: PerformanceCalculationType.TWR,
|
});
|
||||||
currency: userCurrency
|
|
||||||
});
|
|
||||||
|
|
||||||
const { errors, hasErrors, historicalData } =
|
const { errors, hasErrors, historicalData } =
|
||||||
await portfolioCalculator.getSnapshot();
|
await portfolioCalculator.getSnapshot();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user