チャート取引

オープンソースのチャートエンジン

チャート取引

チャート上での取引(シミュレーション)。ロングまたはショートのポジションを選び、リスク額を入力すると、ストップまでの距離から数量が決まります。Place order を押すと、売買方向、エントリー、ターゲット、ストップ、数量を含むインテントがページに渡されます。発注後のポジションは価格ラインで表示され、TP や SL をドラッグするとまずページに確認し、拒否されると元の位置に戻ります。エンジンは取引所に接続せず、何も送信しません。

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 万本の足を読み込み、最初のフレームを描画

WASM · WebGPU

ネイティブで 1,000 万本の足を読み込み(LOD ピラミッド構築を含む)

ネイティブ Rust

パン、ズーム、ストリーミング中のフレームあたりのヒープアロケーション

定常状態

WebAssembly パッケージのサイズ(gzip)

全機能込み

ブラウザの数値は Google Chrome + WebGPU、1,000 万本の読み込みはネイティブ Rust での計測です。バーは各数値の目標に対する割合を示します(読み込み 300 ms、アロケーションなし、パッケージ 1.5 MB)。

サンプル

320 のサンプル、
20 のユースケース。

どのサンプルも FerroChart がブラウザ上でリアルタイムに描画するチャートで、ソースコード付きです。データはすべて合成データです。

バーリプレイ
リアルタイムストリーム
ローソク足と出来高
01

プライスアクション

28 件のサンプル →

あらゆる市場のローソク足、バー、ラインをリアルタイムでストリーミング表示します。

板のヒートマップ
デルタフットプリント
フットプリント
04

オーダーフロー

20 件のサンプル →

フットプリント、板のヒートマップ、マーケットプロファイルをコンピュートシェーダーで描画します。

インプライド・ボラティリティ・ベーシス
ロング/ショート比率・CVD
資金調達率・建玉・清算
06

暗号資産デリバティブ

15 件のサンプル →

資金調達率、建玉、清算、ベーシスを価格の下のペインに表示します。

リスクリバーサル・バタフライ
満期までの予想変動幅
インプライド対実現ボラティリティ・IVランク
12

オプションとボラティリティ

18 件のサンプル →

インプライド対実現ボラティリティ、スキュー、予想変動幅、ディーラーのエクスポージャーを時系列で表示します。

同時ドローダウン
アンダーウォーターカーブ・最大ドローダウン
資産曲線・ドローダウン
16

ポートフォリオ・リスク・クオンツ

24 件のサンプル →

資産曲線、ドローダウン、ローリング統計、ファクターリサーチ、リスクチェックを扱います。

グループ縦棒
積み上げ縦棒
100%積み上げエリア
19

カスタムシリーズとプラグイン

11 件のサンプル →

積み上げ、グループ、箱ひげ、ブラシ選択のシリーズを、同じGPUフレーム上のレイヤープラグインで描画します。

20 のユースケースをすべて見る

プライスアクション

足の種類と変換

テクニカル分析

オーダーフロー

執行とトレードレビュー

暗号資産デリバティブ

オンチェーン

DeFiとAMM

ミームコインとウォレット

株式とファンダメンタルズ

中国A株

オプションとボラティリティ

金利・為替・マクロ

先物とコモディティ

ファンドとETF

ポートフォリオ・リスク・クオンツ

プライベート市場と個人の資産管理

予測市場

カスタムシリーズとプラグイン

ラボとフラッグシップ

特長

市場データのための設計。

01

クロスプラットフォームの描画

ひとつの Rust コアで、ブラウザでは WebGPU と WebGL2、ネイティブアプリでは wgpu により Metal、Vulkan、DirectX 12、サーバーでは CPU レンダラーを使います。ゴールデンイメージテストで各バックエンドの出力を揃えています。

02

データ量に左右されない性能

足のデータは GPU 上に置かれ、座標変換はシェーダーで行うため、パンやズームでは少数の uniform を更新するだけです。LOD ピラミッドにより数千万本の足でも軽快に動きます。

03

30 の組み込みインジケーター

SMA から一目均衡表まで、移動平均、チャネル、オシレーター。値はティックごとにインクリメンタルに更新され、参照データでテストされています。

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 のオープンソース。ブラウザからトレーディングデスクまで。