PINE LIBRARY

CGMA

30
Library "CGMA"
This library provides a function to calculate a moving average based on Chebyshev-Gauss Quadrature. This method samples price data more intensely from the beginning and end of the lookback window, giving it a unique character that responds quickly to recent changes while also having a long "memory" of the trend's start. Inspired by reading https://rohangautam.github.io/blog/chebyshev_gauss/

What is Chebyshev-Gauss Quadrature?
It's a numerical method to approximate the integral of a function f(x) that is weighted by
Pine Script®
1/sqrt(1-x^2)
over the interval [-1, 1]. The approximation is a simple sum:
Pine Script®
∫ f(x)/sqrt(1-x^2) dx ≈ (π/n) * Σ f(xᵢ)
where xᵢ are special points called Chebyshev nodes.

How is this applied to a Moving Average?
A moving average can be seen as the "mean value" of the price over a lookback window. The mean value of a function with the Chebyshev weight is calculated as:

Pine Script®
Mean = [∫ f(x)*w(x) dx] / [∫ w(x) dx]


The math simplifies beautifully, resulting in the mean being the simple arithmetic average of the function evaluated at the Chebyshev nodes:

Pine Script®
Mean = (1/n) * Σ f(xᵢ)


What's unique about this MA?
The Chebyshev nodes xᵢ are not evenly spaced. They are clustered towards the ends of the interval [-1, 1]. We map this interval to our lookback period. This means the moving average samples prices more intensely from the beginning and the end of the lookback window, and less intensely from the middle. This gives it a unique character, responding quickly to recent changes while also having a long "memory" of the start of the trend.

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.