Getting started
The BacktestX Custom Script Editor is a dual-language interactive runtime sandbox built directly into the trading terminal. Write custom indicators and backtesting strategies natively in Pine Script (v5/v6) or ES2020 JavaScript—auto-compiled, linted, and rendered onto the chart canvas in real time.
Introduction
Unlike traditional platforms requiring remote compilation roundtrips, BacktestX runs both native JavaScript and auto-transpiled Pine Script directly in the browser via compileScriptSource. Your script receives the live ctx context containing raw bar feeds, symbol/timezone metadata, coordinate projections, strategy accounting, and 35+ bundled technical indicators.
When writing Pine Script (//@version=5, indicator(...), or strategy(...)), the editor toolbar button automatically switches to Auto · Pine, maps syntax/compile diagnostics back to Pine line numbers via transpilePineToJSWithMap, and allocates dedicated sub-panes whenever overlay=false is specified.
Opening the Editor
To open the Script Editor workspace:
- Open the BacktestX Trading Terminal.
- In the top toolbar, click on the Indicators button (or open Script Editor from the bottom/workspace panel).
- Start a new tab, select the Pine Script: EMA Cross + Logs (
pine_ema) starter template, or import an existing.pine/.pinescript/.jsfile.
Interface Layout
The workspace consists of four primary components upgraded with full Pine + JS IDE support in v1.0.6:
1. Code Pane & IDE Tooling
Includes line numbers, Pine/JS syntax highlighting, indentation folding, 4-space Enter auto-indent, prefix-ranked autocomplete (Ctrl + Space), live parameter hints (signatureBox inside (...)), and hover documentation tooltips.
2. Toolbar & Language Mode Switcher
Features per-tab langModeBtn (Auto · Pine / Auto · JS / Mode · Pine / Mode · JS), .pine / .pinescript / .js import & export, Save, History, and Add to chart (Ctrl + Enter).
3. Performance Profiler
Benchmarks execution speed over 500 iterations of code (automatically transpiling Pine tabs before timing). Typical indicators run under 0.2ms per frame.
4. Console & Pine Logs Inspector
Displays live log.info / warning / error runtime messages, timestamp & #bar filters, and clickable bar badges that jump straight to the matching candle on the chart.
Your First Script
Let's write a simple script that overlays a 20-period Simple Moving Average (SMA) line over the price chart:
// 1. Retrieve bar and viewport data
const bars = ctx.bars;
const bounds = ctx.bounds;
if (!bars || bars.length < 20) return;
// 2. Compute indicator feed
const sma20 = ctx.sma(bars.map(b => b.close), 20);
// 3. Setup canvas options
ctx.beginPath();
ctx.strokeStyle = '#2962ff';
ctx.lineWidth = 2.5;
// 4. Render SMA line
let started = false;
for (let i = Math.max(0, Math.floor(bounds.startIndex) - 1); i <= Math.ceil(bounds.endIndex) + 1; i++) {
if (i >= bars.length || isNaN(sma20[i])) continue;
const x = ctx.barToX(i);
const y = ctx.priceToY(sma20[i]);
if (!started) {
ctx.moveTo(x, y);
started = true;
} else {
ctx.lineTo(x, y);
}
}
ctx.stroke();
Want to write that same SMA overlay directly in Pine Script instead? See Direct Pine Script Mode & Logs.
Standard Workflow
Autosave, Pine Metadata & Dirty Tab Tracker
Modified tabs show a blue dirty dot next to the script name. Pressing Ctrl + S persists both your compiled code and original Pine sourceCode, lang, and langMode on state.customScripts inside `localStorage`.
Zero-Repaint Linting & Pine Source Maps
The live linter validates both JS syntax and Pine zero-repaint diagnostics (flagging barmerge.lookahead_on, negative series offsets, or forward bar_index reads—promoted to errors with //@strict-no-repaint). When a runtime or compile error occurs in Pine mode, source maps highlight the exact Pine line in the gutter.