demo D210
A trading book in htmx
Money units, shadow columns, a running P&L and a live feed, in one grid
createUnitType · shadow · running · statistics
This is a trading book in one grid: money units, shadow columns, a running profit and loss, and a live feed. It shows the statistics and type system working together on a realistic financial screen that keeps updating.
This is the htmx version. A server-rendered table is upgraded in place, so the page is readable before any script runs and gains the full grid when the module loads, which suits a progressively enhanced app.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<!-- A real, server-rendered table: readable with JavaScript off. data-field on
each header names the column that cell becomes, and data-id on each row is
the key the grid carries it under. -->
<table id="finance-desk-table" data-lattice-grid>
<thead>
<tr>
<th data-field="instrument">Instrument</th>
<th data-field="desk">Desk</th>
<th data-field="notional">Notional</th>
<th data-field="price">Price</th>
<th data-field="spread">Spread</th>
<th data-field="pnl">P&L</th>
</tr>
</thead>
<tbody>
<tr data-id="INS-0000000"><td>ELMW.G0</td><td>Rates</td><td>110618217</td><td>462.0258</td><td>2.9</td><td>0</td></tr>
<tr data-id="INS-0000001"><td>RVNS.A1</td><td>Credit</td><td>28868041</td><td>241.8711</td><td>11.9</td><td>0</td></tr>
<tr data-id="INS-0000002"><td>NRTH.E2</td><td>Commodities</td><td>15857773</td><td>98.9614</td><td>9.9</td><td>0</td></tr>
<!-- the rest of the rows, rendered by your server -->
</tbody>
</table>
<script type="module">
import { hydrateTable, registerUnitSystem, defineUnit, createUnitType } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/htmx.esm.min.js';
// A money unit family: the pound, with thousands and millions on the auto ladder.
registerUnitSystem('gbp', [
defineUnit('£', 1, ['gbp']),
defineUnit('k£', 1e3, [], { prefix: 'k' }),
defineUnit('M£', 1e6, [], { prefix: 'M' }),
]);
const money = createUnitType({ system: 'gbp', unit: '£', placement: 'prefix', decimals: 2 });
const moneyAuto = createUnitType({ system: 'gbp', unit: '£', placement: 'prefix', display: 'auto' });
// The markup carries the rows; a server-rendered table's column types are
// declared in the config hydrateTable takes. The grid replaces the table
// where it stood.
const grid = hydrateTable(document.getElementById('finance-desk-table'), {
highlightOnChange: { duration: 500 },
dataTypes: { money, moneyAuto },
toolPanel: { side: 'left', panels: ['columns', 'filters', 'statistics'] },
columns: [
{ field: 'instrument', title: 'Instrument', layout: { pin: 'start', width: 120 } },
{ field: 'desk', title: 'Desk', filter: { type: 'set' } },
{ field: 'notional', title: 'Notional', type: 'moneyAuto', total: 'sum' },
{ field: 'price', title: 'Price', type: 'money' },
{ field: 'spread', title: 'Spread', type: 'number', format: { decimals: 1, suffix: ' bp' }, total: 'median' },
{ field: 'pnl', title: 'P&L', type: 'money', total: 'sum' },
// Shadow and running columns, kept up to date by the grid: the move since
// the last price, the rank by P&L, each book's share of the total, and a
// running P&L down the sorted rows.
{ id: 'moved', title: 'Change', shadow: { of: 'price', kind: 'delta' }, type: 'number', format: { signed: true } },
{ id: 'rank', title: 'Rank', shadow: { of: 'pnl', kind: 'rank' }, type: 'number' },
{ id: 'share', title: 'Share %', shadow: { of: 'notional', kind: 'shareOfTotal' }, type: 'number', format: { style: 'percent', decimals: 1 } },
{ id: 'cum', title: 'Cum. P&L', running: { of: 'pnl', kind: 'total' }, type: 'money' },
],
formatting: {
pnl: [{ when: { op: 'lt', value: 0 }, style: { color: '#a4262c' } }],
},
state: { sort: [{ col: 'pnl', dir: 'desc' }] },
});
</script>