Files
Neon-Desk/lib/financial-metrics.ts
francy51 24aa8e33d4 Consolidate metric definitions with Rust JSON as single source of truth
- Add core.computed.json with 32 ratio definitions (filing + market derived)
- Add Rust types for ComputedDefinition and ComputationSpec
- Create generate-taxonomy.ts to generate TypeScript from Rust JSON
- Generate lib/generated/ (gitignored) with surfaces, computed, kpis
- Update financial-metrics.ts to use generated definitions
- Add build-time generation via 'bun run generate'
- Add taxonomy architecture documentation

Two-phase ratio computation:
- Filing-derived: margins, returns, per-share, growth (Rust computes)
- Market-derived: valuation ratios (TypeScript computes with price data)

All 32 ratios defined in core.computed.json:
- Margins: gross, operating, ebitda, net, fcf
- Returns: roa, roe, roic, roce
- Financial health: debt_to_equity, net_debt_to_ebitda, cash_to_debt, current_ratio
- Per-share: revenue, fcf, book_value
- Growth: yoy metrics + 3y/5y cagr
- Valuation: market_cap, ev, p/e, p/fcf, p/b, ev/sales, ev/ebitda, ev/fcf
2026-03-15 15:22:51 -04:00

88 lines
2.3 KiB
TypeScript

import type {
FinancialCadence,
FinancialSurfaceKind,
FinancialUnit,
RatioRow
} from '@/lib/types';
import {
type ComputedDefinition,
type SurfaceDefinition,
ALL_COMPUTED,
INCOME_SURFACES,
BALANCE_SURFACES,
CASH_FLOW_SURFACES,
RATIO_CATEGORIES
} from '@/lib/generated';
export type GraphableFinancialSurfaceKind = Extract<
FinancialSurfaceKind,
'income_statement' | 'balance_sheet' | 'cash_flow_statement' | 'ratios'
>;
export type StatementMetricDefinition = {
key: string;
label: string;
category: string;
order: number;
unit: FinancialUnit;
};
export type RatioMetricDefinition = {
key: string;
label: string;
category: string;
order: number;
unit: RatioRow['unit'];
denominatorKey: string | null;
supportedCadences?: readonly FinancialCadence[];
};
export const GRAPHABLE_FINANCIAL_SURFACES: readonly GraphableFinancialSurfaceKind[] = [
'income_statement',
'balance_sheet',
'cash_flow_statement',
'ratios'
] as const;
function surfaceToMetric(surface: SurfaceDefinition): StatementMetricDefinition {
return {
key: surface.surface_key,
label: surface.label,
category: surface.category,
order: surface.order,
unit: surface.unit
};
}
function computedToRatioMetric(computed: ComputedDefinition): RatioMetricDefinition {
const denominatorKey = computed.computation.type === 'ratio'
? computed.computation.denominator
: computed.computation.type === 'per_share'
? computed.computation.shares_key
: null;
return {
key: computed.key,
label: computed.label,
category: computed.category,
order: computed.order,
unit: computed.unit as RatioRow['unit'],
denominatorKey,
supportedCadences: computed.supported_cadences as readonly FinancialCadence[] | undefined
};
}
export const INCOME_STATEMENT_METRIC_DEFINITIONS: StatementMetricDefinition[] =
INCOME_SURFACES.map(surfaceToMetric);
export const BALANCE_SHEET_METRIC_DEFINITIONS: StatementMetricDefinition[] =
BALANCE_SURFACES.map(surfaceToMetric);
export const CASH_FLOW_STATEMENT_METRIC_DEFINITIONS: StatementMetricDefinition[] =
CASH_FLOW_SURFACES.map(surfaceToMetric);
export const RATIO_DEFINITIONS: RatioMetricDefinition[] =
ALL_COMPUTED.map(computedToRatioMetric);
export { RATIO_CATEGORIES, type RatioCategory } from '@/lib/generated';