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
- 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
2. Keyword Arguments (kwargs)
Functions accepted only positional arguments, making scripts harder to read and maintain.
plotLine(rsiTs, 3, ["red", "green"])
{% endtab %}
{% tab title="v2 (Current)" %}
All functions now support **keyword arguments** (`kwargs`). More readable and order-independent.plotLine(value=rsiTs, width=3, colors=["red", "green"])
{% endtab %}
{% endtabs %}
3. Compiler Improvements
- Most errors surfaced only at runtime
- Developers had to debug by trial and error
4. Data Subscriptions
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.
timeseries ohlcvTs = ohlcv(symbol="BTCUSDT", exchange="BINANCE")
{% endtab %}
{% endtabs %}
5. Technical Indicator Functions
- Functions like
rsi,emareturned 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
var r = rsi(source=ohlcvTs.close, period=14)
plotLine(value=r, width=2, colors=["#3fa9f5"], label=["RSI"], desc=["Relative Strength Index"])
{% endtab %}
{% endtabs %}
6. Field Accessors
Accessing fields from data was clunky and inconsistent.
Clean accessors for multi-field series like OHLCV:
ohlcvTs.closeohlcvTs.openohlcvTs.highohlcvTs.lowohlcvTs.volume
var diff = ohlcvTs.close - ohlcvTs.open
{% endtab %}
{% endtabs %}
7. Reverse Index Access
- 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.
var lastClose = ohlcvTs[0].close
var prevClose = ohlcvTs[1].close
{% endtab %}
{% endtabs %}
8. Function Definitions
No support for custom functions.
User-defined functions via func. Enables modular, reusable code.
func safeDiv(a, b) {
return b == 0 ? 0 : a / b
}
{% endtab %}
{% endtabs %}
9. Loops
No support for looping constructs.
Full support for for and while loops (restricted to var variables).
for (var i = 0; i < 5; i = i + 1) {
print(text=i)
}
{% endtab %}
{% endtabs %}
10. Plot Functions
Limited plotting functions (plotLine, plotBar).
11. Standard Library
Minimal helper set.
Summary
kScript v2 delivers major improvements over v1:
ohlcv(...), trades(...), and orderbook(...) instead of a generic source(...) catch-all.ohlcvTs.close are first-class paths instead of ad-hoc structures.series[0] reads the latest bar; older bars use higher indexes—matching how traders think.func and constrained for/while loops.Overall, v2 is more intuitive, safer, and expressive — while maintaining performance for real-time charting.