開源圖表引擎
移動停損與清算價
10 倍槓桿的多頭部位(以回放 K 線模擬)。停損起初位於進場價下方 4%,獲利達 1.5% 後改為跟隨最高價、保持在其下方 3%;逐倉保證金的清算價是一條橘色實線。價格觸及停損時平倉,顯示成交標記與損益,隨後重新開倉。引擎不傳送任何資料。
移動停損與清算價:執行與交易檢討範例
trailingStop · liquidationPrice · createTradingLines · tradeMarkers
程式碼
import { FerroChart } from '@ferrochart/core';
import { createTradingLines, trailingStop, liquidationPrice, tradeMarkers } from '@ferrochart/core/trading';
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=BTCUSD&interval=1h&count=400').then((r) => r.json());
// Bar replay with setData and update: all but the last 40 bars, then each
// step() moves the forming bar a little; it closes on its 4th tick. At the end
// of the data the replay starts over (restart() resets the example).
const n = bars.time.length, START = n - 40, TICKS = 4;
const head = (k) => Object.fromEntries(Object.entries(bars).map(([key, v]) => [key, v.slice(0, k)]));
const candles = chart.addCandlestickSeries();
candles.setData(head(START));
let next = START, tick = 0, forming = null;
function step() {
if (next === n) { next = START; candles.setData(head(START)); restart(); }
const i = next, t = ++tick, closed = t === TICKS, o = bars.open[i];
const noise = (Math.random() - 0.5) * (bars.high[i] - bars.low[i]) * 0.5;
const price = closed ? bars.close[i] : Math.round((o + (bars.close[i] - o) * t / TICKS + noise) * 10) / 10;
if (t === 1) forming = { time: bars.time[i], open: o, high: o, low: o };
forming = { ...forming, high: Math.max(forming.high, price), low: Math.min(forming.low, price), close: price, volume: bars.volume[i] * t / TICKS };
candles.update(forming);
if (closed) { next++; tick = 0; }
return { time: bars.time[i], price, closed };
}
const FROM = START - (canvas.clientWidth < 480 ? 30 : 80);
chart.setVisibleLogicalRange(FROM, n + 3);
// Paper trading: a simulated long of 0.5 BTC at 10x that opens and closes in
// this page; nothing is sent anywhere. A real page modifies its stop order on
// the exchange each time the trailing stop moves.
const QTY = 0.5;
const ui = (tag, css, parent = canvas.parentElement) => parent.appendChild(Object.assign(document.createElement(tag), { style: css }));
const note = ui('div', 'position:absolute;left:12px;top:10px;right:84px;font:500 12px system-ui;color:#c9d1e0;z-index:2;pointer-events:none');
const lines = createTradingLines(chart, candles);
const fills = [];
let trail = null, entry = 0, liq = null, wait = 0;
const show = () => candles.setMarkers(tradeMarkers(fills, { times: bars.time }));
function open(time, price) {
entry = price;
// 4% below the entry, then 3% below the best price once it is 1.5% up.
trail = trailingStop({ side: 'buy', percent: 3, activation: price * 1.015, stop: Math.floor(price * 0.96), priceStep: 1 });
liq = liquidationPrice({ side: 'buy', entry: price, quantity: QTY, margin: (price * QTY) / 10, maintenanceRate: 0.005 });
fills.push({ time, price, quantity: QTY, side: 'buy' });
lines.set({ id: 'entry', kind: 'position', price, quantity: QTY });
lines.set({ id: 'sl', kind: 'stopLoss', price: trail.stop, title: 'Stop' });
lines.set({ id: 'liq', kind: 'liquidation', price: liq, title: '10x liquidation' });
note.textContent = `Paper long ${QTY} BTC at ${price.toFixed(1)}, 10x. The stop trails 3% after a 1.5% gain.`;
show();
}
function exit(time, price) {
fills.push({ time, price, quantity: QTY, side: 'sell' });
const pnl = (price - entry) * QTY, margin = (entry * QTY) / 10;
note.textContent = `Stopped out at ${price.toFixed(1)}: P&L ${pnl < 0 ? '−' : '+'}$${Math.abs(pnl).toFixed(0)} (${(pnl / margin * 100).toFixed(1)}% of the margin). Reopening…`;
for (const id of lines.ids()) lines.remove(id);
trail = liq = null;
wait = 8; // ticks until the next entry
show();
}
function onPrice({ time, price }) {
if (!trail) {
if (--wait <= 0) open(time, price);
return;
}
if (price <= trail.stop) return exit(time, price);
const moved = trail.update(price);
if (moved !== null) lines.set({ id: 'sl', kind: 'stopLoss', price: moved, title: `Trailing stop ${moved}` });
}
// The scale keeps the liquidation price (about 9.5% below the entry) in view.
function fit() {
const low = Math.min(liq ?? Infinity, ...bars.low.slice(FROM, next + 1)), high = Math.max(...bars.high.slice(FROM, next + 1));
chart.applyPriceScaleOptions({ range: { low: low * 0.995, high: high * 1.005 } });
}
// The first position opened 60 bars ago: the same logic over those closes.
for (let i = START - 60; i < START; i++) onPrice({ time: bars.time[i], price: bars.close[i] });
fit();
function restart() { fills.length = 0; for (const id of lines.ids()) lines.remove(id); trail = liq = null; wait = 0; show(); }
const timer = setInterval(() => { onPrice(step()); fit(); }, 600);
// The page calls this before it destroys the chart.
const stop = () => { clearInterval(timer); lines.destroy(); note.remove(); };為逐筆等級的資料而生。
Apple M4 Max · Chrome + WebGPU在瀏覽器中載入 100 萬根 K 線並繪出第一個畫面
WASM · WebGPU
原生載入 1,000 萬根 K 線,含多層級細節金字塔
原生 Rust
平移、縮放與即時串流時每個畫面的堆積記憶體配置次數
穩定狀態
WebAssembly 套件大小(gzip)
包含全部功能
瀏覽器數據:Google Chrome + WebGPU;1,000 萬根 K 線的載入為原生 Rust。長條表示各項數據相對於目標的比例:載入 300 毫秒、零配置、套件 1.5 MB。
範例
320 個範例,
涵蓋 20 個應用情境。
每個範例都是 FerroChart 在你的瀏覽器中即時渲染的圖表,並附上原始碼。所有資料均為模擬資料。



價格走勢
28 個範例 →適用於任何市場的 K 線、棒線與折線,即時串流更新。



訂單流
20 個範例 →足跡圖、深度熱力圖與市場輪廓,走 compute shader 路徑。



加密貨幣衍生性商品
15 個範例 →資金費率、未平倉量、清算與基差,顯示在價格下方的窗格中。



選擇權與波動率
18 個範例 →隱含波動率與實現波動率對照、偏斜、預期波動幅度與造市商曝險的時間變化。



投資組合、風險與量化
24 個範例 →權益曲線、回撤、滾動統計、因子研究與風險檢查。



自訂系列與外掛
11 個範例 →堆疊、分組、箱形與筆刷選取系列:在同一個 GPU 影格上疊加圖層外掛。
瀏覽全部 20 個應用情境
價格走勢
K 線類型與轉換
技術分析
訂單流
執行與交易檢討
加密貨幣衍生性商品
鏈上資料
DeFi 與 AMM
迷因幣與錢包
股票與基本面
中國 A 股
選擇權與波動率
利率、外匯與總體經濟
期貨與大宗商品
基金與 ETF
投資組合、風險與量化
私募市場與個人理財
預測市場
自訂系列與外掛
實驗室與旗艦範例
K 線 + 成交量
1 / 28The 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