Build notes
Lightweight Charts v5 Migration: addSeries, a Null From timeToCoordinate, and Late Bars
Batdorj Byambadorj · · 4 min read
I planned a forex chart against Lightweight Charts v4 and built it on v5. Three things stopped me on the way. Only the first is a migration break in the strict sense. The other two are what cost the time, so they are here as well.
1. addCandlestickSeries no longer exists
Every v4 example creates a series with a method named after its type. In v5 that method is gone. There is one addSeries, and the type is passed in.
// v4
const series = chart.addCandlestickSeries(options);
// v5
import { createChart, CandlestickSeries } from 'lightweight-charts';
const series = chart.addSeries(CandlestickSeries, options);
The package is also ESM only now, which matters if your build still expects CommonJS.
This one is quick once you know it. Many examples online are still written for v4, so it helps to check the version before copying one.
2. timeToCoordinate returns null, and on forex that is normal
timeToCoordinate turns a time into an x position. It answers only when a bar exists at exactly that time. Otherwise it returns null.
On a market that trades around the clock you can go a long way without noticing. On forex I hit it in three places.
Session bands. I shaded the Asian, London and New York sessions behind the candles. The forex day turns over at about 21:00 UTC, and there are no bars around that moment. A band that ends there has an edge with no coordinate.
Lines across timeframes. A trendline anchor placed on an M15 chart lands between two bars when the chart is switched to M30. The time is valid. There is simply no bar that starts on it.
Handles past the last bar. A line's right anchor is often dragged out ahead of price, where no bar exists yet. I confirmed in the 5.2.1 source that a time which falls between bars gets null.
The fix for the first two is a helper that walks from the requested time, in 60 second steps, to the nearest bar that does exist, and uses that bar's coordinate. For session bands it walks towards the session's own limit, so the band never spills outside its hours. For handles I stopped using times and placed them by logical index.
What I did not do is interpolate between the two neighbouring bars. It looks like the obvious repair and it is wrong on this kind of chart. A forex chart is not linear in time. The weekend takes up no width at all, and a gap in the bars is not a stretch of empty pixels. Half the time between two bars is not half the distance between them. The same fact later produced a much more expensive bug.
3. Bars that arrive after you have switched timeframe
The chart received live bars over a WebSocket, one channel for each symbol and timeframe. Switching from M5 to H1 meant unsubscribing from one channel and subscribing to another.
The unsubscribe is not instant. A bar from the old channel can already be on its way, and it arrives after the series has been refilled with H1 data. The library then refuses it with a duplicate time error.
The fix is one comparison before any update is applied. Every message carries its channel. The handler compares it with the current subscription and drops anything else.
// in outline
if (msg.ch !== currentChannel.current) return; // late bar from the old timeframe
series.update(msg.bar);
The current channel lives in a ref. The handler was created while the old timeframe was selected, and a ref is how it sees the new one.
Smaller things from the same weeks
- There are no drawing tools in the library. Draggable lines are yours to build. Mine were an HTML and SVG layer on top of the chart.
- A series draws its own last value label by default. Next to my bid and ask labels it looked like a duplicate price.
- Compute a line's slope from its own two points, never from the visible range. The range changes on every scroll, and on M1 the angle jumps.
- Do not place a marker by flooring the Unix time to the timeframe. For W1 and MN it is wrong: 1 January 1970 was a Thursday, and months are not a fixed length.
That chart is offline now, for reasons that had nothing to do with the library.
The short version
- v5:
chart.addSeries(CandlestickSeries, options), and an ESM build. - Treat
nullfromtimeToCoordinateas a normal answer. Walk to the nearest real bar. - Never interpolate x positions by time on a chart that has gaps.
- Tag every live message with its channel and drop the ones that are not current.
Batdorj Byambadorj
Batdorj Byambadorj has been trading forex for his own account since 2007, mostly gold. He is the sole developer of fx-alert and writes every article on this site himself.