Overview
Introduction
The BacktestX Script Editor lets you author indicators and strategies in either ES2020 JavaScript or Direct Pine Script (v5/v6)—compiled seamlessly via compileScriptSource and executed inside the chart rendering pipeline with full access to 2D canvas drawing primitives, mouse hover states, series proxies, and multi-timeframe data.
Execution Flow
Whenever the chart renders due to user panning, zooming, resizing, or incoming ticks, your script executes via the unified runtime passing the sandboxed proxy parameter ctx. In Pine Script tabs, transpilePineToJSWithMap lowers the script once per edit, synchronizes ctx._currentIndex = barIdx across historical bars, enforces zero-repaint bounds via make_series proxies, and streams log.* entries via cl:script-logs.
Safety & Sandboxing boundaries
Drawings are automatically clipped to the target chart pane so visuals never bleed over price/time scales, while make_series proxies return NaN for out-of-bounds or future bar reads (idx > barIdx).
Coordinate Mapping
Drawing on a canvas requires pixel coordinates. The system provides two primary helper conversion methods on the ctx context:
ctx.barToX(i): Converts a bar indexi(from0tobars.length - 1) to its respective X pixel coordinate on the canvas.ctx.priceToY(p): Converts a numeric price valuep(e.g.1.2345) to its respective Y pixel coordinate on the canvas.
State Preservation
Because scripts execute across bar histories on redraws, understanding state and control-flow lowering is critical:
- Persistent State (
var/varip): Declaring variables withvarorvariphoists declarations before the bar loop so values persist across candles (and supportsvar x = if .../switch ...initializers). - UDTs, Enums & Methods: Custom
typerecords,enumvalues, andmethoddot-calls persist cleanly alongside arrays/maps/matrices. - Value Conditionals & Bounds Protection: Value-returning
x = if ..., IIFE-wrappedx = switch ..., andmake_seriesfuture-read protection keep bar-by-bar state deterministic.
Exposing Technical Indicators & Volume
Your custom indicator scripts have access to standard technical indicator values in two distinct ways:
- Built-in Math Helpers: Call standard math functions directly from
ctx(such asctx.sma(closes, 20),ctx.rsi(closes, 14), orctx.macd(closes)) or use Pineta.*calls. - Active Indicators Array: Inspect
ctx.indicatorsto read pre-calculated arrays for active indicators running on the chart. - Accessing Volume: Retrieve raw bar volume using
ctx.bars[i].volume, or get the entire history array viactx.vol(ctx.bars).
Rendering Optimization & 60 FPS Guidelines
To ensure high-performance scripting (60 FPS rendering without lag or stutter):
- Clamp rendering loops: Restrict canvas rendering iterations from
Math.max(0, Math.floor(bounds.startIndex) - 1)toMath.ceil(bounds.endIndex) + 1. This padding ensures indicators pan smoothly without abruptly disappearing. - Function Hoisting (CRITICAL): Never declare helper functions inside loops or main rendering callbacks. Hoist all user-defined functions to the global scope.
- Local Scoping: Scope loop-local variables with block-level
letorconst. - Execution Limits: Scripts taking longer than 12ms to execute trigger a performance warning in the console.
Mouse Interactions & Interactive Tooltips
Query real-time cursor coordinates and click states via ctx.mouse in JS or Pine globals (mouse.x, mouse.y, mouse.is_down, mouse.clicked), test spatial bounds with is_hovered(barIndex, priceLevel, width, height), and attach rich hover popovers directly with tooltip= on box.new / label.new (backed by ctx.registerTooltip).