Exciting Update: Version 1.0.6 is now available, introducing Direct Pine Script Mode, Pine Logs Runtime Bridge, Zero-Repaint Linting, Source Maps, Intrabar MFE/MAE Stats, and expanded Pine v5/v6 Transpiler Parity. Read more

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 index i (from 0 to bars.length - 1) to its respective X pixel coordinate on the canvas.
  • ctx.priceToY(p): Converts a numeric price value p (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 with var or varip hoists declarations before the bar loop so values persist across candles (and supports var x = if ... / switch ... initializers).
  • UDTs, Enums & Methods: Custom type records, enum values, and method dot-calls persist cleanly alongside arrays/maps/matrices.
  • Value Conditionals & Bounds Protection: Value-returning x = if ..., IIFE-wrapped x = switch ..., and make_series future-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 as ctx.sma(closes, 20), ctx.rsi(closes, 14), or ctx.macd(closes)) or use Pine ta.* calls.
  • Active Indicators Array: Inspect ctx.indicators to 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 via ctx.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) to Math.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 let or const.
  • 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).