OPEN-SOURCE SCRIPT
Cập nhật

[blackcat] L2 Ehlers Autocorrelation Periodogram

3 784
Level: 2

Background

John F. Ehlers introduced Autocorrelation Periodogram in his "Cycle Analytics for Traders" chapter 8 on 2013.


Function

Construction of the autocorrelation periodogram starts with the autocorrelation function using the minimum three bars of averaging. The cyclic information is extracted using a discrete Fourier transform (DFT) of the autocorrelation results. This approach has at least four distinct advantages over other spectral estimation techniques. These are:
1. Rapid response. The spectral estimates start to form within a half-cycle period of their initiation.
2. Relative cyclic power as a function of time is estimated. The autocorrelation at all cycle periods can be low if there are no cycles present, for example, during a trend. Previous works treated the maximum cycle amplitude at each time bar equally.
3. The autocorrelation is constrained to be between minus one and plus one regardless of the period of the measured cycle period. This obviates the need to compensate for Spectral Dilation of the cycle amplitude as a function of the cycle period.
4. The resolution of the cyclic measurement is inherently high and is independent of any windowing function of the price data.

The dominant cycle is extracted from the spectral estimate in the next block of code using a center-of-gravity (CG) algorithm. The CG algorithm measures the average center of two-dimensional objects. The algorithm computes the average period at which the powers are centered. That is the dominant cycle. The dominant cycle is a value that varies with time. The spectrum values vary between 0 and 1 after being normalized. These values are converted to colors. When the spectrum is greater than 0.5, the colors combine red and yellow, with yellow being the result when spectrum = 1 and red being the result when the spectrum = 0.5. When the spectrum is less than 0.5, the red saturation is decreased, with the result the color is black when spectrum = 0.

Key Signal

DominantCycle --> Dominant Cycle
Period --> Autocorrelation Periodogram Array


Pros and Cons

100% John F. Ehlers definition translation of original work, even variable names are the same. This help readers who would like to use pine to read his book. If you had read his works, then you will be quite familiar with my code style.


Remarks

The 49th script for Blackcat1402 John F. Ehlers Week publication.

Courtesy of RicardoSantos for RGB functions.


Readme

In real life, I am a prolific inventor. I have successfully applied for more than 60 international and regional patents in the past 12 years. But in the past two years or so, I have tried to transfer my creativity to the development of trading strategies. Tradingview is the ideal platform for me. I am selecting and contributing some of the hundreds of scripts to publish in Tradingview community. Welcome everyone to interact with me to discuss these interesting pine scripts.

The scripts posted are categorized into 5 levels according to my efforts or manhours put into these works.

Level 1 : interesting script snippets or distinctive improvement from classic indicators or strategy. Level 1 scripts can usually appear in more complex indicators as a function module or element.

Level 2 : composite indicator/strategy. By selecting or combining several independent or dependent functions or sub indicators in proper way, the composite script exhibits a resonance phenomenon which can filter out noise or fake trading signal to enhance trading confidence level.

Level 3 : comprehensive indicator/strategy. They are simple trading systems based on my strategies. They are commonly containing several or all of entry signal, close signal, stop loss, take profit, re-entry, risk management, and position sizing techniques. Even some interesting fundamental and mass psychological aspects are incorporated.

Level 4 : script snippets or functions that do not disclose source code. Interesting element that can reveal market laws and work as raw material for indicators and strategies. If you find Level 1~2 scripts are helpful, Level 4 is a private version that took me far more efforts to develop.

Level 5 : indicator/strategy that do not disclose source code. private version of Level 3 script with my accumulated script processing skills or a large number of custom functions. I had a private function library built in past two years. Level 5 scripts use many of them to achieve private trading strategy.
Phát hành các Ghi chú
█ OVERVIEW

A faithful Pine v6 port of John F. Ehlers' Autocorrelation Periodogram, rebuilt so the spectrum you see is the one the algorithm computes. It measures how strongly price repeats itself at every lag, then turns that repetition into a power spectrum and a dominant cycle in bars.

It is a cycle research tool, not a signal engine: it reports the length of the cycle the market is running, and how clean that cycle is.

█ CONCEPTS

• Trend is stripped by a two-pole highpass filter, then smoothed by the SuperSmoother, so the periodogram sees cycles, not drift.
• Pearson autocorrelation is measured for every lag from 0 to 48 bars.
• A discrete Fourier transform of the autocorrelation gives the power spectrum; the centre of gravity of the strong bands is the dominant cycle.
• Automatic gain control normalises power against a decaying maximum, so colours stay comparable across symbols.

█ HOW TO USE

1 — Read the spectrum: each line is one period between 10 and 48 bars. Green means that period carries most of the energy, dark red means it is quiet.
2 — Read the dominant cycle: the thick line is the centre of gravity of the active bands, the thin grey line its average. The fill between them is lime while cycles lengthen, orange while they shorten.
3 — Read the table: dominant cycle, spectral peak, peak power, band width, jitter, correlation at the dominant lag, AGC level, and a sparkline of the whole spectrum.
4 — Judge quality first: a narrow band with high correlation at the dominant lag means one clean cycle; a wide band means competing cycles and an unreliable figure.

Input Parameters
• Correlation Averaging Length: samples averaged per lag, 0 uses each lag's own length (default 3).
• Highpass Cutoff, SuperSmoother Length: pre-filter tuning, defaults 48 and 10 as published.
• Spectrum Smoothing Weight, AGC Decay Factor: spectral memory and gain-control speed (defaults 0.2, 0.995).
• Power Threshold for Centre of Gravity: which bands count as active (default 0.5).
• Spectral Peak Enhancement Power: contrast boost for flat spectra, 1 disables it.
• Display From / To, Line Width, Weak / Half / Dominant Power colours: scope and palette of the heat map.
• Cycle Average Length, Fill, Markers, Background Tint, Status Table, Position: presentation only.

█ FEATURES

• Full 10-48 bar spectrum as an interactive heat map with a selectable display window.
• Dominant cycle with average, regime fill, and jitter measurement.
• Status table with a sparkline of the entire spectrum.
• Five alert conditions: cycle lengthening, cycle shortening, spectral peak shift, broad band, strong cyclicality.
• No security calls and no lookahead: values are final on the closed bar.

█ SIGNALS

• Lime triangle up: the dominant cycle crossed above its average, cycles are lengthening.
• Orange triangle down: the dominant cycle crossed below its average, cycles are shortening.
• Aqua dot: the spectral peak jumped by two periods or more, the market changed cycle family.
• Dotted lines mark the analysed minimum, mid range and maximum period.

█ IMPLEMENTATION NOTES

Version 2 corrects three defects of the first port: the Pearson accumulators are reset for every lag, the transform angle constant is a true 360 degrees, and the spectrum smoothing squares the squared sum as in the published listing, which restores sharp peaks. Normalised power is limited to the analysed band, clamped to 1, and stale correlation slots are zeroed.

█ LIMITATIONS

• The analysis band is fixed at 10 to 48 bars; faster or slower cycles are invisible.
• Allow 150 bars or more of history before trusting the dominant cycle.
• A periodogram measures repetition, not direction: it never says up or down.
• The centre of gravity is a weighted average, so it turns a few bars after the market does.

█ NOTES

• Algorithm by John F. Ehlers, Autocorrelation Periodogram, Cycle Analytics for Traders, chapter 8.
• V1 stays available for comparison; the visual layer is entirely new.

═════════════════════════════════════════════════════════════════════════
For questions or feedback, use the comments section below.
Feel free to use and modify — attribution appreciated.

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.