OPEN-SOURCE SCRIPT

TASC 2025.02 Autocorrelation Indicator

█ OVERVIEW

This script implements the Autocorrelation Indicator introduced by John Ehlers in the "Drunkard's Walk: Theory And Measurement By Autocorrelation" article from the February 2025 edition of TASC's Traders' Tips. The indicator calculates the autocorrelation of a price series across several lags to construct a periodogram, which traders can use to identify market cycles, trends, and potential reversal patterns.


█ CONCEPTS

Drunkard's walk

A drunkard's walk, formally known as a random walk, is a type of stochastic process that models the evolution of a system or variable through successive random steps.

In his article, John Ehlers relates this model to market data. He discusses two first- and second-order partial differential equations, modified for discrete (non-continuous) data, that can represent solutions to the discrete random walk problem: the diffusion equation and the wave equation. According to Ehlers, market data takes on a mixture of two "modes" described by these equations. He theorizes that when "diffusion mode" is dominant, trading success is almost a matter of luck, and when "wave mode" is dominant, indicators may have improved performance.

Pink spectrum

John Ehlers explains that many recent academic studies affirm that market data has a pink spectrum, meaning the power spectral density of the data is proportional to the wavelengths it contains, like pink noise. A random walk with a pink spectrum suggests that the states of the random variable are correlated and not independent. In other words, the random variable exhibits long-range dependence with respect to previous states.

Autocorrelation function (ACF)

Autocorrelation measures the correlation of a time series with a delayed copy, or lag, of itself. The autocorrelation function (ACF) is a method that evaluates autocorrelation across a range of lags, which can help to identify patterns, trends, and cycles in stochastic market data. Analysts often use ACF to detect and characterize long-range dependence in a time series.

The Autocorrelation Indicator evaluates the ACF of market prices over a fixed range of lags, expressing the results as a color-coded heatmap representing a dynamic periodogram. Ehlers suggests the information from the periodogram can help traders identify different market behaviors, including:
  • Cycles: Distinguishable as repeated patterns in the periodogram.
  • Reversals: Indicated by sharp vertical changes in the periodogram when the indicator uses a short data length.
  • Trends: Indicated by increasing correlation across lags, starting with the shortest, over time.



█ USAGE

This script calculates the Autocorrelation Indicator on an input "Source" series, smoothed by Ehlers' UltimateSmoother filter, and plots several color-coded lines to represent the periodogram's information. Each line corresponds to an analyzed lag, with the shortest lag's line at the bottom of the pane. Green hues in the line indicate a positive correlation for the lag, red hues indicate a negative correlation (anticorrelation), and orange or yellow hues mean the correlation is near zero.

Because Pine has a limit on the number of plots for a single indicator, this script divides the periodogram display into three distinct ranges that cover different lags. To see the full periodogram, add three instances of this script to the chart and set the "Lag range" input for each to a different value, as demonstrated in the chart above.

With a modest autocorrelation length, such as 20 on a "1D" chart, traders can identify seasonal patterns in the price series, which can help to pinpoint cycles and moderate trends. For instance, on the daily ES1! chart above, the indicator shows repetitive, similar patterns through fall 2023 and winter 2023-2024. The green "triangular" shape rising from the zero lag baseline over different time ranges corresponds to seasonal trends in the data.

To identify turning points in the price series, Ehlers recommends using a short autocorrelation length, such as 2. With this length, users can observe sharp, sudden shifts along the vertical axis, which suggest potential turning points from upward to downward or vice versa.
correlationCyclesehlerstasc

Script open-source

Dans le plus pur esprit TradingView, l'auteur de ce script l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par nos Règles. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.

Vous voulez utiliser ce script sur un graphique ?


Tools and ideas for all Pine coders: tradingview.com/u/PineCoders/
Pine news broadcasts: t.me/PineCodersSquawkBox or twitter.com/PineCoders
TASC: traders.com/
Aussi sur:

Clause de non-responsabilité