Overview

kScript v2 introduces user-defined functions, allowing you to encapsulate logic into reusable modules. Functions can accept both var and timeseries arguments, support both positional and named parameter calling (kwargs), and must follow specific scoping rules.

func
Keyword
Infinite
Possibilities
Global
Scope Required

Function Declaration

Basic Syntax

Function Declaration Syntax

Kwargs Support

User-defined functions support both positional and named parameter calling conventions.

Function Call Formats

Function Name

Must follow standard identifier rules (letters, numbers, underscore). Cannot start with a number.

Parameters

Can accept var and timeseries arguments. Parameter types are inferred from usage. Support both positional and named parameter calling (kwargs).

Return Statement

Functions must explicitly return a value. The return type is inferred from the returned expression.

Function Examples

Simple Calculation

Basic mathematical operations with error handling.

Safe Division Function

Constraints and Rules

🚫

No Timeseries Declarations

Functions cannot declare new timeseries inside their body. All timeseries must be declared in global scope.

❌ Invalid:
func bad() { timeseries ts = ohlcv(...) }
⚠️

Global Scope Only

Functions must be declared in global scope, not inside loops, conditionals, or other functions.

❌ Invalid:
if (condition) { func bad() {...} }
💡

Parameter Types

Functions can accept var and timeseries arguments. Types are inferred from how parameters are used.

✅ Valid:
func good(varParam, tsParam) {...}

Return Requirement

All functions must have an explicit return statement. The return type is inferred from the expression.

✅ Required:
return someValue
📖
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