demo D194
Variation along an ordering: JavaScript Data Grid Demo
Volatility, growth and drawdown, from rows loaded out of order
grid.statistics.series(col, { by })
This grid computes volatility, growth and drawdown along an ordering, even from rows loaded out of order. It reads a series' behaviour over its own sequence, so trend and swings surface without the data arriving pre-sorted.
Loading a live grid…
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.css">
<script src="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.js"></script>
<div id="grid" style="height: 420px"></div>
<div id="panel" style="margin-top: 12px; font: 13px system-ui; display: flex; flex-wrap: wrap; gap: .4rem 1.4rem"></div>
<script>
const grid = LatticeGrid.createGrid(document.getElementById('grid'), {
rowKey: 'id',
columns: [
{ field: 'date', title: 'Date', type: 'dateString', layout: { pin: 'start', width: 150 } },
{ field: 'price', title: 'Price', type: 'number', format: { decimals: 2 } },
],
rows, // one row a day, loaded in any order: date, price
});
const panel = document.getElementById('panel');
const render = () => {
// series sorts by the ordering column before it reads, so volatility and
// drawdown are right even when the rows arrive out of date order.
const s = grid.statistics.series('price', { by: 'date', periodsPerYear: 252 });
const pct = (v) => (v * 100).toFixed(1) + '%';
panel.innerHTML =
'<span><strong>annualised volatility</strong> ' + pct(s.annualisedVolatility) + '</span>' +
'<span><strong>growth</strong> ' + pct(s.growth) + '</span>' +
'<span><strong>max drawdown</strong> ' + pct(s.maxDrawdown) + '</span>' +
'<span><strong>up / down days</strong> ' + s.upDays + ' / ' + s.downDays + '</span>';
};
grid.on('filter:changed', render);
render();
</script>