OPEN-SOURCE SCRIPT
Goertzel Algorithm [LB]

🔬 Concept
The Goertzel Algorithm, developed by Gerald Goertzel in 1958, is a digital signal processing technique that efficiently computes individual terms of the Discrete Fourier Transform (DFT). Unlike a full FFT which calculates all frequency bins, the Goertzel Algorithm targets a single predetermined frequency — making it the optimal tool for detecting the presence and power of a specific cycle period within a price series.
📐 Mathematical Foundation
The algorithm implements a second‑order recursive filter with a resonance at the target frequency. For a target period T, the normalized angular frequency is :
ω = 2π / T
The recurrence relation is applied to each sample x[n] in the window :
Q[n] = x[n] + 2·cos(ω)·Q[n-1] - Q[n-2]
After processing N samples, the complex DFT coefficient is extracted without storing intermediate values :
Re = Q[N-1] - Q[N-2]·cos(ω)
Im = Q[N-2]·sin(ω)
The output is the squared magnitude (power) of that frequency component :
Power = Re² + Im²
This approach requires only one multiply and two additions per sample, making it substantially lighter than a full FFT when monitoring a single cycle.
🎯 What Problem Does It Solve ?
Classic oscillators and moving averages operate blindly across all frequencies, mixing signal and noise. FFT‑based indicators attempt spectral analysis but compute hundreds of unnecessary frequency bins, wasting computational resources and introducing lag. The Goertzel Algorithm isolates the exact cycle period the trader wants to monitor, delivering pure frequency‑domain intelligence with minimal overhead.
📊 How To Interpret
Power rising and sustained at high levels → the target cycle period is strongly present in the price action ; the market is respecting the chosen rhythm.
Power declining toward zero → the target cycle has faded ; the market is no longer oscillating at that frequency.
Sharp power spike → the cycle has suddenly emerged ; potential entry signal when a known period (e.g., 20‑bar) becomes active.
Compare multiple instances with different periods → add the indicator twice with different target periods (e.g., 10 and 20 bars) to see which cycle dominates.
⚙️ Parameters
Target Period (bars) – the exact cycle length to detect ; typical values are 10, 20, 50, or any dominant cycle observed on the chart.
Analysis Window Length – number of bars over which the algorithm computes the power ; longer windows give more frequency resolution but slower response.
Source – price data used as input (close, HLC3, etc.).
📚 Reference
Goertzel G., "An Algorithm for the Evaluation of Finite Trigonometric Series", The American Mathematical Monthly, Vol. 65, No. 1, pp. 34‑35, January 1958.
Proakis J.G. & Manolakis D.G., "Digital Signal Processing : Principles, Algorithms, and Applications", Chapter 6 – Efficient Computation of the DFT, Prentice Hall, 1996.
The Goertzel Algorithm, developed by Gerald Goertzel in 1958, is a digital signal processing technique that efficiently computes individual terms of the Discrete Fourier Transform (DFT). Unlike a full FFT which calculates all frequency bins, the Goertzel Algorithm targets a single predetermined frequency — making it the optimal tool for detecting the presence and power of a specific cycle period within a price series.
📐 Mathematical Foundation
The algorithm implements a second‑order recursive filter with a resonance at the target frequency. For a target period T, the normalized angular frequency is :
ω = 2π / T
The recurrence relation is applied to each sample x[n] in the window :
Q[n] = x[n] + 2·cos(ω)·Q[n-1] - Q[n-2]
After processing N samples, the complex DFT coefficient is extracted without storing intermediate values :
Re = Q[N-1] - Q[N-2]·cos(ω)
Im = Q[N-2]·sin(ω)
The output is the squared magnitude (power) of that frequency component :
Power = Re² + Im²
This approach requires only one multiply and two additions per sample, making it substantially lighter than a full FFT when monitoring a single cycle.
🎯 What Problem Does It Solve ?
Classic oscillators and moving averages operate blindly across all frequencies, mixing signal and noise. FFT‑based indicators attempt spectral analysis but compute hundreds of unnecessary frequency bins, wasting computational resources and introducing lag. The Goertzel Algorithm isolates the exact cycle period the trader wants to monitor, delivering pure frequency‑domain intelligence with minimal overhead.
📊 How To Interpret
Power rising and sustained at high levels → the target cycle period is strongly present in the price action ; the market is respecting the chosen rhythm.
Power declining toward zero → the target cycle has faded ; the market is no longer oscillating at that frequency.
Sharp power spike → the cycle has suddenly emerged ; potential entry signal when a known period (e.g., 20‑bar) becomes active.
Compare multiple instances with different periods → add the indicator twice with different target periods (e.g., 10 and 20 bars) to see which cycle dominates.
⚙️ Parameters
Target Period (bars) – the exact cycle length to detect ; typical values are 10, 20, 50, or any dominant cycle observed on the chart.
Analysis Window Length – number of bars over which the algorithm computes the power ; longer windows give more frequency resolution but slower response.
Source – price data used as input (close, HLC3, etc.).
📚 Reference
Goertzel G., "An Algorithm for the Evaluation of Finite Trigonometric Series", The American Mathematical Monthly, Vol. 65, No. 1, pp. 34‑35, January 1958.
Proakis J.G. & Manolakis D.G., "Digital Signal Processing : Principles, Algorithms, and Applications", Chapter 6 – Efficient Computation of the DFT, Prentice Hall, 1996.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。