Basic Data Types
kScript provides several fundamental data types that form the building blocks of your trading algorithms.
number
PrimitiveRepresents numerical values including integers and floating-point numbers.
let price = 45000.50;
let volume = 1000;
string
PrimitiveText data used for labels, symbols, and configuration values.
let symbol = "BTCUSDT";
let exchange = "BINANCE";
boolean
PrimitiveTrue/false values used for conditional logic and flags.
let isUptrend = true;
let showSignals = false;
color
VisualColor values for chart visualization and plotting.
let lineColor = "#FF6B35";
let fillColor = "#008080";
ShapeType
VisualDefines the type of shape to render when using plotShape function for marking signals and annotations.
"circle"
- Circular markersAdditional shapes may be available
Complex Data Types
Advanced types for handling time-series data and market information.
TimeSeries
CoreThe fundamental data structure for time-aligned market data. All price data and indicators are TimeSeries.
timeseries ohlcv = source("ohlcv", currentSymbol, currentExchange);
timeseries sma20 = sma(ohlcv, 20, 4);
Structure:
- Timestamp - Unix timestamp for each data point
- Values - Array of numerical data (OHLCV)
- Indexing - Access individual price components
DataSource
InputIdentifiers for different types of market data available in the platform.
"ohlcv"
- Price and volume data"funding_rate"
- Futures funding rates"liquidations"
- Liquidation events Position
ConfigurationDetermines where your indicator appears on the chart interface.
"onchart"
- On the main price chart"offchart"
- In a separate panel below Array Types
kScript supports arrays for handling multiple values of the same type.
Color Arrays
Used for multi-line plots with different colors.
Number Arrays
For storing multiple numerical values or calculations.
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 close_prices = timeseries(ohlcv, 4);
Minimize Type Conversions
Work with native TimeSeries types when possible for better performance.
// Good: Direct TimeSeries operations
timeseries ma = sma(ohlcv, 20, 4);
Use Appropriate Types
Choose the right data type for your use case to ensure type safety.
// Use boolean for flags
boolean showMA = input("showMA", true);