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
PrimitiveRepresents numerical values including integers and floating-point numbers. Used for prices, volumes, calculations, and mathematical operations.
var price = 45000.50;
var volume = 1000;
string
PrimitiveText data used for labels, symbols, exchange names, and configuration values. Essential for identifying assets and displaying information.
var symbol = "BTCUSDT";
var exchange = "BINANCE";
boolean
PrimitiveTrue/false values used for conditional logic, flags, and decision making. Critical for implementing trading conditions and control flow.
var isUptrend = true;
var showSignals = false;
na
SpecialRepresents "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.
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
CoreThe 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.
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
InputDropdown selection type for input parameters with predefined choices. Used with input() function to create user-friendly dropdown menus for configuration options.
var plotType = input(name="mode", type="select", options=["Line", "Bar"], defaultValue="Bar", label="Plot Type")
DataSource
InputString identifiers for different types of market data available in the platform. Used with source() function to specify what data to fetch.
"ohlcv"
- Price and volume data"funding_rate"
- Funding rates"liquidations"
- Liquidation data Position
ConfigurationDetermines where your indicator appears on the chart interface. Used in the define() function to control indicator placement.
"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
VisualColor values for chart visualization and plotting. Supports hex codes, named colors, and RGB values for styling your indicators.
var lineColor = "#FF6B35";
var fillColor = "#008080";
var colors = ["red", "green", "blue"];
ShapeType
VisualDefines the type of shape to render when using plotShape function for marking signals and annotations on the chart.
"circle"
- Round markers for signals 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. Essential for creating multi-timeframe or multi-asset indicators.
Conditional Plotting with na
Combine boolean conditions with na to create indicators that only show when specific market conditions are met, reducing visual noise.
Type Conversion & Indexing
How to work with different data types and extract values from TimeSeries.
TimeSeries Indexing
Access specific price components from OHLCV data:
0
Timestamp1
Open2
High3
Low4
Close5
VolumeCreating TimeSeries
Extract specific data from multi-dimensional TimeSeries:
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);