Indicator · Pine Script v5 · Own pane

Fast Autocorrelation Estimator

by Teerth1· code credited to Motgench

A statistics indicator in a separate pane that estimates the autocorrelation of a chosen series using a fast rolling update rather than recomputing the whole window.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code167
Settings7inputs
LicenceNone statedfile: MPL-2.0
Last update6 months ago10 Apr 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

167 lines of Pine Script v5 from deal-api/src/main/resources/PineScript/Fast Autocorrelation Estimator.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Fast Autocorrelation Estimator.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Motgench

//@version=5
indicator("Fast Autocorrelation Estimator" ,precision = 10)

ac = "ACF"
pac = "PACF"

lr = "Log Returns"
abs = "Absolute Returns"
clo = "Orignial Source"

hist = "Autocorrelarrogram"
lg = "Historical Lagged Autocorrelation"


src = input.source(close, "Source", group = "Source Settings", inline = "Source")
ss = input.string(lr, "Selection", options = [lr,clo, abs], group = "Source Settings",inline = "Source")
len = input.int(1000, "Lookback", group = "Source Settings")
lag = input.int(5, "Lag", group = "Model Settings")
maxl = input.int(30, title = "Max Lag", group = "Model Settings", options = [5,10,15,20,25,30])
a_sel = input.string(ac, "Autocorrelation Function", group = "Model Settings", options = [ac,pac])
p_sel = input.string(hist, "Plot Selection", group = "Model Settings", options = [hist,lg])

rx = math.log(src/src[1])

if lag > maxl
    runtime.error("current lag greater than max lag")

r = ss == "Log Returns" ? rx : ss == "Original Source" ? src : math.abs(rx)
var returns = array.new_float(len+1)
array.unshift(returns,r)
array.pop(returns)

facf(lag)=>
    x = r, n = len
    nh = n - lag
    var x1=0.0,var xxlag = 0.0, var xsum = 0.0, var xlagsum = 0.0, var x2 = 0.0
    
    xn = bar_index > n ? array.get(returns,n) : 0
    xlag = bar_index > lag ? array.get(returns,lag) : 0
    xnh = bar_index > nh ? array.get(returns,nh) : 0
    
    x1 := nz(x1) + x - xn
    xxlag := nz(xxlag) + x*xlag - xn*xnh
    xsum := nz(xsum) + x - xnh
    xlagsum := nz(xlagsum) + xlag - xn
    x2 := nz(x2) + x*x - xn*xn
    
    //mean
    m1 = x1/n
    //variance
    m2 = (x2 - m1*m1*n)/(n)
    //m2 = x2/n - m1*m1 (population variance)
    
    //Mean power
    mp2 = m1*m1
    
    //ACF
    (xxlag - m1*xlagsum - m1*xsum + nh*mp2) / (n*m2)

var maxL = maxl 

acf1 = facf(1)
acf2 = facf(2)
pk = array.new_float(maxl+1)
array.set(pk,0,1.0)
array.set(pk,1,acf1)
array.set(pk,2,facf(2))
array.set(pk,3,facf(3))
array.set(pk,4,facf(4))
array.set(pk,5,facf(5))

if maxl > 5
    array.set(pk,6,facf(6))
    array.set(pk,7,facf(7))
    array.set(pk,8,facf(8))
    array.set(pk,9,facf(9))
    array.set(pk,10,facf(10))
if maxl > 10
    array.set(pk,11,facf(11))
    array.set(pk,12,facf(12))
    array.set(pk,13,facf(13))
    array.set(pk,14,facf(14))
    array.set(pk,15,facf(15))
if maxl > 15
    array.set(pk,16,facf(16))
    array.set(pk,17,facf(17))
    array.set(pk,18,facf(18))
    array.set(pk,19,facf(19))
    array.set(pk,20,facf(20))
if maxl > 20
    array.set(pk,21,facf(21))
    array.set(pk,22,facf(22))
    array.set(pk,23,facf(23))
    array.set(pk,24,facf(24))
    array.set(pk,25,facf(25))
if maxl > 25
    array.set(pk,26,facf(26))
    array.set(pk,27,facf(27))
    array.set(pk,28,facf(28))
    array.set(pk,29,facf(29))
    array.set(pk,30,facf(30))

//...

phikk = array.new_float(maxl+1)
phikj = array.new_float(maxl+1)
array.set(phikk,0,1), array.set(phikk,1,acf1)
array.set(phikj,0,1), array.set(phikj,1,acf1)
    
for k = 2 to maxl
    sum1 = 0.0
    sum2 = 0.0

    for j = 1 to k-1
        pk1j = array.get(phikj,j)
        sum1 += pk1j *array.get(pk,k-j)
        sum2 += pk1j *array.get(pk,j)
    
    pkk = (array.get(pk,k) - sum1) / (1 - sum2)
    
    cphikj = array.copy(phikj)
    for j = 1 to k-1
        pk1j = array.get(cphikj,j)
        pk1kj = array.get(cphikj,k-j)
        pkj = pk1j - pkk* pk1kj
        array.set(phikj,j,pkj)
    
    array.set(phikj,k,pkk)
    array.set(phikk,k,pkk)
    //array.set(phikk,k,math.abs(pkk))

//array.shift(phikk)
//maxid = array.lastindexof(phikk,array.max(phikk))

var cv1 = 2/math.sqrt(len)
var cv2 = -cv1

var aLines = array.new_line (maxl  )
var bLines = array.new_line (maxl  )
var lab = array.new_label(maxl  +1)
var linesAreNA = true


autocorrelation = a_sel == "ACF" ? pk : phikk
col = a_sel == "ACF" ? color.aqua : #ff2d5f

if barstate.islast and p_sel == hist
    if linesAreNA
        for i=1 to maxl
            barpk = bar_index - i + 1
            array.set(aLines, i-1, line.new(barpk , array.get(autocorrelation, maxl-i+1), barpk, 0, color = col, width=2))
        for j=5 to maxl by 5
            barL = bar_index - maxl + j
            array.set(lab, j, label.new(barL, cv2, text="\n" + str.tostring(j), color=#00000000, textcolor=color.white, style=label.style_label_up, size=size.small))
        linesAreNA := false

scol = array.get(phikk,lag) < cv1 and  array.get(phikk,lag) > cv2 ? color.gray: color.yellow

hline(cv1)
hline(cv2)



plot(p_sel == lg ? array.get(phikk,lag) : na, color=scol)

Overview

The series can be log returns, absolute returns or the original source, over a lookback of 1000 bars by default. It can show either the autocorrelation function or the partial autocorrelation function, as a correlogram across lags up to a maximum or as the history of one chosen lag. It gives no trade signals.

Details

Published by

Teerth1 on GitHub

Code credit

Motgench, as written in the script's header

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosesrc
Selectionstringlrss
Lookbackint1000len
Lagint5lag
Max Lagint30maxl
Autocorrelation Functionstringaca_sel
Plot Selectionstringhistp_sel
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 11 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop
261 stars

This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa

Indicator v4 Oscillators & momentum
Updated 4 years ago · None stated GitHub repository of Example: Graph Style - 4th Dimension RSI

KD指標

by TaichiS
219 stars

This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period

Indicator v4 Oscillators & momentum
Updated 5 years ago · None stated GitHub repository of KD指標

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers