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 resetsNamed Parameters
Functions use clear names like
period=20 instead of just numbersHow 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:- var
- timeseries
- static
Temporary Calculations - Resets on each candle. Use for settings, thresholds, and single-candle calculations.
Quick Comparison
var | timeseries | static | |
|---|---|---|---|
| Resets each candle? | Yes | N/A | No |
| Access history? | No | Yes | No |
| Where to use? | Anywhere | Top level | Top level |
Core Variables
Learn more about variables with detailed examples
Data Types
kScript supports several data types for building indicators:| Type | Description | Example |
|---|---|---|
number | Prices, volumes, calculations | 45000.50, 1000 |
string | Text like symbols and labels | "BTCUSDT", "BINANCE" |
boolean | True/false conditions | true, false |
na | Missing values (creates gaps in plots) | na |
TimeSeries | Historical data with indexing | prices[0], prices[1] |
Data Types
Learn more about data types
Getting Market Data
kScript gives you access to various types of market data:| Function | Data Type | Description |
|---|---|---|
ohlcv() | Price & Volume | Get open, high, low, close, and volume data |
funding_rate() | Funding Rates | Track funding rates in perpetual futures |
liquidations() | Liquidation Data | Monitor liquidation events and volumes |
open_interest() | Open Interest | Track total open positions in futures |
source() | Universal | Access any data type with type= parameter |
Data Sources
View all available data sources
Exchange & Symbol Formats
| Exchange | exchange Parameter | symbol Format |
|---|---|---|
| Binance Spot | "BINANCE" | "BTCUSDT" |
| Binance Futures | "BINANCE_FUTURES" | "BTCUSDT" |
| Coinbase | "COINBASE" | "BTC-USD" |
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 usingfunc:
User Functions
Learn more about writing functions
Available Functions
kScript provides a rich library of functions organized into categories:Script Setup
define(), input()Plotting
plotLine(), plotBar(), plotCandle()Moving Averages
sma(), ema(), wma(), vwma()Oscillators
rsi(), stoch(), cci(), macd()Trend Indicators
adx(), supertrend(), ichimoku()Volume Indicators
obv(), mfi(), vwap()Orderbook
orderbook_depth(), bid_ask_spread()Math
abs(), min(), max(), sum()Utility
highest(), lowest(), crossover()Keyword Arguments
Learn more about keyword arguments
Important Rules to Remember
Every indicator needs define()
Every indicator needs define()
Always start with exactly one
define(...) call, or your indicator won’t work.About var variables
About var variables
- 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
About timeseries variables
About timeseries variables
- 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
Getting data
Getting data
Use
ohlcv(symbol=..., exchange=...) for price data, or source(type=..., symbol=..., exchange=...) for other data types. Both return timeseries.Performance limits
Performance limits
Your indicator must finish calculating within 500 milliseconds, or it will stop with an error.
Debugging tips
Debugging tips
Use
print(...) or printTimeSeries(...) to see values. Check the Data Table panel to inspect your indicator’s output.