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
Version: 1.0.6

Pine Script Transpiler

BacktestX includes a Pine Script (v4/v5/v6) transpiler (transpilePineToJSWithMap). In v1.0.6, you no longer have to convert scripts manually—just paste Pine code straight into the Script Editor (Auto · Pine mode) or use the standalone Converter modal to export clean Canvas JavaScript.

How it works

Whether triggered automatically by compileScriptSource when running/linting Pine tabs or manually in the Converter modal, the transpiler lowers Pine syntax into sandboxed JavaScript with source-line mapping:

  • Series & Historical Proxies: OHLCV, hl2, hlc3, ohlc4, hlcc4, and indexed variables use make_series proxies with bounds safety (future reads return NaN).
  • Pine v5/v6 Language Constructs: Supports type UDTs, enum blocks, method dot-dispatch, value-returning x = if / x = switch expressions, negative-step countdown for loops, for [idx, val] in arr, and user-function tuple returns [a, b] = fn().
  • Quote/Comment-Safe Named Args: Resolves out-of-order named parameters (like ta.ema(length=20, source=close)) after masking string literals and stripping comments.
  • Directives & Source Maps: Supports //@strict-no-repaint, //@js-raw ... //@end-js-raw, and sourceMap line projection back to your Pine editor lines.

For full Direct Pine Mode workflows, IDE intellisense, Pine Logs, and UDT/built-in series reference, see Direct Pine Script Mode & Logs.

Drawing, Plotting & Built-in Wrappers

Standard Pine Script drawing primitives, plot handles, gradient fills, metadata namespaces, and logging calls map directly onto BacktestX's Canvas 2D runtime:

Supported Pine Script APIs
// Plot handles, styles, per-bar colors & gradients
p1 = plot(ta.ema(close, 20), title="Fast", color=color.green, style=plot.style_stepline)
h1 = hline(50, color=color.gray)
fill(p1, h1, color=color.gradient(color.new(color.green, 70), color.new(color.green, 95)))

// Drawings, coordinates, splines & tooltips
pt = chart.point.from_index(bar_index, high)
box.new(left, top, right, bottom, bgcolor, border_color, tooltip="Zone Info")
line.new(x1, y1, x2, y2, color, style, width, extend=extend.right)
linefill.new(line1, line2, color)
label.new(point=pt, text="Signal", style=label.style_label_down, tooltip="Details")
polyline.new(pointsArray, curved=true, closed=true, fill_color=color.gradient(c1, c2))
polygon.new(pointsArray, color.blue, color.new(color.blue, 85), 1)
fib.new(pt1, pt2, color.purple, [0, 0.382, 0.5, 0.618, 1.0])

// Metadata, sessions, MTF & telemetry
syminfo.tickerid, barstate.islast, session.ismarket, ticker.heikinashi(syminfo.tickerid)
log.info("Close={0} MFE={1}", close, strategy.closedtrades.mfe(0))

v1.0.6 Parity & Safety Extensions

  • TA & Stdlib: Gap-aware ta.tr, non-repainting 2-arg/3-arg ta.pivothigh/pivotlow, ta.sar, BacktestX TA adapters, and expanded math.* / str.* functions.
  • Realistic Strategy Engine: commission_type/commission_value, tick slippage, weighted pyramiding, closedtrades.*, intrabar mfe/mae, strategy.set_metric, and strategy.tag_trade.
  • Hover Interactivity: Live cursor state via mouse.x, mouse.y, mouse.is_down, mouse.clicked, and is_hovered(barIndex, priceLevel, width, height).