Core Concept8 min read
Essential variables available globally in kScript for accessing current market context, symbols, and exchanges.
Variable Types & Behavior
Understanding kScript’s three variable types: how to declare them, their characteristics, and how they behave during script execution.var - Ephemeral, per-bar
Variables declared with var hold values that reset each bar.
Characteristics:
- Type inference: Types are inferred from the assigned value
- Scope: Can be declared in any scope (global, inside if/for/while, or inside func)
- Mutability: Values are mutable within the current bar
- No history: Cannot persist across bars - you cannot access
var[1]
timeseries - Immutable, historical
Variables declared with timeseries hold historical data that can be indexed.
Characteristics:
- Immutable: Values are immutable snapshots of historical data
- Historical access:
ts[0]is current bar;ts[1]is previous bar;ts[n]is n bars ago - Global only: Must be declared only in global scope (not inside if/for/while or within func bodies)
- Multi-field sources: Some timeseries expose multiple fields per bar (e.g., OHLCV:
.open,.high,.low,.close,.volume) - Source functions return timeseries: All source functions (e.g.,
ohlcv(),open_interest()) return atimeseries
static - Persistent, manual control
Variables declared with static maintain their values between bar executions.
Characteristics:
- Persistent state: Values maintain between bar executions
- Manual updates: Values don’t auto-update; must be explicitly modified
- Global scope: Declared in global scope and accessible throughout the script
- Manual operations: Support increment (
++), decrement (--), and assignment operations - State management: Ideal for counters, flags, and maintaining algorithm state
- Cumulative calculations: Useful for CVD (Cumulative Volume Delta) and similar indicators
Global Context Variables
These variables are automatically available in every kScript and provide access to the current trading context.| Variable | Type | Description |
|---|---|---|
currentSymbol | string | The currently active trading pair symbol |
currentExchange | string | The currently active exchange |
barIndex | number | Current bar index in the dataset |
Common Usage Patterns
Dynamic Data Source
Create flexible indicators that automatically use the current market context:Best Practices
Always Use Current Context
UsecurrentSymbol and currentExchange instead of hardcoding values for maximum flexibility: