Library · Pine Script v5 · Library

SeasonalDecompositionByMovingAverage

by ricardosantos79· code credited to RicardoSantos

SeasonalDecompositionByMovingAverage is a Pine Script v5 library designed to compute seasonal decomposition elements from array data.

View on GitHub

GitHub stars261on the repository
Pine versionv5runs as is
Lines of code160
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

160 lines of Pine Script v5 from beta/libs/SeasonalDecomposition.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

SeasonalDecomposition.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Helper Methods for the creation of arrays.
library("SeasonalDecompositionByMovingAverage")
// reference:
//      https://tedboy.github.io/statsmodels_doc/_modules/statsmodels/tsa/seasonal.html

// @function computes means for each season frequency in data.
// @param sample float, array with data values.
// @param frequency int, seasonal frequency.
// @param remove_null bool, do not count null values, default is true.
// @param default_null_value string, value to fill null values, default is 'zero', options: 'zero', 'mean'.
// @returns int array
export seasonal_mean(float[] sample, int frequency, bool remove_null=true, string default_null_value='zero') => //{
// TODO: add conditional break overloads.
    int _samplesize = array.size(id=sample)
    float[] _means = array.new_float(size=frequency, initial_value=float(na))

    float _null_value = 0.0
    if default_null_value == 'mean'
        _null_value := array.avg(id=sample)
    for _freq = 0 to frequency-1
        float _sum = 0.0
        int _count = 0
        for _i = _freq to _samplesize-1 by frequency
            float _value = array.get(id=sample, index=_i)
            if remove_null and na(_value)
                continue
            else
                if na(_value)
                    _value := _null_value
                _sum += _value
                _count += 1
                continue
        array.set(id=_means, index=_freq, value=_sum / _count)
    _means
// usage:
if barstate.islast
    data = array.from(1, 2, 3, 4, 5, 6, 7, 8, float(na), float(na), float(na), float(na), float(na))
    m = seasonal_mean(data, 5, remove_null=false, default_null_value='mean')
    label.new(bar_index, 0.0, str.tostring(m))
//}

// @function computes the usual correlation 
//      (correlate(a1, a2) != correlate(a2, a1), 
//      and conjugate the second argument for complex inputs
// @param sample_a float array, 
// @param sample_b float array
// @param mode
// export correlate_1d (float[] sample_a, float[] sample_b, string mode) => //{
//     bool _inverted = false
//     int _size_a = array.size(id=sample_a)
//     int _size_b = array.size(id=sample_b)
//     float[] _A = sample_a
//     float[] _B = sample_b
//     if _size_b > _size_a
//         _A := sample_b
//         _B := sample_a
//         int _st = _size_a
//         _size_a := _size_b
//         _size_b := _st
//         _inverted := false
        
    
// reference:
//      https://github.com/numpy/numpy/blob/main/numpy/core/src/multiarray/multiarraymodule.c#L1170
//}

// @function 
// @param mode string, indicates the size of the output, options='full', 'valid', 'same'
// @returns
// export convolve(float[] sample_a, float[] sample_b, string mode='same') => //{
    // a, v = array(a, copy=False, ndmin=1), array(v, copy=False, ndmin=1)
    // int _size_a = array.size(id=sample_a)
    // int _size_b = array.size(id=sample_b)
    // if _size_b > _size_a
    //     correlate()
    //     a, v = v, a
    // if len(a) == 0:
    //     raise ValueError('a cannot be empty')
    // if len(v) == 0:
    //     raise ValueError('v cannot be empty')
    // return multiarray.correlate(a, v[::-1], mode)

// reference:
//      https://github.com/numpy/numpy/blob/v1.21.0/numpy/core/numeric.py#L748-L844

    // """
    // Returns the discrete, linear convolution of two one-dimensional sequences.
    // The convolution operator is often seen in signal processing, where it
    // models the effect of a linear time-invariant system on a signal [1]_.  In
    // probability theory, the sum of two independent random variables is
    // distributed according to the convolution of their individual
    // distributions.
    // If `v` is longer than `a`, the arrays are swapped before computation.
    // Parameters
    // ----------
    // a : (N,) array_like
    //     First one-dimensional input array.
    // v : (M,) array_like
    //     Second one-dimensional input array.
    // mode : {'full', 'valid', 'same'}, optional
    //     'full':
    //       By default, mode is 'full'.  This returns the convolution
    //       at each point of overlap, with an output shape of (N+M-1,). At
    //       the end-points of the convolution, the signals do not overlap
    //       completely, and boundary effects may be seen.
    //     'same':
    //       Mode 'same' returns output of length ``max(M, N)``.  Boundary
    //       effects are still visible.
    //     'valid':
    //       Mode 'valid' returns output of length
    //       ``max(M, N) - min(M, N) + 1``.  The convolution product is only given
    //       for points where the signals overlap completely.  Values outside
    //       the signal boundary have no effect.
    // Returns
    // -------
    // out : ndarray
    //     Discrete, linear convolution of `a` and `v`.
    // See Also
    // --------
    // scipy.signal.fftconvolve : Convolve two arrays using the Fast Fourier
    //                           Transform.
    // scipy.linalg.toeplitz : Used to construct the convolution operator.
    // polymul : Polynomial multiplication. Same output as convolve, but also
    //           accepts poly1d objects as input.
    // Notes
    // -----
    // The discrete convolution operation is defined as
    // .. math:: (a * v)[n] = \\sum_{m = -\\infty}^{\\infty} a[m] v[n - m]
    // It can be shown that a convolution :math:`x(t) * y(t)` in time/space
    // is equivalent to the multiplication :math:`X(f) Y(f)` in the Fourier
    // domain, after appropriate padding (padding is necessary to prevent
    // circular convolution).  Since multiplication is more efficient (faster)
    // than convolution, the function `scipy.signal.fftconvolve` exploits the
    // FFT to calculate the convolution of large data-sets.
    // References
    // ----------
    // .. [1] Wikipedia, "Convolution",
    //     https://en.wikipedia.org/wiki/Convolution
    // Examples
    // --------
    // Note how the convolution operator flips the second array
    // before "sliding" the two across one another:
    // >>> np.convolve([1, 2, 3], [0, 1, 0.5])
    // array([0. , 1. , 2.5, 4. , 1.5])
    // Only return the middle values of the convolution.
    // Contains boundary effects, where zeros are taken
    // into account:
    // >>> np.convolve([1,2,3],[0,1,0.5], 'same')
    // array([1. ,  2.5,  4. ])
    // The two arrays are of the same length, so there
    // is only one position where they completely overlap:
    // >>> np.convolve([1,2,3],[0,1,0.5], 'valid')
    // array([2.5])
    // """
//}

Overview

Its exported function, seasonal_mean, calculates the mean value for each period within a specified seasonal frequency cycle. It includes parameters to manage null or missing array elements by either excluding them or substituting zero or overall array averages. Designed as a general-purpose mathematical library for Pine Script developers, it contains calculation logic and does not plot visuals or generate direct trading signals.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Library, Pine Script v5

Draws

Library (no plots)

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

KernelFunctions

by artificial-intelligence-edge
75 stars

KernelFunctions is a Pine Script v5 library that exports kernel weight functions for Nadaraya-Watson style estimation. It calculates a Rational Quadratic kernel, a Gaussian kernel based on a radial basis function, and a periodic kernel that models repeating be

Library v5
Updated 2 days ago · MIT GitHub repository of KernelFunctions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

Zignaly2Beta

by ruckard
13 stars

This library provides helper functions to format and build JSON webhook alert payloads for the Zignaly automated trading platform. It constructs formatted messages containing parameters such as trading pair, exchange, account type, leverage, margin mode, hedge

Library v5
Updated 1 year ago · None stated GitHub repository of Zignaly2Beta

More libraries

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers