Drawing trading indicators with one codebase in a browser extension and on a website
We have a browser extension that calculates technical indicators and draws them over a trading platform's chart: candles, ribbons of averages, waves, zones, marks. Later we needed a website where the same indicators can
We have a browser extension that calculates technical indicators and draws them over a trading platform's chart: candles, ribbons of averages, waves, zones, marks. Later we needed a website where the same indicators can be explored without installing anything: a live chart, explanations on click, a settings window. And a catalogue of candlestick patterns with examples on real candles.
We did not want a second implementation for the website for a simple reason: two implementations drift apart. A month later a mark on the website sits somewhere other than in the extension, and users fairly ask which one to trust. Here is how we arranged things so there is nothing to drift.
The indicator does not draw, it describes the picture
The key decision: an indicator knows nothing about the canvas. It takes candles and settings and returns a list of primitives β a description of the picture:
type PaneShape =
| { kind: 'line'; points: Series; color: number; width: number; colorAt?: ColorAt }
| { kind: 'band'; upper: Series; lower: Series; color: number; alpha: number }
| { kind: 'area'; values: Series; base: number; color: number; alpha: number }
| { kind: 'hist'; values: Series; colorAt: (i: number) => number }
| { kind: 'level'; value: number; color: number; dashed?: boolean }
| { kind: 'dots'; at: { i: number; v: number; r: number; color: number }[] }
| { kind: 'segment'; from: { i: number; v: number }; to: { i: number; v: number }; color: number; width: number }
| { kind: 'candles'; at: { i: number; o: number; h: number; l: number; c: number; color: number; frame?: number }[] }
// β¦marks, labels, zones
An indicator has one function, shapes(candles, settings): PaneShape[]. Colour and opacity arrive as data rather than being baked into drawing code. The set of primitives grew out of the most complex indicator β a pane with two waves, a fill between them, money flow, levels, signal marks and divergence lines. Once it fit into eight kinds, the rest needed no new ones.
What this gives:
-
the renderer decides nothing on X β it is handed an
xAt(i)function. In the extension it follows the platform chart's scale, on the website β its own scroll and zoom; -
on Y values are mapped linearly from
[min, max]to the canvas height.undefinedin a series means the indicator is not warmed up yet, and the line breaks; - tests are written against data, not pixels: "this bar has a mark of this colour".
Rendering: PIXI with a Canvas 2D fallback
PaneRenderer draws all of this with PIXI. We first used the regular PIXI 7 build, and for one user the pane stayed empty: their machine had no WebGL, and the regular build simply fails without it. We switched to pixi.js-legacy: the same API, and when WebGL is unavailable it falls back to Canvas 2D on its own. The cost is about 60 KB of bundle. The alternative β our own Canvas 2D renderer next to PIXI β would have meant a second implementation of every primitive and keeping them in sync forever. Exactly the problem we were running away from.
The website imports the same code
The chart widget on the website imports three things from the extension's sources: the renderer, the indicator shape calculation and the hint geometry.
import { PaneRenderer } from '@/bot/ui/widgets/paneRenderer';
import { paneBoundsWithMarks, paneHintAt } from '@/bot/ui/widgets/paneGeometry';
import { candleShape, ownShapes } from '@/bot/indicators/own/paneShapes';
The website owns only the shell: page scrolling with the wheel in the small view, an expanded view on click, drag and zoom, a settings window behind β. Changing a setting recalculates the shapes with the same code and saves nothing.
Texts are shared too. The explanation shown on clicking a mark is built by the same functions from the same dictionaries as in the extension, in the page's language. The English page shows English hints without a single translated string on the website side.
Candles are real. For each indicator a 600-bar stretch without gaps is exported from the archive of one-minute candles as JSON, [[time, open, high, low, close], β¦]. The stretch is chosen so the indicator has something to show: about a dozen marks in the visible window and a good variety of kinds. It is pinned in a source file, so re-exporting gives the same data.
Home page images β the same calculation, no browser extension needed
A live chart is great in an article, but the home page needs a fast image. A stand β a test run by hand β makes it:
- takes a stretch of candles and calculates the shapes with the same
shapes(); - turns the primitives into SVG β only the canvas changes, the calculation is the same;
- opens the SVG in headless Chrome and takes a screenshot;
- compresses it to WebP.
Need the same frame with English labels? The stand switches the dictionary language and shoots again from the pinned stretch. The image on the home page and the live chart under it show the same candles.
Candlestick patterns: the card cannot lie
In the website's pattern catalogue each pattern has three parts: a reference drawing, the latest example found on real candles, and the conditions. All of it comes from the extension's code:
- the reference β candles from the examples file. An extension test requires every example to be recognised as its own pattern. You cannot draw a "hammer" on the website that the bot does not consider a hammer;
- the real example β the latest occurrence in the archive, found by the same detector that draws the frames in the extension. No occurrence β no image;
- the conditions β the fields of the pattern definition, the same ones the user sees in the description window.
Next to it is a builder for your own pattern with the same fields as the extension window: colour, body, wicks, size, body overlap. If such a candle cannot be drawn (a body larger than the range, a wick with a "full" body), the builder names the reason in the same words as the extension.
What we ended up with
- One indicator implementation for the extension, the website's live chart, home page images and the catalogue.
- A fix in the calculation shows up everywhere, and a test catches a mismatch before release.
- The website cannot show what the extension would not, and the other way round.
There is a price: the website is built from the extension's sources, and changing an indicator's interface touches both builds. We decided that is cheaper than explaining to users why the pictures differ.
If you want to poke at the live chart described here β drag, zoom, click a mark, change a setting: 7tra.de/en/wiki/cipher-b.
Questions about the renderer or the primitives are welcome in the comments.
This article was written with the help of AI. The code samples and the decisions are from our project and were checked against the repository.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.
