Code Structure

Fundamental patterns for organizing your kScript code.

Use the appropriate variable type for your data.

Static variables for constants:

Use for constants and values that never change across bars (e.g., periods, thresholds, multipliers).

Regular variables for dynamic values:

Use for dynamic values that change per bar but don't need historical access (e.g., signals, calculations).

Timeseries for historical data:

Use only when absolutely necessary as they consume more memory. Declare as timeseries when you need to:

  • Store data source outputs: timeseries price_data = ohlcv(...)
  • Access past values at previous candle indices: sma_values[5]

While indicator functions like sma(), ema(), and rsi() require a timeseries as input, their return values should be stored as var unless you specifically need to access historical values (e.g., sma_values[5]). This saves memory and improves performance.

Technical Indicators

Guidelines for working with technical indicators effectively.

Always specify parameters explicitly for clarity. This makes your code more readable and maintainable.

Use bracket notation to access historical values safely.

Plotting & Visualization

Create clear and informative visualizations.

Use meaningful colors and widths to make your plots easy to distinguish.

Select the right plot function for your data type.

💡 Plot Types
  • plotLine() for continuous lines
  • plotBar() for histogram data
  • plotCandle() for OHLC data
  • plotShape() for discrete signals

Debugging

Best practices for debugging and inspecting your kScript code.

Always use printTimeSeries() for timeseries objects instead of print(). The print() function only outputs the first value (e.g., only "open" for OHLCV data), while printTimeSeries() displays the complete timeseries structure.

Error Prevention

Protect your code against common runtime errors.

Always check for invalid data before performing calculations.

Protect against division by zero errors.

Be careful when accessing historical data to avoid index errors.

Performance Optimization

Write efficient code that executes quickly.

Store frequently used calculations in variables instead of recalculating.

Declare constants as static to avoid unnecessary recalculation on every bar.

Code Organization

Structure your code for maximum readability and maintainability.

Organize your code into logical sections: inputs, data sources, calculations, signals, and plotting.

Write clear comments that explain the "why" behind your code logic.

Always include label and desc parameters in your plotting functions. These parameters improve autocomplete functionality, make scripts more readable, and help with script organization in the editor.

📖
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