Browse Source

refactor get position endpoint

Co-authored-by: Thomas <dotsilver@gmail.com>
pull/239/head
Valentin Zickner 4 years ago
committed by Thomas
parent
commit
71a3115fc6
  1. 128
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 2
      apps/client/src/app/components/position/position-detail-dialog/position-detail-dialog.component.ts

128
apps/api/src/app/portfolio/portfolio.service.ts

@ -321,9 +321,44 @@ export class PortfolioService {
aSymbol: string aSymbol: string
): Promise<PortfolioPositionDetail> { ): Promise<PortfolioPositionDetail> {
const userId = await this.getUserId(aImpersonationId); const userId = await this.getUserId(aImpersonationId);
const portfolio = await this.createPortfolio(userId);
const position = portfolio.getPositions(new Date())[aSymbol]; const portfolioCalculator = new PortfolioCalculator(
this.currentRateService,
this.request.user.Settings.currency
);
const { transactionPoints, orders } = await this.getTransactionPoints(
userId
);
if (transactionPoints?.length <= 0) {
return {
averagePrice: undefined,
currency: undefined,
firstBuyDate: undefined,
grossPerformance: undefined,
grossPerformancePercent: undefined,
historicalData: [],
investment: undefined,
marketPrice: undefined,
maxPrice: undefined,
minPrice: undefined,
quantity: undefined,
symbol: aSymbol,
transactionCount: undefined
};
}
portfolioCalculator.setTransactionPoints(transactionPoints);
const portfolioStart = parseDate(transactionPoints[0].date);
const currentPositions = await portfolioCalculator.getCurrentPositions(
portfolioStart
);
const position = currentPositions.positions.find(
(item) => item.symbol === aSymbol
);
if (position) { if (position) {
const { const {
@ -331,11 +366,10 @@ export class PortfolioService {
currency, currency,
firstBuyDate, firstBuyDate,
investment, investment,
marketPrice,
quantity, quantity,
transactionCount transactionCount
} = position; } = position;
let marketPrice = position.marketPrice;
const orders = portfolio.getOrders(aSymbol);
const historicalData = await this.dataProviderService.getHistorical( const historicalData = await this.dataProviderService.getHistorical(
[aSymbol], [aSymbol],
@ -344,33 +378,29 @@ export class PortfolioService {
new Date() new Date()
); );
if (marketPrice === 0) {
marketPrice = averagePrice;
}
const historicalDataArray: HistoricalDataItem[] = []; const historicalDataArray: HistoricalDataItem[] = [];
let currentAveragePrice: number;
let maxPrice = marketPrice; let maxPrice = marketPrice;
let minPrice = marketPrice; let minPrice = marketPrice;
if (historicalData[aSymbol]) { if (historicalData[aSymbol]) {
let j = -1;
for (const [date, { marketPrice }] of Object.entries( for (const [date, { marketPrice }] of Object.entries(
historicalData[aSymbol] historicalData[aSymbol]
)) { )) {
const currentDate = parse(date, DATE_FORMAT, new Date()); while (
if ( j + 1 < transactionPoints.length &&
isSameDay(currentDate, parseISO(orders[0]?.getDate())) || !isAfter(parseDate(transactionPoints[j + 1].date), parseDate(date))
isAfter(currentDate, parseISO(orders[0]?.getDate()))
) { ) {
// Get snapshot of first day of next month j++;
const snapshot = portfolio.get( }
addMonths(setDate(currentDate, 1), 1) let currentAveragePrice = 0;
)?.[0]?.positions[aSymbol]; const currentSymbol = transactionPoints[j].items.find(
orders.shift(); (item) => item.symbol === aSymbol
);
if (snapshot?.averagePrice) { if (currentSymbol) {
currentAveragePrice = snapshot.averagePrice; currentAveragePrice = currentSymbol.investment
} .div(currentSymbol.quantity)
.toNumber();
} }
historicalDataArray.push({ historicalDataArray.push({
@ -379,58 +409,40 @@ export class PortfolioService {
value: marketPrice value: marketPrice
}); });
if ( maxPrice = Math.max(marketPrice ?? 0, maxPrice);
marketPrice && minPrice = Math.min(marketPrice ?? Number.MAX_SAFE_INTEGER, minPrice);
(marketPrice > maxPrice || maxPrice === undefined)
) {
maxPrice = marketPrice;
}
if (
marketPrice &&
(marketPrice < minPrice || minPrice === undefined)
) {
minPrice = marketPrice;
}
} }
} }
return { return {
averagePrice, averagePrice: averagePrice.toNumber(),
currency, currency,
firstBuyDate, firstBuyDate,
investment, investment: investment.toNumber(),
marketPrice, marketPrice,
maxPrice, maxPrice,
minPrice, minPrice,
quantity, quantity: quantity.toNumber(),
transactionCount, transactionCount,
grossPerformance: this.exchangeRateDataService.toCurrency( grossPerformance: position.grossPerformance.toNumber(),
marketPrice - averagePrice, grossPerformancePercent: position.grossPerformancePercentage.toNumber(),
currency,
this.request.user.Settings.currency
),
grossPerformancePercent: roundTo(
(marketPrice - averagePrice) / averagePrice,
4
),
historicalData: historicalDataArray, historicalData: historicalDataArray,
symbol: aSymbol symbol: aSymbol
}; };
} else if (portfolio.getMinDate()) { } else {
const currentData = await this.dataProviderService.get([aSymbol]); const currentData = await this.dataProviderService.get([aSymbol]);
let historicalData = await this.dataProviderService.getHistorical( let historicalData = await this.dataProviderService.getHistorical(
[aSymbol], [aSymbol],
'day', 'day',
portfolio.getMinDate(), portfolioStart,
new Date() new Date()
); );
if (isEmpty(historicalData)) { if (isEmpty(historicalData)) {
historicalData = await this.dataProviderService.getHistoricalRaw( historicalData = await this.dataProviderService.getHistoricalRaw(
[{ dataSource: DataSource.YAHOO, symbol: aSymbol }], [{ dataSource: DataSource.YAHOO, symbol: aSymbol }],
portfolio.getMinDate(), portfolioStart,
new Date() new Date()
); );
} }
@ -462,22 +474,6 @@ export class PortfolioService {
transactionCount: undefined transactionCount: undefined
}; };
} }
return {
averagePrice: undefined,
currency: undefined,
firstBuyDate: undefined,
grossPerformance: undefined,
grossPerformancePercent: undefined,
historicalData: [],
investment: undefined,
marketPrice: undefined,
maxPrice: undefined,
minPrice: undefined,
quantity: undefined,
symbol: aSymbol,
transactionCount: undefined
};
} }
public async getPositions( public async getPositions(

2
apps/client/src/app/components/position/position-detail-dialog/position-detail-dialog.component.ts

@ -67,7 +67,7 @@ export class PositionDetailDialog implements OnDestroy {
this.benchmarkDataItems = []; this.benchmarkDataItems = [];
this.currency = currency; this.currency = currency;
this.firstBuyDate = firstBuyDate; this.firstBuyDate = firstBuyDate;
this.grossPerformance = quantity * grossPerformance; this.grossPerformance = grossPerformance;
this.grossPerformancePercent = grossPerformancePercent; this.grossPerformancePercent = grossPerformancePercent;
this.historicalDataItems = historicalData.map( this.historicalDataItems = historicalData.map(
(historicalDataItem) => { (historicalDataItem) => {

Loading…
Cancel
Save