图表交易

开源图表引擎

图表交易

直接在图上交易(模拟)。选中多头或空头仓位,填写风险金额,数量会按到止损的距离算出;点 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

原生载入 1000 万根 K 线,含多级细节金字塔

原生 Rust

平移、缩放和实时推送时每帧的堆分配次数

稳定状态

WebAssembly 包体积(gzip)

包含全部功能

浏览器数据:Google Chrome + WebGPU;1000 万根 K 线的载入为原生 Rust。进度条表示各项数据相对于目标的比例:载入 300 毫秒、零分配、安装包 1.5 MB。

示例

320 个示例,
覆盖 20 个应用场景。

每个示例都是 FerroChart 在你的浏览器中实时渲染的图表,并附带源代码。所有数据均为模拟数据。

K 线回放
实时数据流
K 线 + 成交量
01

价格走势

28 个示例 →

适用于任何市场的 K 线、美国线和折线,实时流式更新。

深度热力图
Delta 足迹图
足迹图
04

订单流

20 个示例 →

足迹图、深度热力图和市场轮廓,走计算着色器路径。

隐含波动率 · 基差
多空比 · CVD
资金费率 · 持仓量 · 清算
06

加密衍生品

15 个示例 →

资金费率、持仓量、清算和基差,显示在价格下方的窗格中。

风险逆转 · 蝶式
到期预期波动幅度
隐含与已实现波动率 · IV Rank
12

期权与波动率

18 个示例 →

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

联合回撤
水下曲线 · 最大回撤
净值曲线 · 回撤
16

组合、风险与量化

24 个示例 →

净值曲线、回撤、滚动统计、因子研究和风险检查。

分组柱状图
堆叠柱状图
100% 堆叠面积图
19

自定义系列与插件

11 个示例 →

堆叠、分组、箱线和刷选系列:在同一 GPU 帧上叠加图层插件。

浏览全部 20 个应用场景

价格走势

K 线类型与变换

技术分析

订单流

执行与交易复盘

加密衍生品

链上数据

DeFi 与 AMM

Meme 币与钱包

股票与基本面

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 开源,从浏览器到交易台。