Overview

Statistical Functions

Calculate statistical measures like standard deviation, highest/lowest values, and sums over specified periods for robust technical analysis.

• Standard deviation • Min/max values • Period sums

Cross Detection

Detect when time series cross above, below, or in either direction to identify trend changes and generate trading signals.

• Crossover detection • Crossunder detection • Bidirectional crosses

Support/Resistance Levels

Identify key price levels using mathematical functions that track highest highs and lowest lows over specified periods for breakout and reversal analysis.

• Dynamic channels • Breakout identification • Level confirmation

Type Checking

Validate data integrity and handle edge cases by checking for NaN values and ensuring numeric validity before performing calculations.

• NaN detection • Numeric validation • Error prevention

Functions Reference

lowest - find lowest value over specified period

lowest(source: TimeSeries, period?: number = 12, priceIndex?: number = 3): number

Parameters:

  • source (TimeSeries) - Source data series
  • period (number) - Number of periods to look back (default: 12)
  • priceIndex (number) - Index of price data (default: 3 for low)

Returns:

number (lowest value in the specified period)

Code Example:

highest - find highest value over specified period

highest(source: TimeSeries, period?: number = 12, priceIndex?: number = 2): number

Parameters:

  • source (TimeSeries) - Source data series
  • period (number) - Number of periods to look back (default: 12)
  • priceIndex (number) - Index of price data (default: 2 for high)

Returns:

number (highest value in the specified period)

Code Example:

sum - calculate sum of values over specified period

sum(source: TimeSeries, period?: number = 12, priceIndex?: number = 1): number

Parameters:

  • source (TimeSeries) - Source data series
  • period (number) - Number of periods to sum (default: 12)
  • priceIndex (number) - Index of price data (default: 1)

Returns:

number (sum of values in the specified period)

Code Example:

stddev - calculate standard deviation of values

stddev(source: TimeSeries, period?: number = 12, priceIndex?: number = 1): number

Parameters:

  • source (TimeSeries) - Source data series
  • period (number) - Number of periods for calculation (default: 12)
  • priceIndex (number) - Index of price data (default: 1)

Returns:

number (standard deviation value)

Code Example:

donchian - Donchian Channel midpoint calculation

donchian(source: TimeSeries, period?: number = 12): number

Parameters:

  • source (TimeSeries) - Source data series
  • period (number) - Number of periods (default: 12)

Returns:

number (Donchian Channel midpoint value)

Code Example:

crossover - detect when series A crosses above series B

crossover(seriesA: TimeSeries, seriesB: TimeSeries): boolean

Parameters:

  • seriesA (TimeSeries) - First series (e.g., fast moving average)
  • seriesB (TimeSeries) - Second series (e.g., slow moving average)

Returns:

boolean (true if A crosses above B at the current bar)

Code Example:

crossunder - detect when series A crosses below series B

crossunder(seriesA: TimeSeries, seriesB: TimeSeries): boolean

Parameters:

  • seriesA (TimeSeries) - First series
  • seriesB (TimeSeries) - Second series

Returns:

boolean (true when A crosses below B)

Code Example:

cross - detect when series A crosses series B in either direction

cross(seriesA: TimeSeries, seriesB: TimeSeries): boolean

Parameters:

  • seriesA (TimeSeries) - First series
  • seriesB (TimeSeries) - Second series

Returns:

boolean (true when any cross occurs)

Code Example:

isnan - check if a value is NaN (Not a Number)

isnan(value: any): boolean

Parameters:

  • value (any) - Value to check for NaN

Returns:

boolean (true if value is NaN, false otherwise)

Code Example:

isnum - check if a value is a valid finite number

isnum(value: any): boolean

Parameters:

  • value (any) - Value to check for numeric validity

Returns:

boolean (true if value is a valid finite number, false otherwise)

Code Example:

Best Practices

📊

Lookback Periods

Choose appropriate lookback periods based on your timeframe. Shorter periods for scalping, longer periods for swing trading.

Performance

Statistical functions can be computationally expensive. Cache results when calculating multiple statistics on the same data.

🎯

Cross Detection

Use crossover() for bullish signals, crossunder() for bearish signals, and cross() when you need to detect any direction change. Always combine with trend confirmation.

📈

Donchian Channels

Donchian channels work best in trending markets for breakout strategies. In ranging markets, use them as support/resistance levels rather than breakout signals.

⚠️

False Signals

Cross functions can generate false signals in choppy markets. Use additional filters like volume confirmation or trend direction to improve signal quality.

🔍

Data Validation

Always validate data using isnan() and isnum() before performing calculations. Invalid data can propagate through indicators and cause incorrect results.

📖
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