Notice!
This is documentation for kScript v1.0, which may not include the latest features. For up-to-date documentation, see the latest version.

Overview

Simple Moving Average (SMA)

Arithmetic mean of prices over a specified period. Equal weight given to all data points.

• Smooth trend identification • Support/resistance levels • Crossover signals

Exponential Moving Average (EMA)

Weighted average giving more importance to recent prices. More responsive to price changes.

• Faster trend detection • Reduced lag • Better for volatile markets

Functions Reference

sma - arithmetic mean over specified period

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

Parameters:

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

Returns:

TimeSeries (simple moving average values)

Code Example:

ema - weighted average giving more weight to recent prices

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

Parameters:

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

Returns:

TimeSeries (exponential moving average values)

Code Example:

_sma - Simple Moving Average helper function

_sma(barIndex: number, series: TimeSeries, period: number, priceIndex?: number = 12): number

Parameters:

  • barIndex (number) - Current bar index
  • series (TimeSeries) - Source data series
  • period (number) - Number of periods for calculation
  • priceIndex (number) - Index of price data (default: 12)

Returns:

number (moving average value at specific bar)

Code Example:

_ema - Exponential Moving Average helper function

_ema(barIndex: number, series: TimeSeries, period: number, priceIndex?: number = 12): number

Parameters:

  • barIndex (number) - Current bar index
  • series (TimeSeries) - Source data series
  • period (number) - Number of periods for calculation
  • priceIndex (number) - Index of price data (default: 12)

Returns:

number (exponential moving average value at specific bar)

Code Example:

Common Usage Patterns

Golden Cross Strategy

Detect bullish momentum when fast MA crosses above slow MA

Price Support/Resistance

Use moving averages as dynamic support and resistance levels

Multiple MA Analysis

Combine different period moving averages for trend confirmation

Best Practices

💡

Period Selection

Common periods: 20 (short-term), 50 (medium-term), 200 (long-term). Adjust based on trading timeframe and market volatility.

EMA vs SMA

Use EMA for faster signals in trending markets, SMA for smoother signals in ranging markets. EMA reacts quicker to price changes.

📊

Confirmation

Combine moving averages with volume, RSI, or other indicators for stronger signal confirmation. Never rely on MA alone.

🎯

False Signals

Moving averages lag price action. In choppy markets, use additional filters to reduce whipsaws and false breakouts.

📖
Introduction
Overview of kScript language
🚀
Quick Start
Get started with kScript basics
📋
Function Reference
Complete API reference guide
🔧
Core Concepts
Variables, data types & data sources
🔗
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
🏠
Go Home
Return to main landing page