Advanced15 min read
kScript’s type system is specifically designed for financial data analysis, featuring a hybrid approach that combines static analysis at compile time with dynamic execution at runtime.
Introduction
The compiler performs type checking with limited implicit conversions, helping catch type mismatches early during compilation rather than encountering them at runtime. At its core, kScript distinguishes between two fundamental categories of values: primitive types (numbers, strings, booleans) and time-series types. Thetimeseries type is the central data structure for handling historical market data, indicators, and any values that change across chart bars.
Type Inference: In kScript, you don’t explicitly declare types like
int or float. Instead, types are inferred from the values and expressions you write. The distinction you make is between regular variables (var) and time-series data (timeseries).Variable Declarations and Type Inference
In kScript, variable declarations use two keywords:var and timeseries. The choice between them fundamentally determines how the variable behaves throughout your script’s execution.
The var Keyword
Variables declared with var hold scalar values: numbers, strings, booleans, or arrays of these types. These values can change from bar to bar, but they represent single values at any given point in time.
The type of a var variable is inferred from its initialization expression. Once inferred, the type remains consistent.
The timeseries Keyword
The timeseries keyword declares variables that hold historical data—values that exist across multiple bars with temporal alignment. A timeseries variable maintains a complete history that can be indexed to access past values.
Timeseries variables are immutable in the sense that you cannot reassign them once declared. However, you can create new timeseries through operations on existing ones.
Primitive Data Types
number
Represents all numeric values, including both integers and floating-point numbers. Used for prices, volumes, periods for technical indicators, thresholds, and mathematical calculations.
All standard arithmetic operators (+, -, *, /, %) work with numbers.
string
Represents textual data using double quotes. Essential for identifying trading pairs, exchanges, and providing labels. Strings are immutable.
boolean
Either true or false. Fundamental to conditional logic. Comparison operators (<, >, ==, !=) and logical operators (&&, ||, !) return boolean values.
na - Not Available
A special constant representing “not available” or missing data. When you pass na to a plot function, that data point is skipped, creating gaps in the visualization.
TimeSeries: kScript’s Core Type
What is a TimeSeries?
A timeseries is not just a single value—it’s a complete history of values aligned to chart bars. You can look backward in time using the history reference operator[].
Conceptually, a timeseries is like a two-dimensional array where each row represents a bar (timestamped) and each column represents a data field. Timeseries variables automatically track history.
Historical Indexing
To access previous values, use square bracket notation with an integer offset:[0](or no index) - current bar[1]- previous bar[2]- two bars ago
Immutability and Global Scope
Timeseries have two important constraints:- Immutable: Cannot be reassigned after declaration. Create new timeseries through operations instead.
- Global scope: Must be declared at the top level of your script, not inside functions or conditional blocks.
OHLCV Data Structure
| Index | Field | Description |
|---|---|---|
| 0 | timestamp | Unix timestamp (milliseconds) |
| 1 | open | Opening price for the bar |
| 2 | high | Highest price during the bar |
| 3 | low | Lowest price during the bar |
| 4 | close | Closing price for the bar |
| 5 | volume | Trading volume during the bar |
Arrays and Collections
Array Type System
kScript supports typed arrays:number[], string[], and boolean[]. Arrays are homogeneous—all elements must be of the same type.
- Multiple indicator parameters (periods, thresholds)
- Color schemes for multi-line plots
- Label sets for display options
- Configuration options for
input()with type"select"
Array Constraints
- Array size is typically fixed at declaration
- All elements must be of the same type (enforced at compile time)
- Arrays are passed by reference
Data Sources and Access Methods
kScript v2 provides extensive access to market data through data source functions.Two Access Methods
- Generic
source()function: Accepts atypeparameter for any data type - Specialized functions: Type-specific functions like
ohlcv(),open_interest(),funding_rate()
timeseries data that can be accessed historically.