demo D210
A trading book in Web Component
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 web component version. A lattice-grid custom element renders the grid with no build step, its configuration set as a property, so it works on a page that has no bundler at all.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<script type="module">
import { defineLatticeGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/webcomponent.esm.min.js';
defineLatticeGrid();
</script>
<lattice-grid row-key="id" style="display: block; height: 520px"></lattice-grid>
<script type="module">
import { registerUnitSystem, createUnitType } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/webcomponent.esm.min.js';
// A money unit family: the pound, with k£ and M£ on the auto ladder. These
// come from the element's own module, so its columns recognise the types.
registerUnitSystem('gbp', [
{ symbol: '£', factor: 1, aliases: ['gbp'], binary: false, prefix: null, auto: true },
{ symbol: 'k£', factor: 1e3, aliases: [], binary: false, prefix: 'k', auto: true },
{ symbol: 'M£', factor: 1e6, aliases: [], binary: false, prefix: 'M', auto: true },
]);
const money = createUnitType({ system: 'gbp', unit: '£', placement: 'prefix', decimals: 2 });
const moneyAuto = createUnitType({ system: 'gbp', unit: '£', placement: 'prefix', display: 'auto' });
const grid = document.querySelector('lattice-grid');
grid.config = {
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, maintained by the grid as the book moves.
{ 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: 'climb', title: 'Moved', shadow: { of: 'pnl', kind: 'rankChange' }, type: 'number', format: { signed: true } },
{ 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: 'climb', dir: 'desc' }] },
};
grid.rows = rows; // trading book records
</script>