This document highlights the major improvements and design changes between kScript v1 and kScript v2 . The goal of v2 is to make scripting more intuitive, powerful, and performant for indicator developers.
1. Execution Model
v1 (Legacy) v2 (Current)
Execution was line-by-line and evaluated expressions directly
Developers had to manually assemble time-aligned series using:
buildTimeseries(...)
mergeTimeseries(...)
matchTimestamp(...)
timeseries(...) constructor
This was unintuitive and often led to performance bottlenecks
Introduces per-bar execution model , similar to Pine Script
Script phases:
Initialization → definitions, inputs, data subscriptions
Calculation → runs once per bar
Plotting → render output
No need for manual alignment; the engine handles timeseries iteration automatically
2. Keyword Arguments (kwargs)
v1 (Legacy) v2 (Current)
Functions accepted only positional arguments , making scripts harder to read and maintain.
v1 Positional Arguments
plotLine (rsiTs, 3 , [ "red" , "green" ]) All functions now support keyword arguments (kwargs). More readable and order-independent.
v2 Keyword Arguments
plotLine (value = rsiTs, width = 3 , colors = [ "red" , "green" ])
3. Compiler Improvements
v1 (Legacy) v2 (Current)
Most errors surfaced only at runtime
Developers had to debug by trial and error
Strong compile-time analysis
Detects syntax errors, scope violations, and type mismatches before execution
Safer and faster iteration for script developers
4. Data Subscriptions
v1 (Legacy) v2 (Current)
All data sources handled via the generic source(...) function.
Dedicated functions for specific data types:
ohlcv(symbol, exchange)
trades(symbol, exchange)
orderbook(symbol, exchange)
Improves readability and ensures correct schema handling.
Dedicated Data Subscription
timeseries ohlcvTs = ohlcv (symbol = "BTCUSDT" , exchange = "BINANCE" )
5. Technical Indicator Functions
v1 (Legacy) v2 (Current)
Functions like rsi, ema returned a full timeseries
Developers often misunderstood how to work with them, leading to redundant or incorrect code
Each technical function returns a scalar value per bar
Much simpler to use directly in expressions and plots
Scalar Technical Indicator
var r = rsi (source = ohlcvTs.close, period = 14 )
plotLine (value = r, width = 2 , colors = [ "#3fa9f5" ], label = [ "RSI" ], desc = [ "Relative Strength Index" ])
6. Field Accessors
v1 (Legacy) v2 (Current)
Accessing fields from data was clunky and inconsistent.
Clean accessors for multi-field series like OHLCV:
ohlcvTs.close
ohlcvTs.open
ohlcvTs.high
ohlcvTs.low
ohlcvTs.volume
Field Accessors
var diff = ohlcvTs.close - ohlcvTs.open
7. Reverse Index Access
v1 (Legacy) v2 (Current)
Forward-style indexing only (oldest first)
Hard to get the latest values directly
Reverse indexing: ts[0] → latest bar, ts[1] → one bar before last, etc.
Reverse Index Access
var lastClose = ohlcvTs[ 0 ].close
var prevClose = ohlcvTs[ 1 ].close
8. Function Definitions
v1 (Legacy) v2 (Current)
No support for custom functions.
User-defined functions via func. Enables modular, reusable code.
User-Defined Function
func safeDiv (a, b) {
return b == 0 ? 0 : a / b
}
9. Loops
v1 (Legacy) v2 (Current)
No support for looping constructs.
Full support for for and while loops (restricted to var variables).
For Loop
for ( var i = 0 ; i < 5 ; i = i + 1 ) {
print (text = i)
}
10. Plot Functions
v1 (Legacy) v2 (Current)
Limited plotting functions (plotLine, plotBar).
Extended plotting options:
plotLine(...)
plotBar(...)
plotCandle(...)
plotShape(...)
All support kwargs and flexible styling.
11. Standard Library
v1 (Legacy) v2 (Current)
Extended standard library with math, indicators, and utility helpers:
Indicators: rsi, ema, sma, stoch, ma, ...
Helpers: abs, round, max, min, ...
Debugging: print, printTimeSeries
Summary
kScript v2 delivers major improvements over v1:
Per-bar execution
Eliminates manual time alignment—the engine advances bar-by-bar for you.
Keyword arguments
Functions read clearly with named parameters instead of brittle positional-only calls.
Compile-time checking
Syntax, scope, and many type issues surface before you run against live data.
Dedicated subscriptions
Use ohlcv(...), trades(...), and orderbook(...) instead of a generic source(...) catch-all.
Scalar indicators
Built-in indicators return values for the current bar—no whole-series juggling for typical plots.
Field accessors
OHLCV fields like ohlcvTs.close are first-class paths instead of ad-hoc structures.
Reverse indexing
series[0] reads the latest bar; older bars use higher indexes—matching how traders think.
Functions and loops
Author reusable helpers with func and constrained for/while loops.
Plots and standard library
Candles, shapes, more kwargs—and a wider math/indicator/debug toolkit out of the box.
Overall, v2 is more intuitive, safer, and expressive — while maintaining performance for real-time charting.