Core Concept5 min read
Understanding kScript’s type system and the fundamental data types available for building trading algorithms.
Primitive Data Types
The fundamental data types that form the building blocks of kScript. These are the basic values you’ll work with in your trading algorithms.number
Represents numerical values including integers and floating-point numbers. Used for prices, volumes, calculations, and mathematical operations.
string
Text data used for labels, symbols, exchange names, and configuration values. Essential for identifying assets and displaying information.
boolean
True/false values used for conditional logic, flags, and decision making. Critical for implementing trading conditions and control flow.
na
Represents “not a number” or missing values. When used in plots, those data points won’t be rendered, creating gaps in the visualization. Perfect for conditional indicators.
Core kScript Types
TimeSeries
The fundamental data structure for time-aligned market data. All price data and indicators are TimeSeries. This is how kScript handles historical data with indexing support.
- Historical Access: Use
ts[0],ts[1],ts[n]for current and past values - Multi-field Data: OHLCV exposes
.open,.high,.low,.close,.volume - Immutable: Values cannot be changed once set
- Global Scope: Must be declared at the top level
Input & Configuration Types
select
Dropdown selection type for input parameters with predefined choices. Used with input() function to create user-friendly dropdown menus.
DataSource
String identifiers for different types of market data available in the platform. Used with source() function to specify what data to fetch.
| Identifier | Description |
|---|---|
"ohlcv" | Price and volume data |
"funding_rate" | Funding rates |
"liquidations" | Liquidation data |
Position
Determines where your indicator appears on the chart interface. Used in the define() function.
| Value | Description |
|---|---|
"onchart" | Plotted on the main price chart |
"offchart" | Plotted in a separate panel below |
Visual & Plotting Types
color
Color values for chart visualization and plotting. Supports hex codes, named colors, and RGB values.
ShapeType
Defines the type of shape to render when using plotShape function for marking signals and annotations.
Practical Examples
Color Arrays for Multi-Line Plots
Use color arrays to style multiple data series with different colors:Conditional Plotting with na
Combine boolean conditions withna to create indicators that only show when specific conditions are met:
Type Conversion & Indexing
TimeSeries Indexing
Access specific price components from OHLCV data:| Index | Field |
|---|---|
| 0 | Timestamp |
| 1 | Open |
| 2 | High |
| 3 | Low |
| 4 | Close |
| 5 | Volume |