开源图表引擎
LP 风险画布
在一张图上展示一个头寸及其风险。集中流动性区间是从入场 K 线开始的色带;10 倍空单对冲用价格线标出开仓价和强平价。开仓、每次离开和回到区间,以及价格触及时的强平,都是画在确切价格上的 atPrice 标记。第二个窗格显示头寸相对于持有代币的表现(无常损失)和对冲后的盈亏,未计手续费:按初始 ETH 数量做空后,剩下的大致就是无常损失。
LP 风险画布:DeFi 与 AMM示例
position: 'atPrice' · addBand · createPriceLine · addSeries('baseline')
代码
import { FerroChart } from '@ferrochart/core';
const canvas = document.querySelector('canvas');
// Market data: the synthetic FerroChart demo API. Use your own feed.
const chart = await FerroChart.create(canvas, { theme: 'dark' });
const bars = await fetch('/api/bars?symbol=ETHUSD&interval=1h&count=720').then((r) => r.json());
const candles = chart.addCandlestickSeries();
candles.setData(bars);
const n = bars.time.length;
// An ETH/USDC concentrated-liquidity position opened 12 days ago, ±8% around
// the entry, hedged two hours later with a 10x short of its ETH.
const entry = n - 288, p0 = bars.close[entry], pa = p0 * 0.92, pb = p0 * 1.08;
const amounts = (p) => { // ETH and USDC of one unit of liquidity at price p
const s = Math.sqrt(Math.min(Math.max(p, pa), pb));
return [1 / s - 1 / Math.sqrt(pb), s - Math.sqrt(pa)];
};
const [x0, y0] = amounts(p0);
const hedgeAt = entry + 2, ph = bars.close[hedgeAt], margin = (x0 * ph) / 10;
const liq = ph * (1 + 1 / 10 - 0.005); // maintenance margin 0.5%
// The range as a band from the entry bar; the short's entry and liquidation as price lines.
const edge = (price) => bars.time.map((t, i) => (i < entry ? NaN : price));
const style = { color: '#7ee787', lineWidth: 1, lineStyle: 'dashed', priceLineVisible: false, lastValueVisible: false };
const hi = chart.addSeries('line', style), lo = chart.addSeries('line', style);
hi.setData({ time: bars.time, value: edge(pb) });
lo.setData({ time: bars.time, value: edge(pa) });
chart.addBand(hi, lo, { fill: 'rgba(126, 231, 135, 0.08)' });
candles.createPriceLine({ price: ph, color: '#b388ff', title: 'short entry' });
candles.createPriceLine({ price: liq, color: '#ff5d73', lineStyle: 'solid', lineWidth: 2, title: 'short liquidation' });
// Events at the price they happened: the opens, the range exits and returns, a liquidation.
const at = (i, price, color, text) => ({ time: bars.time[i], position: 'atPrice', price, shape: 'circle', size: 9, color, text });
const markers = [at(entry, p0, '#7ee787', 'LP open'), at(hedgeAt, ph, '#b388ff', 'short')];
const lp = [], net = [];
let zone = 0, streak = 0, liquidated = false; // zone: -1 below the range, 0 in it, 1 above
for (let i = 0; i < n; i++) {
if (i < entry) { lp.push(NaN); net.push(NaN); continue; }
const p = bars.close[i], [x, y] = amounts(p);
if (i >= hedgeAt && !liquidated && bars.high[i] >= liq) { liquidated = true; markers.push(at(i, liq, '#ff5d73', 'liquidated')); }
const now = p > pb ? 1 : p < pa ? -1 : 0;
streak = now === zone ? 0 : streak + 1; // a new zone counts after 12 closes
if (streak === 12) {
const j = i - 11, edge = (now || zone) > 0 ? pb : pa;
markers.push(now ? at(j, edge, '#ffb547', 'out of range') : at(j, edge, '#2ee6b6', 'back in range'));
zone = now;
streak = 0;
}
const hedge = i < hedgeAt ? 0 : liquidated ? -margin : x0 * (ph - p);
lp.push(((x * p + y) / (x0 * p + y0) - 1) * 100); // against holding the tokens
net.push(((x * p + y + hedge) / (x0 * p0 + y0) - 1) * 100); // LP and short, of the capital
}
candles.setMarkers(markers.sort((a, b) => a.time - b.time));
// Second pane: what the position earns, before fees.
const pane = chart.addPane(0.5);
chart.addSeries('baseline', { baseValue: 0, color: '#2ee6b6', downColor: '#ff5d73', topFill: 'rgba(46, 230, 182, 0.2)', bottomFill: 'rgba(255, 93, 115, 0.2)', pricePrecision: 2 }, pane)
.setData({ time: bars.time, value: net });
chart.addSeries('line', { color: '#ffb547', lineWidth: 1.5, pricePrecision: 2 }, pane).setData({ time: bars.time, value: lp });
chart.applyPriceScaleOptions({ margins: { top: 0.22, bottom: 0.05 } }, pane);
chart.addLayer({ pane, build: (ctx) => [
{ type: 'text', x: ctx.rect.x + 8, y: ctx.rect.y + 6, text: 'LP + short, % of capital', color: '#2ee6b6', size: 11 },
{ type: 'text', x: ctx.rect.x + 8, y: ctx.rect.y + 22, text: 'LP vs holding (impermanent loss), %', color: '#ffb547', size: 11 },
] });
chart.setVisibleLogicalRange(entry - 40, n + 4);为逐笔级数据而生。
Apple M4 Max · Chrome + WebGPU在浏览器中载入 100 万根 K 线并画出首帧
WASM · WebGPU
原生载入 1000 万根 K 线,含多级细节金字塔
原生 Rust
平移、缩放和实时推送时每帧的堆分配次数
稳定状态
WebAssembly 包体积(gzip)
包含全部功能
浏览器数据:Google Chrome + WebGPU;1000 万根 K 线的载入为原生 Rust。进度条表示各项数据相对于目标的比例:载入 300 毫秒、零分配、安装包 1.5 MB。
示例
313 个示例,
覆盖 20 个应用场景。
每个示例都是 FerroChart 在你的浏览器中实时渲染的图表,并附带源代码。所有数据均为模拟数据。



价格走势
25 个示例 →适用于任何市场的 K 线、美国线和折线,实时流式更新。



订单流
20 个示例 →足迹图、深度热力图和市场轮廓,走计算着色器路径。



加密衍生品
15 个示例 →资金费率、持仓量、清算和基差,显示在价格下方的窗格中。



期权与波动率
18 个示例 →隐含波动率与已实现波动率、偏斜、预期波动幅度和做市商敞口随时间的变化。



组合、风险与量化
24 个示例 →净值曲线、回撤、滚动统计、因子研究和风险检查。



自定义系列与插件
11 个示例 →堆叠、分组、箱线和刷选系列:在同一 GPU 帧上叠加图层插件。
浏览全部 20 个应用场景
价格走势
K 线类型与变换
技术分析
订单流
执行与交易复盘
加密衍生品
链上数据
DeFi 与 AMM
Meme 币与钱包
股票与基本面
A 股
期权与波动率
利率、外汇与宏观
期货与大宗商品
基金与 ETF
组合、风险与量化
私募市场与个人理财
预测市场
自定义系列与插件
实验室与旗舰示例
K 线 + 成交量
1 / 25The classic view. A day of real one-minute bars, updating every second, on a GPU-rendered price scale with a volume histogram below. Drag to pan, scroll or pinch to zoom.
FerroChart.createaddCandlestickSeriesaddVolumeSeries拖动平移 · 滚轮或双指缩放 · 双击复位实时数据 · OKXcandles
特性
为行情数据而设计。
跨平台渲染
同一个 Rust 内核:浏览器中使用 WebGPU 和 WebGL2 后端,原生应用中通过 wgpu 运行在 Metal、Vulkan 和 DirectX 12 上,服务器上使用 CPU 渲染器。黄金图像测试保证各后端输出一致。
性能与数据量无关
K 线数据常驻 GPU,坐标变换在着色器中完成,平移和缩放只需更新少量 uniform。多级细节金字塔让数千万根 K 线依然流畅。
30 个内置指标
从 SMA 到一目均衡表的均线、通道和震荡指标。每个 tick 增量更新,并用参考数据测试。
29 种画线工具
趋势线、通道、斐波那契工具、叉子线、江恩角度线、形态、艾略特波浪、区间、仓位和文字,支持撤销与重做。画线和图表状态可序列化为 JSON。
多图联动
同步组可在多个图表之间联动十字光标和可见范围。
易于扩展
自定义系列、图层和画线工具的插件接口。提供 JavaScript、Rust 和 Python 绑定,以及供 C#、C++、Qt 和 Flutter 使用的 C ABI。
开始使用
每种语言,同一套 API。
所有绑定遵循同一模型:创建图表、添加系列、设置数据、添加指标。数据按列传入,可以是类型化数组、Rust 切片或 DataFrame。
- JavaScript 与 TypeScript
- Rust
- Python 与 Jupyter
- C、C#、C++
import { FerroChart } from '@ferrochart/core';
const chart = await FerroChart.create(canvas, { theme: 'dark' });
const candles = chart.addCandlestickSeries();
chart.addVolumeSeries(candles);
candles.setData(bars); // [{ time, open, high, low, close, volume }, ...]
chart.addIndicator(candles, 'ema', { n: 20 });
chart.addIndicator(candles, 'macd'); // in a new pane