fix position currency conversion (#256)

Co-authored-by: Valentin Zickner <github@zickner.ch>
pull/257/head
Valentin Zickner 3 years ago committed by GitHub
parent c4b6273886
commit bf256ae50c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -258,14 +258,9 @@ export class PortfolioService {
): Promise<PortfolioPositionDetail> { ): Promise<PortfolioPositionDetail> {
const userId = await this.getUserId(aImpersonationId); const userId = await this.getUserId(aImpersonationId);
const portfolioCalculator = new PortfolioCalculator( const orders = await this.getOrders(userId);
this.currentRateService,
this.request.user.Settings.currency
);
const { transactionPoints } = await this.getTransactionPoints(userId);
if (transactionPoints?.length <= 0) { if (orders.length <= 0) {
return { return {
averagePrice: undefined, averagePrice: undefined,
currency: undefined, currency: undefined,
@ -283,7 +278,24 @@ export class PortfolioService {
}; };
} }
portfolioCalculator.setTransactionPoints(transactionPoints); const positionCurrency = orders[0].currency;
const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({
currency: order.currency,
date: format(order.date, DATE_FORMAT),
name: order.SymbolProfile?.name,
quantity: new Big(order.quantity),
symbol: order.symbol,
type: <OrderType>order.type,
unitPrice: new Big(order.unitPrice)
}));
const portfolioCalculator = new PortfolioCalculator(
this.currentRateService,
positionCurrency
);
portfolioCalculator.computeTransactionPoints(portfolioOrders);
const transactionPoints = portfolioCalculator.getTransactionPoints();
const portfolioStart = parseDate(transactionPoints[0].date); const portfolioStart = parseDate(transactionPoints[0].date);
const currentPositions = await portfolioCalculator.getCurrentPositions( const currentPositions = await portfolioCalculator.getCurrentPositions(
@ -299,16 +311,22 @@ export class PortfolioService {
averagePrice, averagePrice,
currency, currency,
firstBuyDate, firstBuyDate,
investment, marketPrice,
quantity, quantity,
transactionCount transactionCount
} = position; } = position;
// Convert market price back to currency of position // Convert investment and gross performance to currency of user
const marketPrice = this.exchangeRateDataService.toCurrency( const userCurrency = this.request.user.Settings.currency;
position.marketPrice, const investment = this.exchangeRateDataService.toCurrency(
this.request.user.Settings.currency, position.investment.toNumber(),
currency currency,
userCurrency
);
const grossPerformance = this.exchangeRateDataService.toCurrency(
position.grossPerformance.toNumber(),
currency,
userCurrency
); );
const historicalData = await this.dataProviderService.getHistorical( const historicalData = await this.dataProviderService.getHistorical(
@ -357,15 +375,15 @@ export class PortfolioService {
return { return {
currency, currency,
firstBuyDate, firstBuyDate,
grossPerformance,
investment,
marketPrice, marketPrice,
maxPrice, maxPrice,
minPrice, minPrice,
transactionCount, transactionCount,
averagePrice: averagePrice.toNumber(), averagePrice: averagePrice.toNumber(),
grossPerformance: position.grossPerformance.toNumber(),
grossPerformancePercent: position.grossPerformancePercentage.toNumber(), grossPerformancePercent: position.grossPerformancePercentage.toNumber(),
historicalData: historicalDataArray, historicalData: historicalDataArray,
investment: investment.toNumber(),
quantity: quantity.toNumber(), quantity: quantity.toNumber(),
symbol: aSymbol symbol: aSymbol
}; };

@ -45,7 +45,7 @@
<gf-value <gf-value
class="mr-3" class="mr-3"
[colorizeSign]="true" [colorizeSign]="true"
[currency]="position?.currency" [currency]="baseCurrency"
[locale]="locale" [locale]="locale"
[value]="position?.grossPerformance" [value]="position?.grossPerformance"
></gf-value> ></gf-value>

Loading…
Cancel
Save