Yield curve chart with update markers
Chart types
Two yield curves updating live, with markers that show each change.
Interactive example — scroll, zoom, and hover the chart.
Code
// Lightweight Charts™ Example: Yield Curve Chart with Update Markers
// https://tradingview.github.io/lightweight-charts/examples/chart-types/yield-curve-with-update-markers
const chartOptions = {
autoSize: true,
layout: {
textColor: 'black',
background: { type: 'solid', color: 'white' },
},
yieldCurve: {
baseResolution: 12,
minimumTimeRange: 10,
startTimeRange: 3,
},
handleScroll: false,
handleScale: false,
grid: {
vertLines: {
visible: false,
},
horzLines: {
visible: false,
},
},
timeScale: {
minBarSpacing: 3,
},
};
const container = document.getElementById('container');
const chart = createYieldCurveChart(container, chartOptions);
const series1 = chart.addSeries(LineSeries, {
lineType: 2,
color: '#26c6da',
pointMarkersVisible: true,
lineWidth: 2,
});
const priceChangeMarkers = createUpDownMarkers(series1);
priceChangeMarkers.setData(curve1);
const series2 = chart.addSeries(LineSeries, {
lineType: 2,
color: 'rgb(164, 89, 209)',
pointMarkersVisible: true,
lineWidth: 1,
});
series2.setData(curve2);
chart.timeScale().fitContent();
chart.timeScale().subscribeSizeChange(() => {
chart.timeScale().fitContent();
});
setInterval(() => {
curve1
.filter(() => Math.random() < 0.1)
.forEach(data => {
const shift = (Math.random() > 0.5 ? -1 : 1) * Math.random() * 0.01 * data.value;
priceChangeMarkers.update(
{
...data,
value: data.value + shift,
},
true
);
});
}, 5000);
The code expects the library’s exports, such as createChart, to be imported and a div with the id container on the page. Copy as HTML gives a page that runs on its own.
How it works
A yield curve chart has durations, in months, on the horizontal scale instead of dates. This one shows two curves and marks each change to the first one.
createYieldCurveChartcreates the chart.- Two line series are added with
lineType: 2for curved lines andpointMarkersVisible: trueto mark each point. createUpDownMarkersis attached to the first series. ItssetDatasets the initial curve.- Every five seconds a few points of the first curve change. The markers'
updateapplies each change and shows an up or down marker on the point.
Tips
- Pass the data through the markers'
setDataandupdate, not through the series directly. Otherwise the markers cannot tell what changed.