Utility Functions
Helper functions for statistical calculations and data manipulation. Essential building blocks for creating custom indicators and performing mathematical operations on time series data.
Overview
Statistical Functions
Calculate statistical measures like standard deviation, highest/lowest values, and sums over specified periods for robust technical analysis.
Cross Detection
Detect when time series cross above, below, or in either direction to identify trend changes and generate trading signals.
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.
Type Checking
Validate data integrity and handle edge cases by checking for NaN values and ensuring numeric validity before performing calculations.
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.