Getting Started

What's New in v3

kScript v3 turns the language from plot-an-indicator into model-a-trading-idea: typed structs, persistent collections, self-managing drawings, no-repaint multi-timeframe, multi-venue aggregation, and a reference-validated TA library. Every capability here is verified against the live engine.

kScript v3 is the largest release since the language launched. v2 let you compute a series and plot it. v3 lets you model a trading idea: hold state in typed structures across bars, draw and retire chart objects, read higher and lower timeframes without repaint risk, combine symbols and venues under one budget, and call a technical-analysis library whose every function is validated against an independent reference.

It is additive. Every v2 script keeps running, and the version marker is still //@version=2 (see The version marker below). "v3" is the engine capability tier, not a number you write.

            v2                              v3
  ----------------------------    ------------------------------------
  numbers, strings, booleans      + arrays & maps with lambdas/reducers
  fire-and-forget plotting        + type Zone { ... } structs with methods
  one symbol, one timeframe        + na & color as real types
  ~15 ad-hoc TA helpers           + drawings: line/box/label/polyline/
                                    linefill/table handles with .delete()
                                  + htf()/ltf()/request(): no-repaint MTF,
                                    multi-symbol, multi-venue aggregation
                                  + 70+ reference-validated TA functions,
                                    named streams (bb.upper, macd.signal)
                                  + vwap(anchor="day"|"week"|"month")
                                  + a named limits manifest; every error
                                    carries a line and column

The headline capabilities

Typed structs with methods

Declare a type with fields and behavior, construct instances with named fields, and carry them across bars. State now has the shape of the problem instead of a pile of parallel arrays.

scripts/probes/lang-types/band_struct_method.ks
//@version=2
define(title="Struct Methods", position="offchart", axis=true)

type Band {
  top: number,
  bottom: number,

  func width() {
    return this.top - this.bottom
  }
}

timeseries d = ohlcv(symbol=currentSymbol, exchange=currentExchange)
var band = Band.new(top=d.high[0], bottom=d.low[0])
plotLine(value=band.width(), colors=["#2563eb"], width=2, label=["Band width"], desc=["high minus low via a struct method"])

See User-Defined Types.

Self-managing drawings

Lines, boxes, labels, polylines, linefills, and tables are now handles you can create and .delete(). Build supply/demand zones that remove themselves when price mitigates them, or a live dashboard table that updates on the last bar.

See Drawing Objects.

No-repaint multi-timeframe

htf() reads a confirmed higher timeframe with no look-ahead: the 4h value at any bar uses only data that closed before that bar. The repaint trap that security() invites in other languages is impossible by default. ltf() attaches finer bars, and request() is one-line shorthand for loading another symbol.

See Multi-Timeframe.

Multi-venue aggregation

A single script can load up to 20 distinct sources. Sum real buy/sell flow across four exchanges into one cumulative delta, or aggregate open interest across venues — the kind of cross-market view that took one source per script in v2.

See Multi-Source & Aggregation and Data Sources for all 18 source types.

A reference-validated TA library

70+ canonical indicators, each matching an independent reference implementation. Multi-output indicators expose named streams: bb.upper, macd.signal, stoch.k, supertrend.direction — no more positional guesswork.

See the TA Library and Named Streams.

Session-anchored VWAP

vwap(anchor="day" | "week" | "month") resets on real UTC session boundaries instead of drifting with however much history happened to load. The no-argument form is the familiar cumulative VWAP.

See Special Indicators.

Compatibility

  • Every v2 script keeps running. v3 is additive.
  • Eight TA builtins were corrected to their textbook forms (ema seeding, rsi Wilder smoothing, macd, psar, adx, highest/lowest, strict sma warmup). Early-bar values change for those functions; this is a correction, not a regression, since each now matches an independent reference.
  • A few v2 conveniences were tightened or renamed; the v2 vs v3 migration guide lists them.

The version marker

A v3 script still starts with //@version=2 on its first line. The number is the script-format marker, not the engine version — and it must be //@version=2. Do not write //@version=3: the engine does not recognize it and falls back. All of the v3 capabilities above compile and run under the //@version=2 marker.

scripts/probes/lang-variables/var_timeseries_basic.ks
//@version=2
define(title="v3 under the v2 marker", position="offchart", axis=true)

timeseries d = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries sma20 = sma(source=d.close, period=20)
plotLine(value=sma20, colors=["#2563eb"], width=2, label=["SMA 20"], desc=["v3 features run under //@version=2"])

If something does not behave as documented, the Common Errors page maps every real engine error message to its cause and fix.