---
title: kScript v1 vs v2 — Key Differences
description: >-
  Major improvements and design changes between kScript v1 and v2. Learn what's
  new in the latest version.
---


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

{% tabs %}
{% tab title="v1 (Legacy)" %}
{% hint style="warning" %}
Legacy approach - not recommended
{% endhint %}

- 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**
{% endtab %}

{% tab title="v2 (Current)" %}
{% hint style="success" %}
Recommended approach
{% endhint %}

- Introduces **per-bar execution model**, similar to Pine Script
- Script phases:
  1. **Initialization** → definitions, inputs, data subscriptions
  2. **Calculation** → runs once per bar
  3. **Plotting** → render output
- No need for manual alignment; the engine handles timeseries iteration automatically
{% endtab %}
{% endtabs %}

## 2. Keyword Arguments (kwargs)

{% tabs %}
{% tab title="v1 (Legacy)" %}
Functions accepted only **positional arguments**, making scripts harder to read and maintain.

```javascript title="v1 Positional Arguments" lines wrap
plotLine(rsiTs, 3, ["red", "green"])
```
{% endtab %}

{% tab title="v2 (Current)" %}
All functions now support **keyword arguments** (`kwargs`). More readable and order-independent.

```javascript title="v2 Keyword Arguments" lines wrap
plotLine(value=rsiTs, width=3, colors=["red", "green"])
```
{% endtab %}
{% endtabs %}

## 3. Compiler Improvements

{% tabs %}
{% tab title="v1 (Legacy)" %}
- Most errors surfaced only **at runtime**
- Developers had to debug by trial and error
{% endtab %}

{% tab title="v2 (Current)" %}
- Strong **compile-time analysis**
- Detects **syntax errors, scope violations, and type mismatches** before execution
- Safer and faster iteration for script developers
{% endtab %}
{% endtabs %}

## 4. Data Subscriptions

{% tabs %}
{% tab title="v1 (Legacy)" %}
All data sources handled via the generic `source(...)` function.

{% endtab %}

{% tab title="v2 (Current)" %}
Dedicated functions for specific data types:
- `ohlcv(symbol, exchange)`
- `trades(symbol, exchange)`
- `orderbook(symbol, exchange)`

Improves readability and ensures correct schema handling.

```javascript title="Dedicated Data Subscription" lines wrap
timeseries ohlcvTs = ohlcv(symbol="BTCUSDT", exchange="BINANCE")
```
{% endtab %}
{% endtabs %}

## 5. Technical Indicator Functions

{% tabs %}
{% tab title="v1 (Legacy)" %}
- Functions like `rsi`, `ema` returned a **full timeseries**
- Developers often misunderstood how to work with them, leading to redundant or incorrect code
{% endtab %}

{% tab title="v2 (Current)" %}
- Each technical function returns a **scalar value per bar**
- Much simpler to use directly in expressions and plots

```javascript title="Scalar Technical Indicator" lines wrap
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

{% tabs %}
{% tab title="v1 (Legacy)" %}
Accessing fields from data was clunky and inconsistent.
{% endtab %}

{% tab title="v2 (Current)" %}
Clean accessors for multi-field series like OHLCV:
- `ohlcvTs.close`
- `ohlcvTs.open`
- `ohlcvTs.high`
- `ohlcvTs.low`
- `ohlcvTs.volume`

```javascript title="Field Accessors" lines wrap
var diff = ohlcvTs.close - ohlcvTs.open
```
{% endtab %}
{% endtabs %}

## 7. Reverse Index Access

{% tabs %}
{% tab title="v1 (Legacy)" %}
- Forward-style indexing only (oldest first)
- Hard to get the latest values directly
{% endtab %}

{% tab title="v2 (Current)" %}
Reverse indexing: `ts[0]` → latest bar, `ts[1]` → one bar before last, etc.

```javascript title="Reverse Index Access" lines wrap
var lastClose = ohlcvTs[0].close
var prevClose = ohlcvTs[1].close
```
{% endtab %}
{% endtabs %}

## 8. Function Definitions

{% tabs %}
{% tab title="v1 (Legacy)" %}
No support for custom functions.
{% endtab %}

{% tab title="v2 (Current)" %}
User-defined functions via `func`. Enables modular, reusable code.

```javascript title="User-Defined Function" lines wrap
func safeDiv(a, b) {
  return b == 0 ? 0 : a / b
}
```
{% endtab %}
{% endtabs %}

## 9. Loops

{% tabs %}
{% tab title="v1 (Legacy)" %}
No support for looping constructs.
{% endtab %}

{% tab title="v2 (Current)" %}
Full support for `for` and `while` loops (restricted to `var` variables).

```javascript title="For Loop" lines wrap
for (var i = 0; i < 5; i = i + 1) {
  print(text=i)
}
```
{% endtab %}
{% endtabs %}

## 10. Plot Functions

{% tabs %}
{% tab title="v1 (Legacy)" %}
Limited plotting functions (`plotLine`, `plotBar`).
{% endtab %}

{% tab title="v2 (Current)" %}
Extended plotting options:
- `plotLine(...)`
- `plotBar(...)`
- `plotCandle(...)`
- `plotShape(...)`

All support kwargs and flexible styling.
{% endtab %}
{% endtabs %}

## 11. Standard Library

{% tabs %}
{% tab title="v1 (Legacy)" %}
Minimal helper set.
{% endtab %}

{% tab title="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`
{% endtab %}
{% endtabs %}

## Summary

kScript v2 delivers major improvements over v1:

{% hint style="success" %}
**Per-bar execution** eliminates manual time alignment
{% endhint %}
{% hint style="success" %}
**Keyword arguments** make functions clear and self-documenting
{% endhint %}
{% hint style="success" %}
**Compile-time checking** prevents most runtime surprises
{% endhint %}
{% hint style="success" %}
**Dedicated subscription functions** improve clarity
{% endhint %}
{% hint style="success" %}
**Technical indicators return scalars** per bar instead of entire timeseries
{% endhint %}
{% hint style="success" %}
**Field accessors** make OHLCV data ergonomic
{% endhint %}
{% hint style="success" %}
**Reverse indexing** gives direct access to the latest data
{% endhint %}
{% hint style="success" %}
**Functions and loops** enable modular and expressive code
{% endhint %}
{% hint style="success" %}
**Extended plot functions and library** unlock richer visualization and analysis
{% endhint %}

Overall, v2 is more **intuitive, safer, and expressive** — while maintaining performance for real-time charting.
