Fast Fourier Transform (FFT) Filter

tbiktag Premium Uppdaterad   
Dear friends!
I'm happy to present an implementation of the Fast Fourier Transform ( FFT ) algorithm. The script uses the FFT procedure to decompose the input time series into its cyclical constituents, in other words, its frequency components, and convert it back to the time domain with modified frequency content, that is, to filter it.

Input Description and Usage

Source and Length:
Indicates where the data comes from and the size of the lookback window used to build the dataset.

Standardize Input Dataset:
If enabled, the dataset is preprocessed by subtracting its mean and normalizing the result by the standard deviation, which is sometimes useful when analyzing seasonalities. This procedure is not recommended when using the FFT filter for smoothing (see below), as it will not preserve the average of the dataset.

Show Frequency-Domain Power Spectrum:
When enabled, the results of Fourier analysis (for the last price bar!) are plotted as a frequency-domain power spectrum, where “power” is a measure of the significance of the component in the dataset. In the spectrum, lower frequencies (longer cycles) are on the right, higher frequencies are on the left. The graph does not display the 0th component, which contains only information about the mean value. Frequency components that are allowed to pass through the filter (see below) are highlighted in magenta.

Dominant Cycles, Rows:
If this option is activated, the periods and relative powers of several dominant cyclical components that is, those that have a higher power, are listed in the table. The number of the component in the power spectrum (N) is shown in the first column. The number of rows in the table is defined by the user.

Show Inverse Fourier Transform (Filtered):
When enabled, the reconstructed and filtered time-domain dataset (for the last price bar!) is displayed.

Apply FFT Filter in a Moving Window:
When enabled, the FFT filter with the same parameters is applied to each bar. The last data point of the reconstructed​ and filtered dataset is used to build a new time series. For example, by getting rid of high-frequency noise, the FFT filter can make the data smoother. By removing slowly evolving low-frequency components (including non-periodic constituents), one can reveal and analyze shorter cycles. Since filtering is done in real-time in a moving window (similar to the moving average), the modified data can potentially be used as part of a strategy and be subjected to other technical indicators.

Lowest Allowed N:
Indicates the number of the lowest frequency component used in the reconstructed time series.

Highest Allowed N:
Indicates the number of the highest frequency component used in the reconstructed time series.

Filtering Time Range block:
Specifies the time range over which real-time FFT filtering is applied. The reason for the presence of this block is that the FFT procedure is relatively computationally intensive. Therefore, the script execution may encounter the time limit imposed by TradingView when all historical bars are processed.

As always, I look forward to your feedback!
Also, leave a comment if you'd be interested in the tutorial on how to use this tool and/or in seeing the FFT filter in a strategy.
Release Notes: following issues improved:
- consistency in the definition of the lower and upper thresholds
- zeroth element removed from the table in order to avoid artefacts for non-standardized data
Release Notes:
  • Transferred to Pine Script v.5.
  • The FFT function is now imported from the FFTLibrary.
Problem fixed:
  • The graphs are now correctly redrawn with each new time step.

If you enjoy using my scripts, consider becoming a supporter: https://www.buymeacoffee.com/tbiktag
Skript med en öppen källkod

I sann TradingView-anda har författaren publicerat detta skript med öppen källkod så att andra handlare kan förstå och verifiera det. Hatten av för författaren! Du kan använda det gratis men återanvändning av den här koden i en publikation regleras av våra ordningsregler. Du kan ange den som favorit för att använda den i ett diagram.

Frånsägelse av ansvar

Informationen och publikationerna är inte avsedda att vara, och utgör inte heller finansiella, investerings-, handels- eller andra typer av råd eller rekommendationer som tillhandahålls eller stöds av TradingView. Läs mer i Användarvillkoren.

Vill du använda det här skriptet i ett diagram?