圖表交易

開源圖表引擎

圖表交易

直接在圖上交易(模擬)。選取多頭或空頭部位,填寫風險金額,數量會依到停損的距離算出;點 Place order,頁面會收到一份意圖,包含方向、進場、目標、停損和數量。下單後的部位以價格線表示,拖曳停利或停損時會先詢問頁面:被拒絕就退回原位。引擎不連接任何交易所,也不傳送任何資料。

Apache-2.0

圖表交易:執行與交易檢討範例

createTradeTicket · createTradingLines · priceLineDrag · drawings.add('long_position')

程式碼
import { FerroChart } from '@ferrochart/core';
import { createTradeTicket, createTradingLines } 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=15m&count=600').then((r) => r.json());
const candles = chart.addCandlestickSeries();
candles.setData(bars);
const n = bars.time.length, entry = bars.close[n - 1];
// Paper trading: the order "fills" in this page and goes nowhere else. A real
// page sends the intent to its own server here.
const ui = (tag, css, parent = canvas.parentElement) => {
  const e = document.createElement(tag);
  e.style.cssText = css;
  parent.append(e);
  return e;
};
const BTN = 'font:600 12px system-ui;color:#fff;border:0;border-radius:5px;padding:7px 14px;cursor:pointer;white-space:nowrap;pointer-events:auto';
const bar = ui('div', 'position:absolute;left:12px;top:10px;right:84px;display:flex;flex-wrap:wrap;gap:8px;align-items:center;z-index:2;pointer-events:none');
const buy = ui('button', BTN + ';background:#26a69a', bar);
const sell = ui('button', BTN + ';background:#ef5350', bar);
buy.textContent = 'Buy / Long';
sell.textContent = 'Sell / Short';
const note = ui('span', 'font:500 12px system-ui;color:#8b93a7;margin-left:4px', bar);
const say = (text) => { note.textContent = `Paper trading · ${text}`; };

// The panel of the filled position, with a Close button.
const panel = ui('div', 'margin-left:auto;display:none;gap:10px;align-items:center;white-space:nowrap;' +
  'padding:4px 5px 4px 12px;border-radius:8px;background:rgba(30,34,45,.96);border:1px solid #363a45;' +
  'font:12px system-ui;color:#d1d4dc;font-variant-numeric:tabular-nums', bar);
const info = ui('span', '', panel);
const close = ui('button', BTN + ';background:#363a45;padding:5px 10px', panel);
close.textContent = 'Close position';

// Wide: controls over the chart, with room below the bars for the ticket.
// Narrow (phones): the chart leaves a strip above it for the buttons and one
// below it, where the ticket docks and the position panel goes.
const host = canvas.parentElement, css = canvas.style.cssText;
let narrow = null;
function layout() {
  const now = host.clientWidth < 480;
  if (now === narrow) return;
  narrow = now;
  canvas.style.cssText = css + (now ? ';margin-top:76px;height:calc(100% - 172px)' : '');
  const first = n - (now ? 30 : 110);
  chart.setVisibleLogicalRange(first, n + (now ? 34 : 40));
  // The bars in view and a 2% target on either side.
  chart.applyPriceScaleOptions({
    margins: now ? { top: 0.14, bottom: 0.14 } : { top: 0.12, bottom: 0.32 },
    range: {
      low: Math.min(entry * 0.98, ...bars.low.slice(first)),
      high: Math.max(entry * 1.02, ...bars.high.slice(first)),
    },
  });
  bar.style.right = now ? '12px' : '84px';
  note.style.flexBasis = now ? '100%' : 'auto';
  (now ? host : bar).append(panel);
  Object.assign(panel.style, now
    ? { position: 'absolute', left: '12px', right: '12px', bottom: '24px', justifyContent: 'space-between' }
    : { position: 'static', justifyContent: 'flex-start' });
}
layout();
const resized = new ResizeObserver(layout);
resized.observe(host);

// The lines of what was placed; drag TP or SL to change it.
let position = null;
const lines = createTradingLines(chart, candles, {
  priceStep: 0.5,
  async onModify({ kind, price }) {
    await new Promise((r) => setTimeout(r, 300)); // the server's answer
    if (!position) return false; // closed meanwhile
    const above = price > position.entry;
    if ((kind === 'takeProfit') !== (above === (position.side === 'buy'))) throw new Error('refused: wrong side of the entry');
    position[kind === 'takeProfit' ? 'target' : 'stop'] = price;
    say(`${kind === 'takeProfit' ? 'Take profit' : 'Stop'} moved to ${price}.`);
  },
  onError: (e) => say(`Change ${e.message}; the line went back.`),
});

// Buy and Sell open a position at the market price, 2% target and 1% stop;
// drag its handles to adjust. Users can also draw one with
// chart.drawings.startTool('long_position' or 'short_position').
const until = bars.time[n - 1] + 25 * 15 * 60_000;
function open(long) {
  for (const id of chart.drawings.ids()) if (chart.drawings.get(id)?.kind.endsWith('_position')) chart.drawings.remove(id);
  const k = long ? 1 : -1;
  chart.drawings.select(chart.drawings.add({ kind: long ? 'long_position' : 'short_position', anchors: [
    { time: bars.time[n - 1], price: entry },
    { time: until, price: entry * (1 + 0.02 * k) },
    { time: until, price: entry * (1 - 0.01 * k) },
  ] }));
  say('Adjust the target and stop, then Place order.');
}
buy.onclick = () => open(true);
sell.onclick = () => open(false);
// One position at a time: Buy and Sell wait until it is closed.
const holding = (on) => {
  for (const b of [buy, sell]) { b.disabled = on; b.style.opacity = on ? '0.4' : '1'; b.style.cursor = on ? 'default' : 'pointer'; }
  panel.style.display = on ? 'flex' : 'none';
};
close.onclick = () => {
  for (const id of lines.ids()) lines.remove(id);
  position = null;
  holding(false);
  say('Position closed. Buy or Sell to open another.');
};
open(true);

// The ticket beside the selected position: quantity from the amount at risk.
const ticket = createTradeTicket(chart, candles, {
  canvas, risk: 500, currency: '$', priceStep: 0.5, quantityStep: 0.001,
  async onPlaceOrder(intent) {
    buy.disabled = sell.disabled = true; // until the answer
    await new Promise((r) => setTimeout(r, 400));
    position = intent;
    const { entry, target, stop, quantity, side } = intent;
    lines.set({ id: 'position', kind: 'position', price: entry, quantity, title: `${side === 'buy' ? 'Long' : 'Short'} ${quantity}` });
    lines.set({ id: 'tp', kind: 'takeProfit', price: target, quantity });
    lines.set({ id: 'sl', kind: 'stopLoss', price: stop, quantity });
    chart.drawings.remove(intent.drawing);
    info.innerHTML = `<b style="color:${side === 'buy' ? '#26a69a' : '#ef5350'}">${side === 'buy' ? 'LONG' : 'SHORT'}</b> ${quantity} @ ${entry}`;
    holding(true);
    say('Filled. Drag TP or SL on the chart.');
  },
});

// The page calls this before it destroys the chart.
const stop = () => { resized.disconnect(); ticket.destroy(); lines.destroy(); bar.remove(); panel.remove(); canvas.style.cssText = css; };

為逐筆等級的資料而生。

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 在你的瀏覽器中即時渲染的圖表,並附上原始碼。所有資料均為模擬資料。

K 線回放
即時串流
K 線 + 成交量
01

價格走勢

28 個範例 →

適用於任何市場的 K 線、棒線與折線,即時串流更新。

深度熱力圖
Delta 足跡圖
足跡圖
04

訂單流

20 個範例 →

足跡圖、深度熱力圖與市場輪廓,走 compute shader 路徑。

隱含波動率 · 基差
多空比 · CVD
資金費率 · 未平倉量 · 清算
06

加密貨幣衍生性商品

15 個範例 →

資金費率、未平倉量、清算與基差,顯示在價格下方的窗格中。

風險逆轉 · 蝶式
到期前的預期波動
隱含與實現波動率 · IV Rank
12

選擇權與波動率

18 個範例 →

隱含波動率與實現波動率對照、偏斜、預期波動幅度與造市商曝險的時間變化。

同步回撤
水下曲線 · 最大回撤
權益曲線 · 回撤
16

投資組合、風險與量化

24 個範例 →

權益曲線、回撤、滾動統計、因子研究與風險檢查。

分組柱狀圖
堆疊柱狀圖
100% 堆疊區域圖
19

自訂系列與外掛

11 個範例 →

堆疊、分組、箱形與筆刷選取系列:在同一個 GPU 影格上疊加圖層外掛。

瀏覽全部 20 個應用情境

價格走勢

K 線類型與轉換

技術分析

訂單流

執行與交易檢討

加密貨幣衍生性商品

鏈上資料

DeFi 與 AMM

迷因幣與錢包

股票與基本面

中國 A 股

選擇權與波動率

利率、外匯與總體經濟

期貨與大宗商品

基金與 ETF

投資組合、風險與量化

私募市場與個人理財

預測市場

自訂系列與外掛

實驗室與旗艦範例

特色

為行情資料而設計。

01

跨平台渲染

同一個 Rust 核心:瀏覽器中使用 WebGPU 與 WebGL2 後端,原生應用程式透過 wgpu 在 Metal、Vulkan 與 DirectX 12 上執行,伺服器上則使用 CPU 渲染器。黃金影像測試確保各後端輸出一致。

02

效能與資料量無關

K 線資料常駐 GPU,座標轉換在著色器中完成,平移與縮放只需更新少量 uniform。多層級細節金字塔讓數千萬根 K 線依然流暢。

03

30 個內建指標

從 SMA 到一目均衡表的均線、通道與震盪指標。每個 tick 增量更新,並以參考資料測試。

04

29 種繪圖工具

趨勢線、通道、費波那契工具、叉子線、江恩角度線、型態、艾略特波浪、區間、部位與文字,支援復原與重做。繪圖與圖表狀態可序列化為 JSON。

05

多圖連動

同步群組可在多個圖表之間連動十字游標與可見範圍。

06

易於擴充

自訂系列、圖層與繪圖工具的外掛介面。提供 JavaScript、Rust 與 Python 繫結,以及供 C#、C++、Qt 與 Flutter 使用的 C ABI。

開始使用

每種語言,同一套 API。

所有繫結都遵循同一模型:建立圖表、加入系列、設定資料、加入指標。資料以欄的形式傳入,可以是型別化陣列、Rust 切片或 DataFrame。

  • JavaScript 與 TypeScript
  • Rust
  • Python 與 Jupyter
  • C、C#、C++
閱讀文件
main.js
$ npm install @ferrochart/core
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

FerroChart

每一個市場。
每一塊螢幕。
同一個引擎。

以 Apache-2.0 開源,從瀏覽器到交易桌。