Skip to main content
Beginner10 min read
A complete guide to understanding how kScript works. This page covers the core concepts, rules, and patterns you’ll use to build indicators.

Key Concepts

kScript is designed around a few core ideas that make building indicators straightforward:

Three-Phase Execution

Every indicator runs in three steps: Setup → Calculate → Display

Historical Data Access

timeseries lets you look at past values (e.g., prices[1] for previous candle)

Temporary Variables

var holds values for the current candle only, then resets

Named Parameters

Functions use clear names like period=20 instead of just numbers

How Indicators Run: Three Phases

1

Setup Phase

Runs once at start. Use define(), input(), and data sources like ohlcv().
2

Calculate Phase

Runs per candle. Use rsi(), ema(), if/else logic, and var variables.
3

Display Phase

Runs per candle. Use plotLine(), plotBar(), plotShape() to visualize.

Execution Model

View detailed execution model with examples

Three Ways to Store Data

kScript has three types of variables, each designed for a specific purpose:
Temporary Calculations - Resets on each candle. Use for settings, thresholds, and single-candle calculations.
var threshold = 70
var labelText = "Buy Signal"

Quick Comparison

vartimeseriesstatic
Resets each candle?YesN/ANo
Access history?NoYesNo
Where to use?AnywhereTop levelTop level

Core Variables

Learn more about variables with detailed examples

Data Types

kScript supports several data types for building indicators:
TypeDescriptionExample
numberPrices, volumes, calculations45000.50, 1000
stringText like symbols and labels"BTCUSDT", "BINANCE"
booleanTrue/false conditionstrue, false
naMissing values (creates gaps in plots)na
TimeSeriesHistorical data with indexingprices[0], prices[1]
Type Inference: You don’t need to specify types - kScript automatically figures out what type each variable is based on the value you assign.

Data Types

Learn more about data types

Getting Market Data

kScript gives you access to various types of market data:
FunctionData TypeDescription
ohlcv()Price & VolumeGet open, high, low, close, and volume data
funding_rate()Funding RatesTrack funding rates in perpetual futures
liquidations()Liquidation DataMonitor liquidation events and volumes
open_interest()Open InterestTrack total open positions in futures
source()UniversalAccess any data type with type= parameter
// Generic source
timeseries trades = source(type="ohlcv", symbol="BTCUSDT", exchange="BINANCE")

// OHLCV alias (preferred for price indicators)
timeseries ohlcvTs = ohlcv(symbol="BTCUSDT", exchange="BINANCE")

// Field access
timeseries openTs  = ohlcvTs.open
timeseries highTs  = ohlcvTs.high
timeseries lowTs   = ohlcvTs.low
timeseries closeTs = ohlcvTs.close
timeseries volTs   = ohlcvTs.volume
All data source functions return timeseries data that you can access historically using [0], [1], etc.

Data Sources

View all available data sources

Exchange & Symbol Formats

Exchangeexchange Parametersymbol Format
Binance Spot"BINANCE""BTCUSDT"
Binance Futures"BINANCE_FUTURES""BTCUSDT"
Coinbase"COINBASE""BTC-USD"
kScript supports data from multiple exchanges. Each exchange uses its own symbol format - some use hyphens like BTC-USDT, others use no separator like BTCUSDT.

Symbol Formats

View complete list of exchanges & symbol formats

Writing Your Own Functions

Break down complex logic into reusable functions using func:
func calculateAverage(a, b) {
  return (a + b) / 2;
}

var avg = calculateAverage(close[0], close[1]);

User Functions

Learn more about writing functions

Available Functions

kScript provides a rich library of functions organized into categories:
About “kwargs”: Short for “keyword arguments” - it means writing parameter names when calling functions, like period=20 instead of just 20. This makes your code self-documenting.Example: sma(source=prices, period=20) vs sma(prices, 20)

Keyword Arguments

Learn more about keyword arguments

Important Rules to Remember

Always start with exactly one define(...) call, or your indicator won’t work.
  • kScript automatically detects the type (number, text, true/false)
  • Can be used anywhere in your code
  • Only exists for the current candle - can’t access var[1]
  • Text variables can be displayed as labels
  • Values never change once set (read-only historical data)
  • Access history with [0] (now), [1] (previous), etc.
  • Must be created at the top level only - not inside loops or functions
  • Required for indicator calculations like rsi, ema, etc.
  • Price data includes .open, .high, .low, .close, .volume
Use ohlcv(symbol=..., exchange=...) for price data, or source(type=..., symbol=..., exchange=...) for other data types. Both return timeseries.
Your indicator must finish calculating within 500 milliseconds, or it will stop with an error.
Use print(...) or printTimeSeries(...) to see values. Check the Data Table panel to inspect your indicator’s output.

What’s Next?