Overview

Conditional Statements

Use if/else statements to execute different code paths based on conditions. Essential for decision-making logic in your indicators.

• Condition-based execution • Multiple branches • Boolean logic

For Loops

Use for loops when you know the number of iterations in advance. Perfect for counted iterations and array-like processing.

• Known iteration count • Counter-based control • Array-like processing

While Loops

Use while loops when the number of iterations depends on a condition. Ideal for search patterns and conditional iteration.

• Condition-based control • Dynamic iteration count • Search patterns

Functions Reference

if/else - conditional execution statements

if (condition) { /* body */ } else { /* alternative */ }

Parameters:

  • condition (boolean expression) - Expression that evaluates to true or false
  • if body (code block) - Code executed when condition is true
  • else body (code block) - Code executed when condition is false (optional)

Returns:

void (executes conditional code)

Code Example:

for - counted iteration loop

for (var i = start; i < end; i = i + increment) { /* body */ }

Parameters:

  • initialization (var declaration) - Initialize the loop counter variable
  • condition (boolean expression) - Loop continues while this condition is true
  • increment (assignment) - Update the counter variable each iteration

Returns:

void (executes loop body)

Code Example:

while - conditional iteration loop

while (condition) { /* body */ }

Parameters:

  • condition (boolean expression) - Loop continues while this condition is true

Returns:

void (executes loop body)

Code Example:

Best Practices

⚠️

Infinite Loop Protection

Always ensure loop conditions will eventually become false. Infinite loops will cause your script to hang and may be terminated by the runtime.

💡

Variable Scope

Loop variables like i are scoped to the loop. Use var declarations inside loops for temporary calculations.

🚀

Performance

Loops run during the calculation phase of each bar. Keep iterations reasonable to maintain good performance across large datasets.

🚫

Timeseries Restriction

You cannot declare timeseries inside loops. Timeseries must be declared in global scope only.

📖
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
🆕
Updates
v1 vs v2 differences and improvements
🔧
Core Concepts
Variables, data types & data sources
⚙️
Execution Model
Per-bar execution lifecycle and phases
🏷️
Keyword Arguments
Named parameters for clear function calls
🔗
Field Accessors
Dot notation for timeseries field access
🛠️
User-Defined Functions
Create custom reusable functions
🔗
Script Definition
Defining inputs and metadata
TimeSeries Management
Working with time-aligned data
🎯
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
📊
Data Subscriptions
Subscribe to OHLCV, trades, and orderbook data
🔄
Loops
For loops and while loops for iteration
🧮
Math Functions
Mathematical functions and constants
🏠
Go Home
Return to main landing page