kScript v1 vs v2 — Key Differences
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
- 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 when misunderstood.
v2
- 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
- Functions accepted only positional arguments.
- Harder to read and maintain scripts.
v2
- All functions now support keyword arguments (
kwargs
). - More readable and order-independent.
3) Compiler Improvements
v1
- Most errors surfaced only at runtime.
- Developers had to debug by trial and error.
v2
- 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
- All data sources handled via the generic
source(...)
.
v2
- Dedicated functions for specific data types:
ohlcv(symbol, exchange)
trades(symbol, exchange)
orderbook(symbol, exchange)
- Improves readability and ensures correct schema handling.
5) Technical Indicator Functions
v1
- Functions like
rsi
,ema
returned a full timeseries. - Developers often misunderstood how to work with them, leading to redundant or incorrect code.
v2
- Each technical function returns a scalar value per bar.
- Much simpler to use directly in expressions and plots.
6) Field Accessors
v1
- Accessing fields from data was clunky and inconsistent.
v2
- Clean accessors for multi-field series like OHLCV.
- Example:
ohlcvTs.close
,ohlcvTs.open
,ohlcvTs.high
,ohlcvTs.low
,ohlcvTs.volume
.
7) Reverse Index Access
v1
- Forward-style indexing only (oldest first).
- Hard to get the latest values directly.
v2
- Reverse indexing:
ts[0]
→ latest bar,ts[1]
→ one bar before last, etc.
8) Function Definitions
v1
- No support for custom functions.
v2
- User-defined functions via
func
. - Enables modular, reusable code.
9) Loops
v1
- No support for looping constructs.
v2
- Full support for
for
andwhile
loops (restricted tovar
variables).
10) Plot Functions
v1
- Limited plotting functions (
plotLine
,plotBar
).
v2
- Extended plotting options:
plotLine(...)
plotBar(...)
plotCandle(...)
plotShape(...)
- All support kwargs and flexible styling.
11) Standard Library
v1
- Minimal helper set.
v2
- Extended standard library with math, indicators, and utility helpers:
- Indicators:
rsi
,ema
,sma
,stoch
,ma
, … - Helpers:
abs
,round
,max
,min
, … - Debugging:
print
,printTimeSeries
- Indicators:
Summary
kScript v2 delivers major improvements over v1:
- Per-bar execution eliminates manual time alignment.
- Keyword arguments make functions clear and self-documenting.
- Compile-time checking prevents most runtime surprises.
- Dedicated subscription functions improve clarity.
- Technical indicators return scalars per bar instead of entire timeseries.
- Field accessors make OHLCV data ergonomic.
- Reverse indexing gives direct access to the latest data.
- Functions and loops enable modular and expressive code.
- Extended plot functions and library unlock richer visualization and analysis.
Overall, v2 is more intuitive, safer, and expressive — while maintaining performance for real-time charting.