Skip to content

useScriptEditor

A Pine Script-style custom indicator editor. Users write plain JavaScript function bodies that receive TA, dataList (array of KLineData) and params (parsed from a comma-separated string). The script returns an array of objects — one per candle, where each key becomes a chart series.

Scripts execute inside a sandboxed new Function() with dangerous globals shadowed: fetch, XMLHttpRequest, WebSocket, Worker, SharedWorker, importScripts, self, caches, indexedDB.

import { useScriptEditor } from "react-klinecharts-ui";
const {
code, setCode,
scriptName, setScriptName,
params, setParams,
placement, setPlacement,
error, status, isRunning, hasActiveScript,
runScript, removeScript, resetCode,
exportScript, importScript,
defaultScript,
} = useScriptEditor();
PropertyTypeDescription
codestringCurrent script source
setCode(code: string) => voidUpdate the source
scriptNamestringDisplay name
setScriptName(name: string) => voidUpdate the name
paramsstringComma-separated numeric params (e.g. "14, 26, 9")
setParams(params: string) => voidUpdate params
placementScriptPlacement"main" or "sub"
setPlacement(p: ScriptPlacement) => voidSet placement
errorstringLast error message (empty = none)
statusstringLast status message
isRunningbooleanWhether the script is executing
hasActiveScriptbooleanWhether a script indicator is on the chart
runScript() => voidExecute and register the indicator
removeScript() => voidRemove the script indicator
resetCode() => voidReset to the default template
exportScript() => voidDownload the code as a .js file
importScript(file: File) => voidLoad a script from a file
defaultScriptstringThe default template code
// Available: TA, dataList, params
const period = params[0] ?? 14;
const closes = dataList.map((d) => d.close);
const rsi = TA.rsi(closes, period);
const boll = TA.bollinger(closes, period, 2);
// Return one object per candle — each key = one line on the chart
return rsi.map((v, i) => ({
rsi: v,
upper: boll.upper[i],
mid: boll.mid[i],
lower: boll.lower[i],
}));

The TA helpers available inside scripts are documented in TA (Technical Analysis).