Commit a0d18598 authored by Anthony Jacob's avatar Anthony Jacob
Browse files

add extra info to portfolio detail

parent cb6d334e
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -87,7 +87,18 @@ export class PortfoliosService {
            (StockData.data ?? []).map((stock: StockInfo) => [stock.ticker, stock]),
        );

        let totalOpenPositionsValue = 0;
        let totalOpenPositionsCurrentValue = 0;
        let totalOpenPositionsOriginalBuyValue = 0;
        let totalFeesAndTaxes = 0;

        const totalClosedPositionsFeesAndTaxes = await this.positionHistoryRepo.createQueryBuilder('ph')
            .select('SUM(ph.buyFees + ph.buyTaxes + ph.sellFees + ph.sellTaxes)', 'total')
            .where('ph.portfolio_id = :portfolioId', { portfolioId: id })
            .getRawOne();

        totalFeesAndTaxes += this.toNumber(totalClosedPositionsFeesAndTaxes?.total);


        /* group position by ticker and enrich with stockdata */
        const positionGroupedByTicker: Record<string, Array<PositionWithCurrentPrice>> = {};

@@ -98,11 +109,14 @@ export class PortfoliosService {
                positionGroupedByTicker[position.ticker] = [];
            }

            totalFeesAndTaxes += this.toNumber(position.buyFees) + this.toNumber(position.buyTaxes);

            const stockInfo = stockByTicker.get(position.ticker);
            if (stockInfo) {
                console.log(`Found stock data for ${position.ticker}`);
                const price = this.toNumber(stockInfo.price);
                totalOpenPositionsValue += this.toNumber(position.quantity) * price;
                totalOpenPositionsCurrentValue += this.toNumber(position.quantity) * price;
                totalOpenPositionsOriginalBuyValue += this.toNumber(position.quantity) * this.toNumber(position.buyUnitPrice);

                (position as PositionWithCurrentPrice).currentPrice = stockInfo.price;
                (position as PositionWithCurrentPrice).name = stockInfo.name;
@@ -118,7 +132,9 @@ export class PortfoliosService {
            name: portfolio.name,
            initialCash: portfolio.initialCash,
            currentCash: portfolio.currentCash,
            totalOpenPositionsValue,
            totalOpenPositionsCurrentValue,
            totalOpenPositionsOriginalBuyValue,
            totalFeesAndTaxes,
            position: positionGroupedByTicker,
            stockDataWarnings: StockData.warnings,
            stockDataErrors: StockData.errors,