PINE LIBRARY
Updated

Padding

The Padding library is a comprehensive and flexible toolkit designed to extend time series data within TradingView, making it an indispensable resource for advanced signal processing tasks such as FFT, filtering, convolution, and wavelet analysis. At its core, the library addresses the common challenge of edge effects by "padding" your data—that is, by appending additional data points beyond the natural boundaries of your original dataset. This extension not only mitigates the distortions that can occur at the endpoints but also helps to maintain the integrity of various transformations and calculations performed on the series. The library accomplishes this while preserving the ordering of your data, ensuring that the most recent point always resides at index 0.

Central to the functionality of this library are two key enumerations: Direction and PaddingType. The Direction enum determines where the padding will be applied. You can choose to extend the data in the forward direction (ahead of the current values), in the backward direction (behind the current values), or in both directions simultaneously. The PaddingType enum defines the specific method used for extending the data. The library supports several methods—including symmetric, reflect, periodic, antisymmetric, antireflect, smooth, constant, and zero padding—each of which has been implemented to suit different analytical scenarios. For instance, symmetric padding mirrors the original data across its boundaries, while reflect padding continues the trend by reflecting around endpoint values. Periodic padding repeats the data, and antisymmetric padding mirrors the data with alternating signs to counterbalance it. The antireflect and smooth methods take into account the derivatives of your data, thereby extending the series in a way that preserves or smoothly continues these derivative values. Constant and zero padding simply extend the series using fixed endpoint values or zeros. Together, these enums allow you to fine-tune how your data is extended, ensuring that the padding method aligns with the specific requirements of your analysis.

The library is designed to work with both single variable inputs and array inputs. When using array-based methods—particularly with the antireflect and smooth padding types—please note that the implementation intentionally discards the last data point as a result of the delta computation process. This behavior is an important consideration when integrating the library into your TradingView studies, as it affects the overall data length of the padded series. Despite this, the library’s structure and documentation make it straightforward to incorporate into your existing scripts. You simply provide your data source, define the length of your data window, and select the desired padding type and direction, along with any optional parameters to control the extent of the padding (using both_period, forward_period, or backward_period).

In practical application, the Padding library enables you to extend historical data beyond its original range in a controlled and predictable manner. This is particularly useful when preparing datasets for further signal processing, as it helps to reduce artifacts that can otherwise compromise the results of your analytical routines. Whether you are an experienced Pine Script developer or a trader exploring advanced data analysis techniques, this library offers a robust solution that enhances the reliability and accuracy of your studies by ensuring your algorithms operate on a more complete and well-prepared dataset.

Library "Padding"
A comprehensive library for padding time series data with various methods. Supports both single variable and array inputs, with flexible padding directions and periods. Designed for signal processing applications including FFT, filtering, convolution, and wavelets. All methods maintain data ordering with most recent point at index 0.

symmetric(source, series_length, direction, both_period, forward_period, backward_period)
  Applies symmetric padding by mirroring the input data across boundaries
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with symmetric padding applied

method symmetric(source, direction, both_period, forward_period, backward_period)
  Applies symmetric padding to an array by mirroring the data across boundaries
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with symmetric padding applied

reflect(source, series_length, direction, both_period, forward_period, backward_period)
  Applies reflect padding by continuing trends through reflection around endpoint values
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with reflect padding applied

method reflect(source, direction, both_period, forward_period, backward_period)
  Applies reflect padding to an array by continuing trends through reflection around endpoint values
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with reflect padding applied

periodic(source, series_length, direction, both_period, forward_period, backward_period)
  Applies periodic padding by repeating the input data
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with periodic padding applied

method periodic(source, direction, both_period, forward_period, backward_period)
  Applies periodic padding to an array by repeating the data
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with periodic padding applied

antisymmetric(source, series_length, direction, both_period, forward_period, backward_period)
  Applies antisymmetric padding by mirroring data and alternating signs
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with antisymmetric padding applied

method antisymmetric(source, direction, both_period, forward_period, backward_period)
  Applies antisymmetric padding to an array by mirroring data and alternating signs
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with antisymmetric padding applied

antireflect(source, series_length, direction, both_period, forward_period, backward_period)
  Applies antireflect padding by reflecting around endpoints while preserving derivatives
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with antireflect padding applied

method antireflect(source, direction, both_period, forward_period, backward_period)
  Applies antireflect padding to an array by reflecting around endpoints while preserving derivatives
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with antireflect padding applied. Note: Last data point is lost when using array input

smooth(source, series_length, direction, both_period, forward_period, backward_period)
  Applies smooth padding by extending with constant derivatives from endpoints
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with smooth padding applied

method smooth(source, direction, both_period, forward_period, backward_period)
  Applies smooth padding to an array by extending with constant derivatives from endpoints
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with smooth padding applied. Note: Last data point is lost when using array input

constant(source, series_length, direction, both_period, forward_period, backward_period)
  Applies constant padding by extending endpoint values
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with constant padding applied

method constant(source, direction, both_period, forward_period, backward_period)
  Applies constant padding to an array by extending endpoint values
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with constant padding applied

zero(source, series_length, direction, both_period, forward_period, backward_period)
  Applies zero padding by extending with zeros
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with zero padding applied

method zero(source, direction, both_period, forward_period, backward_period)
  Applies zero padding to an array by extending with zeros
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with zero padding applied

pad_data(source, series_length, padding_type, direction, both_period, forward_period, backward_period)
  Generic padding function that applies specified padding type to input data
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    padding_type (series PaddingType): Type of padding to apply (see PaddingType enum)
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with specified padding applied

method pad_data(source, padding_type, direction, both_period, forward_period, backward_period)
  Generic padding function that applies specified padding type to array input
  Namespace types: array<float>
  Parameters:
    source (array<float>): Array of values to pad
    padding_type (series PaddingType): Type of padding to apply (see PaddingType enum)
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to array length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to array length if not specified
  Returns: Array ordered with most recent point at index 0, containing original data with specified padding applied. Note: Last data point is lost when using antireflect or smooth padding types

make_padded_data(source, series_length, padding_type, direction, both_period, forward_period, backward_period)
  Creates a window-based padded data series that updates with each new value. WARNING: Function must be called on every bar for consistency. Do not use in scopes where it may not execute on every bar.
  Parameters:
    source (float): Input value to pad from
    series_length (int): Length of the data window
    padding_type (series PaddingType): Type of padding to apply (see PaddingType enum)
    direction (series Direction): Direction to apply padding
    both_period (int): Optional - periods to pad in both directions. Overrides forward_period and backward_period if specified
    forward_period (int): Optional - periods to pad forward. Defaults to series_length if not specified
    backward_period (int): Optional - periods to pad backward. Defaults to series_length if not specified
  Returns: Array ordered with most recent point at index 0, containing windowed data with specified padding applied
Release Notes
v2
Make delta for arrays has been corrected. It was originally using i - 1, and that was fixed to be 1 + 1

Disclaimer