Chartsv26.10.18
Kits

Portfolio kit

Return and risk metrics of a book, the benchmark and underwater curves, allocation views, the monthly heatmap and the four risk figures.

@unifin/chart-kit-portfolio owns the performance and risk maths of a portfolio and the painters of the views a portfolio surface shows.

import { periodReturns, maxDrawdown, sharpeRatio, sortinoRatio, beta } from '@unifin/chart-kit-portfolio';

const daily = periodReturns(portfolioValues);

maxDrawdown(portfolioValues);     // worst peak-to-trough decline
sharpeRatio(daily);               // annualised, 252 periods
sortinoRatio(daily);              // the same on downside deviation
beta(daily, benchmarkReturns);    // sensitivity to the benchmark

What the kit renders

ViewFunctionReads
Benchmark curvesbenchmarkCurves + paintBenchmarkCurvesportfolio and benchmark values; the curve is indexed to 100 at the first point
UnderwaterdrawdownSeries + paintUnderwaterthe same values; every point is a percent below the running peak
AllocationallocationShares + treemapRects / paintTreemap, paintAllocationDonut, paintStackedBarweights per instrument; the treemap is drawn by the kit, the donut and the stacked bar are alternatives
Monthly heatmapmonthlyGrid + paintReturnsHeatmapmonthly returns; rows are years, columns are months, growth and fall tones come from the theme
Positions tablebuildPositionRows + POSITION_COLUMNSquantity, value, weight, profit and profit percent per position

Live curves, allocation and heatmap

Portfolio kit - live panel
Theme
Portfolio - benchmark curve and drawdown
Hover for values, click to pin, double click to reset

Benchmark curve, underwater view and allocation

The S105 wave ramps the fills: the underwater band is dense at the zero line and fades into the depth (0.28 -> 0.06), the portfolio curve of the benchmark frame gets a ribbon down to its baseline (0.16 -> 0.02), and every allocation shape - treemap cell, stacked bar slice and donut ring - carries one vertical ramp (0.42 -> 0.25) with a light 1 px edge. The fills are additive options of the kit painters; on a canvas without gradients every shape keeps its flat tone.

The ramped underwater ribbon and benchmark underlay

Risk figures

Four figures answer where the risk of a book comes from. They reuse the analytic family of the core - the correlogram is the shared heatmap with the diverging tone, the cone is the fan chart - so no figure ships its own painter.

Theme
FigureFunctionReads
CorrelogramcorrelationGrid + paintCorrelationMatrixa labelled correlation matrix; the diagonal is forced to 1, out-of-range values are clamped and the upper triangle wins
Risk decompositionriskContributions + paintRiskDecompositionweights and a covariance matrix; the contributions are the Euler decomposition of the variance and add up to one
Scenario conemonteCarloCone + paintMonteCarloConea spec with a seed; the same seed always yields the same cone and the bands are nested
AttributionattributionRowsthe steps of a performance bridge; the rows share the base arithmetic of the waterfall
import { correlationGrid, riskContributions, monteCarloCone } from '@unifin/chart-kit-portfolio';

correlationGrid(['AAPL', 'GLD'], correlations);       // diverging heatmap grid
riskContributions([0.6, 0.4], covariance);            // shares that add up to one
monteCarloCone({ start: 100, drift: 0.0006, volatility: 0.013, steps: 120, seed: 11 });

The cone also has a worker form: monteCarloJob builds a flat payload, monteCarloJobTransferables lists the buffers to transfer and coneFromJob rebuilds the bands on the receiving side, so a long simulation can leave the main thread without changing the numbers.

The screener

The portfolio monitoring board builds its rows from the same positions: market value, weight and profit, with the lots of every position behind a chevron and the P&L colours coming from the kit tones. The locale switch moves the grouping and the decimal separator, the precision stays declared.

Number locale moves the grouping and the decimal separator; the declared precision of every column stays.
Number locale
Portfolio monitoring5 rows
Headless rows of @unifin/chart-kit-portfolio - weights, P&L and tones come from the kit model, the chevron opens the lots
KEGC912,438.0045.2%-612.00-4.69%
BAC406,984.0025.4%256.003.80%
RDGR605,124.0018.6%-222.00-4.15%
KZAP61,629.005.9%21.001.31%
AAPL121,354.804.9%102.008.14%
import { POSITION_COLUMNS, buildPositionRows } from '@unifin/chart-kit-portfolio';
import { filterTableRows, sortTableRows } from '@unifin/chart-core';

const rows = buildPositionRows(positions);   // market value, cost, P&L, P&L %, weight
const sorted = sortTableRows(rows, POSITION_COLUMNS, { key: 'pnl', direction: 'desc' });
const top = filterTableRows(sorted, POSITION_COLUMNS, 'AAPL');

The correlation terrain in 3D

The correlation matrix behind the correlogram also reads as a terrain: every pair of asset classes lifts a ridge by its coefficient, the pairs above the ridge threshold carry a warn stroke and the deep negatives mark pockets in the down tone. The same drag, wheel and keyboard controls steer the camera, the reset button and the two angle presets restore the canonical views, and prefers-reduced-motion keeps the frame static while the controls stay live. The S105 frame sheen washes the whole scene once with a single diagonal pass - never per cell.

Theme
yaw 0.60 · pitch 0.90 · scale 1.00
The correlation terrain: ridge strokes above the 0.60 threshold, pocket markers below -0.35 and the three strongest pairs as chips

The correlation matrix as a 3D terrain

import { correlationTerrainModel, paintCorrelationTerrain } from '@unifin/chart-kit-portfolio/formats';
import { scene3dCamera } from '@unifin/chart-core/scene3d';

const terrain = correlationTerrainModel({ labels, matrix });
paintCorrelationTerrain(ctx, terrain, { width, height, theme, camera: scene3dCamera() });

The kit reference is the Portfolio kit API; the camera is part of the core API.

See also

On this page