Custom indicators
이 콘텐츠는 아직 번역되지 않았습니다.
A plugin is an ES module of indicator definitions. Its kernel runs inside the data engine, in the worker or on the main thread when the page blocks workers, so the module is loaded by URL:
// my-indicators.js: a standalone module (no bare imports), served by the applicationexport default [ { name: 'momentum', inputs: ['close'], outputs: [{ name: 'value', style: 'histogram', color: '#26a69a' }], params: [{ name: 'period', default: 10, min: 1, integer: true }], windowed: true, // values depend only on the last `lookback + 1` rows stateSize: () => 0, lookback: params => params[0], warmup: params => params[0], // Rows [offset, offset + length) of a column-major buffer (inputs, then outputs); the `carry` // input rows before `offset` hold the rows before the slice. kernel(buffer, offset, length, state, carry, params) { const stride = buffer.length / 2; for (let r = offset; r < offset + length; r++) { buffer[stride + r] = buffer[r] - buffer[r - params[0]]; } }, },];await chart.registerIndicators(new URL('./my-indicators.js', import.meta.url));candles.addIndicator('momentum', { params: { period: 14 } });import type { OhlcColumns } from '@lumen-ticks/core';import { LumenChart, type ChartApi } from '@lumen-ticks/react';import { useEffect, useState } from 'react';
const moduleUrl = new URL('./my-indicators.js', import.meta.url);
export function CustomIndicatorChart({ bars }: { bars: OhlcColumns }) { const [chart, setChart] = useState<ChartApi | null>(null);
useEffect(() => { if (chart === null) return; let cancelled = false; const candles = chart.addSeries('candlestick'); candles.setData(bars);
void chart.registerIndicators(moduleUrl).then(() => { if (!cancelled) { candles.addIndicator('momentum', { params: { period: 14 } }); } });
return () => { cancelled = true; chart.removeSeries(candles); }; }, [chart, bars]);
return <LumenChart ref={setChart} style={{ height: 420 }} />;}<script setup lang="ts">import type { OhlcColumns } from '@lumen-ticks/core';import { LumenChart, type ChartApi } from '@lumen-ticks/vue';import { onBeforeUnmount, onMounted, useTemplateRef } from 'vue';
const props = defineProps<{ bars: OhlcColumns }>();const chartEl = useTemplateRef<{ chart: ChartApi | null }>('chartEl');const moduleUrl = new URL('./my-indicators.js', import.meta.url);let unmounted = false;
onMounted(async () => { const chart = chartEl.value?.chart; if (!chart) return; const candles = chart.addSeries('candlestick'); candles.setData(props.bars); await chart.registerIndicators(moduleUrl); if (!unmounted) { candles.addIndicator('momentum', { params: { period: 14 } }); }});
onBeforeUnmount(() => { unmounted = true;});</script>
<template> <LumenChart ref="chartEl" style="height: 420px" /></template><script lang="ts"> import type { OhlcColumns } from '@lumen-ticks/core'; import { lumenChart, type ChartApi } from '@lumen-ticks/svelte'; import { onDestroy } from 'svelte';
let { bars }: { bars: OhlcColumns } = $props(); const moduleUrl = new URL('./my-indicators.js', import.meta.url); let destroyed = false; onDestroy(() => { destroyed = true; });
async function onReady(chart: ChartApi) { const candles = chart.addSeries('candlestick'); candles.setData(bars); await chart.registerIndicators(moduleUrl); if (!destroyed) { candles.addIndicator('momentum', { params: { period: 14 } }); } }</script>
<div style="height: 420px" use:lumenChart={{ onReady }}></div>import type { OhlcColumns } from '@lumen-ticks/core';import { LumenChart, type ChartApi } from '@lumen-ticks/solid';import { onCleanup } from 'solid-js';
const moduleUrl = new URL('./my-indicators.js', import.meta.url);
export function CustomIndicatorChart(props: { bars: OhlcColumns }) { let disposed = false; onCleanup(() => { disposed = true; });
const handleReady = async (chart: ChartApi) => { const candles = chart.addSeries('candlestick'); candles.setData(props.bars); await chart.registerIndicators(moduleUrl); if (!disposed) { candles.addIndicator('momentum', { params: { period: 14 } }); } };
return <LumenChart class="chart-box" onReady={handleReady} />;}The contract (IndicatorKernel, IndicatorDefinition) is documented in the types. A cumulative
kernel (the default) keeps its running values in state, which is zeros on a fresh start; a
windowed one must rebuild its state from the carry rows after a reset, which lets the engine
recompute only the rows near a prepend. The page’s content security policy must allow the module
(script-src, and blob: for a module made from a Blob). Bundlers must leave it as a separate file
with a stable URL, for example in public/; the engine’s import() is marked to be ignored by
Vite and webpack. To draw an output other than as a line or histogram, give it a custom renderer
(see Primitives and custom renderers).