tbiktag

Fast Fourier Transform (FFT) Filter

tbiktag 已更新   
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.
​​
版本注释:
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
版本注释:
  • 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.

DISCLAIMER: I am not a financial advisor, and my scripts are for educational purposes only. Any trades you make are at your own risk.
开源脚本

本着真正的TradingView精神,该脚本的作者将其开源发布,以便交易者可以理解和验证它。为作者喝彩!您可以免费使用它,但在出版物中重复使用此代码受网站规则的约束。 您可以收藏它以在图表上使用。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想在图表上使用此脚本?