mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.1 KiB
97 lines
2.1 KiB
import { Activity } from '@ghostfolio/common/interfaces';
|
|
|
|
import {
|
|
ActivityFundingResult,
|
|
createActivityFunding
|
|
} from './activity-cash-flow.provider';
|
|
import {
|
|
BenchmarkPrice,
|
|
evaluateRange,
|
|
FinancialPoint,
|
|
simulateBenchmark
|
|
} from './activity-matched-benchmark';
|
|
import {
|
|
BenchmarkFxQuote,
|
|
BenchmarkFxResult,
|
|
convertBenchmarkPrices
|
|
} from './benchmark-fx.adapter';
|
|
import {
|
|
BenchmarkPriceAdapterResult,
|
|
prepareBenchmarkPrices
|
|
} from './benchmark-price.adapter';
|
|
|
|
export interface ActivityMatchedBenchmarkPipelineInput {
|
|
activities: Activity[];
|
|
currency: string;
|
|
benchmarkCurrency: string;
|
|
prices: BenchmarkPrice[];
|
|
fxQuotes: BenchmarkFxQuote[];
|
|
valuationDates: string[];
|
|
start: string;
|
|
end: string;
|
|
method: 'ROI' | 'ROAI';
|
|
maxPriceAgeDays?: number;
|
|
maxFxAgeDays?: number;
|
|
}
|
|
|
|
export interface ActivityMatchedBenchmarkPipelineResult {
|
|
funding: ActivityFundingResult;
|
|
fx: BenchmarkFxResult;
|
|
preparedPrices: BenchmarkPriceAdapterResult;
|
|
points: FinancialPoint[];
|
|
performance: ReturnType<typeof evaluateRange>;
|
|
}
|
|
|
|
export function runActivityMatchedBenchmarkPipeline({
|
|
activities,
|
|
currency,
|
|
benchmarkCurrency,
|
|
prices,
|
|
fxQuotes,
|
|
valuationDates,
|
|
start,
|
|
end,
|
|
method,
|
|
maxPriceAgeDays,
|
|
maxFxAgeDays
|
|
}: ActivityMatchedBenchmarkPipelineInput): ActivityMatchedBenchmarkPipelineResult {
|
|
const funding = createActivityFunding({
|
|
activities,
|
|
currency
|
|
});
|
|
|
|
if (!funding.canSimulate) {
|
|
throw new Error('Invalid activity funding');
|
|
}
|
|
|
|
const preparedPrices = prepareBenchmarkPrices({
|
|
executionDates: funding.flows.map(({ date }) => date),
|
|
prices,
|
|
maxExecutionAgeDays: maxPriceAgeDays
|
|
});
|
|
|
|
const fx = convertBenchmarkPrices({
|
|
prices: preparedPrices.prices,
|
|
currencyFrom: benchmarkCurrency,
|
|
currencyTo: currency,
|
|
fxQuotes,
|
|
maxFxAgeDays
|
|
});
|
|
|
|
const points = simulateBenchmark({
|
|
flows: funding.flows,
|
|
prices: fx.prices,
|
|
valuationDates,
|
|
maxPriceAgeDays
|
|
});
|
|
|
|
const performance = evaluateRange(points, method, start, end);
|
|
|
|
return {
|
|
funding,
|
|
fx,
|
|
preparedPrices,
|
|
points,
|
|
performance
|
|
};
|
|
}
|
|
|