“Autocorrelation, also known as serial correlation, is the cross-correlation of a signal with itself. Informally, it is the similarity between observations as a function of the time lag between them” – Wikipedia. Autocorrelation value ranges from +1 to -1. Where autocorrelation above zero indicates a positive correlation and a negative value indicates a negative correlation.
Theoretically speaking, Positive Correlation means the time series is trending in the direction of its lagged time series and generally suits for trend trading. whereas the negative correlation mean the time series is mean reverting against the direction of lagged time series and generally suits for mean reversion trading. And if the auto-correlation value of zero represents random movements in stock price.
The code snippets for Amibroker and Tradingview have given below
Autocorrelation – Pinescript Code (Tradingview)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © marketcalls_in //@version=4 study("Autocorrelation - Marketcalls") len=input(10, minval=1,maxval = 50) Dayreturn=roc(close,1) autocorr = correlation(Dayreturn,Dayreturn,len) plot(autocorr, color = color.green , style = plot.style_line , linewidth = 2,title = "Autocorrelation") hline(0, title='hline1', color=color.red, linestyle=hline.style_dotted, linewidth=2)
You can also access the autocorrelation at tradingview public library
Autocorrelation – AFL Code (Amibroker)
_SECTION_BEGIN("Auto correlation"); Dayreturn=ROC(C,1); AC = Optimize("AC",10,1,50,1); AutoCor=Correlation(Dayreturn,Ref(Dayreturn,-1),AC); Plot(AutoCor,"AutoCorrelation",colorRed); Plot(0,"",colorGreen,style = styleDots); _SECTION_END();