Core Concepts

Data Types

Understanding kScript's type system and the fundamental data types available for building trading algorithms.

Core Concept 5 min read

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.

lines wrap
var price = 45000.5;
var volume = 1000;

string

Text data used for labels, symbols, exchange names, and configuration values. Essential for identifying assets and displaying information.

lines wrap
var symbol = 'BTCUSDT';
var exchange = 'BINANCE';

boolean

True/false values used for conditional logic, flags, and decision making. Critical for implementing trading conditions and control flow.

lines wrap
var isUptrend = true;
var showSignals = false;

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.

lines wrap
plotLine(
  (value = condition ? value : na),
  (width = 2),
  (colors = ['red']),
  (label = ['Conditional']),
  (desc = ['Conditional Value']),
);

Core kScript Types

Essential types specific to kScript's trading-focused architecture. These handle time-series data and market information.

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.

lines wrap
timeseries ohlcvData = ohlcv(currentSymbol, currentExchange);
timeseries rsiData = rsi(source=ohlcvData.close, period=14);

Key Features:

  • 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

Types used for user inputs and script configuration. These help create customizable indicators with user-friendly interfaces.

select

Dropdown selection type for input parameters with predefined choices. Used with input() function to create user-friendly dropdown menus.

lines wrap
var plotType = input(
  (name = 'mode'),
  (type = 'select'),
  (options = ['Line', 'Bar']),
  (defaultValue = 'Bar'),
  (label = 'Plot Type'),
);

DataSource

String identifiers for different types of market data available in the platform. Used with source() function to specify what data to fetch.

IdentifierDescription
"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.

ValueDescription
"onchart"Plotted on the main price chart
"offchart"Plotted in a separate panel below

Visual & Plotting Types

Types specifically for chart visualization and plotting functions. These control the appearance and style of your indicators.

color

Color values for chart visualization and plotting. Supports hex codes, named colors, and RGB values.

lines wrap
var lineColor = '#FF6B35';
var fillColor = '#008080';
var colors = ['red', 'green', 'blue'];

ShapeType

Defines the type of shape to render when using plotShape function for marking signals and annotations.

lines wrap
plotShape(
  (value = close),
  (shape = 'circle'),
  (width = 2),
  (colors = ['red']),
  (fill = true),
  (label = ['Close']),
  (desc = ['Close Price Shape']),
);

Practical Examples

Real-world examples showing how different data types work together in kScript to create useful trading indicators and visualizations.

Color Arrays for Multi-Line Plots

Use color arrays to style multiple data series with different colors:

lines wrap
//@version=2

define(title="Multi-MA Indicator", position="onchart", axis=true);

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange);

// Calculate multiple moving averages
timeseries sma20 = sma(source=trade.close, period=20);
timeseries sma50 = sma(source=trade.close, period=50);

// Use color arrays for different lines
var colors = ["#FF6B35", "#3B82F6", "#10B981"];

plotLine(value=sma20, width=2, colors=colors, colorIndex=0, label=["SMA 20"], desc=["20-period Simple Moving Average"]);
plotLine(value=sma50, width=2, colors=colors, colorIndex=1, label=["SMA 50"], desc=["50-period Simple Moving Average"]);

Conditional Plotting with na

Combine boolean conditions with na to create indicators that only show when specific conditions are met:

lines wrap
//@version=2

define(title="50BTC Limit", position="offchart", axis=true);

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var limit = 50;

// Only plot when volume exceeds limit, otherwise use na
plotLine(value=(trade.volume > limit ? trade.close : na), width=2, colors=["red"], label=["Volume Filtered"], desc=["Volume Filtered Price"])

Type Conversion & Indexing

TimeSeries Indexing

Access specific price components from OHLCV data:

IndexField
0Timestamp
1Open
2High
3Low
4Close
5Volume

Creating TimeSeries

Extract specific data from multi-dimensional TimeSeries:

lines wrap
timeseries trade = ohlcv(currentSymbol, currentExchange);

// Extract close prices
var close = trade.close;

// Extract volume
var volume = trade.volume;

// Using ShapeType for plotting
plotShape(value=close, shape="circle", width=2, colors=["red"], fill=true, label=["Close"], desc=["Close Price Shape"]);

Best Practices

Use Descriptive Names

Choose clear variable names that indicate the data type and purpose:

timeseries trade = ohlcv(currentSymbol, currentExchange);
var close = trade.close;
Use Appropriate Types

Choose the right data type for your use case to ensure type safety:

var showMA = input("showMA", "boolean", true);