All files / src/modules/implementations/queries/support investment-returns.js

97.94% Statements 191/195
90.9% Branches 80/88
100% Functions 8/8
97.94% Lines 191/195

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 19651x 51x 51x 783x 783x 280670x 280670x 783x 3422x 3422x 3422x 3422x 3422x 3422x 3422x 3422x 3357x 3357x 3357x 3357x 3357x 3357x 3422x 482x 482x 482x 3422x 3422x 3422x 112177x 112177x 112177x 112177x 112177x 63653x 63653x 63653x 45167x 45167x 45167x 112177x   3422x 783x 3356x 3356x 3356x 3356x 3356x 3356x 3356x 3356x 783x 3421x 3421x 3421x 3421x 3408x 3408x 3421x 3358x 3421x 3421x 783x 3421x 3421x 3356x 3356x 3421x 3356x 3421x 783x 48x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 46x 47x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3155x 3155x 223x 34x 34x 189x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 3x 3x 3x 3x 3378x       3189x 3189x 3189x 3189x 3189x 3189x 3189x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 3378x 46x 46x 47x 46x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 783x 783x 783x 783x 51x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = () => {
    const DAY_MILLISECONDS = 24 * 60 * 60 * 1000;
    function daysBetween(first, second) {
        return (Date.parse(`${second}T00:00:00Z`) - Date.parse(`${first}T00:00:00Z`)) / DAY_MILLISECONDS;
    }
    function xirr(cashFlows) {
        const combined = new Map();
        for (const { date, amount } of cashFlows)
            combined.set(date, (combined.get(date) ?? 0) + amount);
        const flows = [...combined].map(([date, amount]) => ({ date, amount }))
            .filter(({ amount }) => Math.abs(amount) > 1e-9)
            .sort((left, right) => left.date.localeCompare(right.date));
        if (!flows.some(({ amount }) => amount < 0) || !flows.some(({ amount }) => amount > 0))
            return null;
        const start = flows[0].date;
        const npv = (logRate) => flows.reduce((total, flow) => total + flow.amount * Math.exp(-logRate * daysBetween(start, flow.date) / 365), 0);
        let low = -20;
        let high = Math.log(2);
        let lowValue = npv(low);
        let highValue = npv(high);
        while (Math.sign(lowValue) === Math.sign(highValue) && high < 700) {
            high = Math.min(700, high * 2 + 1);
            highValue = npv(high);
        }
        if (!Number.isFinite(lowValue) || !Number.isFinite(highValue) || Math.sign(lowValue) === Math.sign(highValue))
            return null;
        for (let iteration = 0; iteration < 200; iteration += 1) {
            const middle = (low + high) / 2;
            const middleValue = npv(middle);
            if (Math.abs(middleValue) < 1e-8)
                return Math.expm1(middle);
            if (Math.sign(middleValue) === Math.sign(lowValue)) {
                low = middle;
                lowValue = middleValue;
            }
            else {
                high = middle;
            }
        }
        return Math.expm1((low + high) / 2);
    }
    function cashFlowSpanYears(cashFlows) {
        const dates = cashFlows
            .filter(({ amount }) => Math.abs(amount) > 1e-9)
            .map(({ date }) => date)
            .sort();
        if (dates.length < 2 || dates[0] === dates.at(-1))
            return null;
        return daysBetween(dates[0], dates[dates.length - 1]) / 365;
    }
    function annualizedTimeWeightedReturn(totalReturn, from, to) {
        if (totalReturn === null)
            return null;
        if (from === null || to === null)
            return null;
        const years = daysBetween(from, to) / 365;
        if (years <= 0)
            return null;
        const annualizedReturn = Math.expm1(Math.log1p(totalReturn) / years);
        return Number.isFinite(annualizedReturn) ? annualizedReturn : null;
    }
    function totalReturnFromXirr(annualizedReturn, cashFlows) {
        if (annualizedReturn === null)
            return null;
        const years = cashFlowSpanYears(cashFlows);
        if (years === null)
            return null;
        return Math.expm1(Math.log1p(annualizedReturn) * years);
    }
    function calculatePerformance(values, flows, options, commodities, valuationCommodity) {
        if (values.length === 0) {
            return {
                from: options.from ?? null,
                to: options.to ?? null,
                commodities,
                valuationCommodity,
                openingValue: 0,
                endingValue: 0,
                netContributions: 0,
                profitLoss: 0,
                timeWeightedReturn: null,
                timeWeightedReturnAnnualized: null,
                moneyWeightedReturn: null,
                moneyWeightedReturnTotal: null,
                points: [],
            };
        }
        const effectiveFrom = options.from ?? values[0].date;
        const effectiveTo = options.to ?? [effectiveFrom, values[values.length - 1].date].sort()[1];
        const previous = [...values].reverse().find((row) => row.date < effectiveFrom);
        const periodValues = values.filter((row) => row.date >= effectiveFrom && row.date <= effectiveTo);
        const openingValue = previous?.value ?? 0;
        const endingValue = periodValues.at(-1)?.value ?? openingValue;
        const periodFlows = flows.filter((row) => row.date >= effectiveFrom && row.date <= effectiveTo);
        const flowsByDate = new Map(periodFlows.map((row) => [row.date, row.flow]));
        let priorValue = openingValue;
        let growthFactor = 1;
        let hasReturn = false;
        let validReturn = true;
        // Exclude empty time before the first investment and after the last exit,
        // while retaining any empty interval between investments.
        let returnStartDate = Math.abs(openingValue) > 1e-9 ? effectiveFrom : null;
        let lastReturnDate = null;
        let netContributions = 0;
        const investorCashFlows = [];
        if (Math.abs(openingValue) > 1e-9)
            investorCashFlows.push({ date: effectiveFrom, amount: -openingValue });
        const points = [];
        for (const row of periodValues) {
            const flow = flowsByDate.get(row.date) ?? 0;
            netContributions += flow;
            if (Math.abs(flow) > 1e-9)
                investorCashFlows.push({ date: row.date, amount: -flow });
            let factor;
            // The journal has daily closing values but no intraday valuations. Treat a
            // dated external flow as occurring at the end of that day; on the first
            // funded day, compare the closing value directly with the contribution.
            if (Math.abs(priorValue) > 1e-9) {
                factor = (row.value - flow) / priorValue;
            }
            else if (flow > 0) {
                factor = row.value / flow;
            }
            else if (Math.abs(row.value) <= 1e-9) {
                priorValue = row.value;
                const pointCashFlows = [...investorCashFlows, { date: row.date, amount: row.value }];
                const moneyWeightedReturn = xirr(pointCashFlows);
                const timeWeightedReturn = validReturn && hasReturn ? growthFactor - 1 : null;
                points.push({
                    date: row.date,
                    value: row.value,
                    netContributions,
                    profitLoss: row.value - openingValue - netContributions,
                    timeWeightedReturn,
                    timeWeightedReturnAnnualized: annualizedTimeWeightedReturn(timeWeightedReturn, returnStartDate, lastReturnDate),
                    moneyWeightedReturn,
                    moneyWeightedReturnTotal: totalReturnFromXirr(moneyWeightedReturn, pointCashFlows),
                });
                continue;
            }
            else {
                validReturn = false;
                break;
            }
            if (!Number.isFinite(factor) || factor < 0) {
                validReturn = false;
                break;
            }
            growthFactor *= factor;
            hasReturn = true;
            returnStartDate ??= row.date;
            lastReturnDate = row.date;
            priorValue = row.value;
            const pointCashFlows = [...investorCashFlows, { date: row.date, amount: row.value }];
            const moneyWeightedReturn = xirr(pointCashFlows);
            const timeWeightedReturn = validReturn ? growthFactor - 1 : null;
            points.push({
                date: row.date,
                value: row.value,
                netContributions,
                profitLoss: row.value - openingValue - netContributions,
                timeWeightedReturn,
                timeWeightedReturnAnnualized: annualizedTimeWeightedReturn(timeWeightedReturn, returnStartDate, lastReturnDate),
                moneyWeightedReturn,
                moneyWeightedReturnTotal: totalReturnFromXirr(moneyWeightedReturn, pointCashFlows),
            });
        }
        const terminalCashFlows = [...investorCashFlows];
        if (Math.abs(endingValue) > 1e-9)
            terminalCashFlows.push({ date: effectiveTo, amount: endingValue });
        const moneyWeightedReturn = xirr(terminalCashFlows);
        const timeWeightedReturn = validReturn && hasReturn ? growthFactor - 1 : null;
        return {
            from: effectiveFrom,
            to: effectiveTo,
            commodities,
            valuationCommodity,
            openingValue,
            endingValue,
            netContributions,
            profitLoss: endingValue - openingValue - netContributions,
            timeWeightedReturn,
            timeWeightedReturnAnnualized: annualizedTimeWeightedReturn(timeWeightedReturn, returnStartDate, Math.abs(endingValue) > 1e-9 ? effectiveTo : lastReturnDate),
            moneyWeightedReturn,
            moneyWeightedReturnTotal: totalReturnFromXirr(moneyWeightedReturn, terminalCashFlows),
            points,
        };
    }
    return {
        calculatePerformance,
        $$private: { xirr },
    };
};