demo D182
Correlation and weighted averages
Two-column figures from the API, over the filtered rows
grid.statistics.correlation(a, b)
Loading a live grid…
The configuration
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.0/lattice-grid.min.css">
<div id="app"></div>
<script type="module">
import React from 'https://esm.sh/react@18';
import { createRoot } from 'https://esm.sh/react-dom@18/client';
import { createGrid } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.0/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.0/modules/react.esm.min.js';
const LatticeGrid = createLatticeGrid({ React, createGrid });
const columns = [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'desk', title: 'Desk', filter: { type: 'set' } },
{ field: 'price', title: 'Price', type: 'number', format: { decimals: 4 } },
{ field: 'bid', title: 'Bid', type: 'number', format: { decimals: 4 } },
{ field: 'volume', title: 'Volume', type: 'number', format: { notation: 'compact' } },
];
const rows = [/* trading book records */];
function App() {
const gridRef = React.useRef(null);
const [out, setOut] = React.useState('');
// Two-column figures read straight from the live grid through the ref:
// correlation and a regression between two prices, and a volume-weighted
// price. Every figure is over the filtered rows, so filter a desk to see
// them move.
const report = () => {
const grid = gridRef.current && gridRef.current.grid;
if (!grid) return;
const s = grid.statistics;
setOut(JSON.stringify({
correlation: s.correlation('price', 'bid'),
regression: s.regression('price', 'bid'),
weighted: s.weightedAverage('price', 'volume'),
}, null, 2));
};
React.useEffect(report, []);
return (
<>
<LatticeGrid
ref={gridRef}
rowKey="id"
toolPanel={{ side: 'left', panels: ['filters', 'columns'], exportName: 'book' }}
columnDefaults={{ filter: true }}
columns={columns}
rows={rows}
onFilterChanged={report}
style={{ height: '460px' }}
/>
<pre style={{ marginTop: '12px' }}>{out}</pre>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>