Exciting Update: Version 1.0.1 is now available, introducing the high-performance BacktestX Custom Script Editor. Read more
Version: 1.0.1

Getting started

The BacktestX Custom Script Editor is an interactive runtime sandbox built into the trading terminal. It allows you to write custom scripts in JavaScript that execute on each price update and draw shapes, indicators, zones, and signals directly onto the canvas.

Introduction

Unlike traditional platforms requiring heavy compile times, the BacktestX script engine interprets standard JavaScript immediately. The engine injects a execution context variable ctx containing raw bar feeds, coordinate conversion projections, and 23+ pre-bundled indicators.

Any script you apply runs directly on the HTML5 2D canvas, allowing you to build indicators like moving averages, render custom buy/sell tags, highlight bands, or generate custom trade alerts.

Opening the Editor

To open the Script Editor workspace:

  1. Launch the BacktestX Trading Terminal.
  2. In the bottom toolbar area (below the main chart grid), locate and click the "Script Editor" button.
  3. The split-screen editor panel will slide open, displaying the code editor on the left, an action toolbar, and the interactive mockup chart console.

Interface Layout

The workspace consists of four primary components:

1. Code Pane

Includes line numbers, parenthesis coloring, live bracket matching, and auto-complete (triggered via Ctrl + Space).

2. Toolbar Actions

Contains run/save buttons. Click "Add to chart" or press Ctrl + Enter to immediately draw on the chart.

3. Performance Profiler

Benchmarks execution speed over 500 iterations of code. Average runs compile under 0.2ms for lag-free performance.

4. Console Inspector

Prints warnings, lint issues, compiler errors (with source code line numbers), and logging outputs in real time.

Your First Script

Let's write a simple script that overlays a 20-period Simple Moving Average (SMA) line over the price chart:

JavaScript
// 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();

Standard Workflow

Autosave & Dirty Tab Tracker

When you make modifications to a script, a blue dot indicator appears next to the tab name in the tab header panel indicating a dirty unsaved state. Click the Save icon or press Ctrl + S to save. Saved indicators are saved locally to `localStorage` and persist between page loads.

Compiler & Syntax Linting

The editor lints your code continuously in the background using an isolated evaluation shell. If you write syntax errors, the line number gutter immediately highlights in a transparent red band, and the Console inspector lists the error context and expected characters.