Every indicator follows a simple recipe: Setup once → Calculate per candle → Display per candle

🎬

Phase 1: Setup

Runs once when your indicator first loads

Prepare everything your indicator needs before processing any candles.

Call define(...)Required • Tells kScript about your indicator
⚙️
Create settings with input(...)Optional • Let users adjust period, colors, etc.
📊
Get data with ohlcv(...)Create all timeseries here
🧮

Phase 2: Calculate

Runs for each candle on the chart

Process indicator calculations. This repeats for every candle, computing values based on your defined logic.

📈
Calculate indicatorsrsi(...), ema(...), sma(...)
🔢
Store temporary values in varFor comparisons, colors, or per-candle logic
🎯
Make decisionsUse if/else to compare values
⚠️ Don't create timeseries here — use the ones from Phase 1
🎨

Phase 3: Display

Runs for each candle, after calculations

Draw your indicator on the chart using the calculated values.

📉
plotLine(...)Lines for RSI, moving averages, etc.
📊
plotBar(...)Vertical bars for volume, histograms
📍
plotShape(...)Markers, arrows, labels for signals
💡 You can use multiple plot functions in one indicator

Understanding the Flow

Phase 1

Setup runs once

Phase 2

Calculate for candle 1

Phase 3

Display candle 1

Phase 2

Calculate for candle 2

Phase 3

Display candle 2

...and so on for every candle

Key Characteristics

Deterministic Results

Given the same input data, a script will always produce identical output. This property is essential for reliable backtesting and strategy validation.

Immutable Historical Data

timeseries objects provide read-only access to historical values. Past data cannot be modified, ensuring data integrity throughout execution.

Efficient Memory Usage

Only current bar calculations are held in memory. Historical data is managed by the runtime with optimized caching strategies.

Real-time Compatibility

The same script logic handles both historical analysis and live data processing without requiring different code paths or special handling.

Limitations and Constraints

⚠️

No Future Data Access

Scripts cannot access data from future bars (e.g., ts[-1] is invalid). This prevents look-ahead bias in analysis.

🚫

Global Scope Timeseries Only

timeseries declarations must be in global scope. They cannot be declared inside functions, loops, or conditional blocks.

⏱️

Execution Time Limits

Scripts must complete execution within 500ms (excluding data fetch). This ensures responsive chart rendering and prevents infinite loops.

🔒

No Cross-Bar Variable Persistence

var variables cannot maintain state between bars. Use timeseries for values that need historical access.

📖
Introduction
Overview of kScript language
🔍
Overview
Complete technical documentation
🚀
Quick Start
Get started with kScript basics
📋
Function Reference
Complete API reference guide
📚
Type System
Understanding kScript data types
General FAQ
Frequently asked questions about kScript
Best Practices
Guidelines for writing efficient kScript code
⚠️
Limitations
Known constraints and workarounds
🌐
Exchange & Symbol Format
List of supported exchanges and symbol formats
🆕
Updates
v1 vs v2 differences and improvements
🔧
Variables
Core variables and data handling
📊
Data Sources
Subscribe to OHLCV, trades, and orderbook data
⚙️
Execution Model
Per-bar execution lifecycle and phases
🏷️
Keyword Arguments
Named parameters for clear function calls
🛠️
User-Defined Functions
Create custom reusable functions
🔗
Script Definition
Defining inputs and metadata
🎯
Utility Functions
Helper functions and calculations
📈
Moving Averages
SMA, EMA and trend-following indicators
📊
Oscillators
RSI, Stochastic and momentum indicators
📈
Trend Indicators
Trend direction and strength analysis
📉
Volume Indicators
Volume-based analysis tools
📦
Orderbook Functions
Market depth analysis tools
🎨
Plotting & Visualization
Chart rendering and styling
🌈
Color Functions
Color manipulation and styling
🔄
Loops
For loops and while loops for iteration
🧮
Math Functions
Mathematical functions and constants
📝
String Functions
String manipulation and transformation
🏠
Go Home
Return to main landing page