{
Indicator: Autocorrelation
This indicator plots the autocorrelation of the price differences of
the most recent NBars bars for a lag value of Lag.
The autocorrelation is the correlation coefficient of the price differences
(close to close) with the same series offset by the lag. For example, for
a lag of 1, the correlation coefficient is calculated between the following
two series: (1) the price differences starting from the current bar, and
(2) the price differences starting one bar back.
The autocorrelation is calculated as the covariance divided by the variance.
A value of the autocorrelation sufficiently different from 0 indicates
non-randomness in the price differences.
Michael R. Bryant
Breakout Futures
www.BreakoutFutures.com
Copyright 2010 Breakout Futures
November 2010
}
Input: NBars (30), { Look-back length }
Lag (1); { Offset }
Array: DeltaP[200](0), { Price differences on most recent NBars bars }
DeltaPLag[200](0); { Price differences offset by the lag }
Var: AutoCorr (0), { Value of the autocorrelation }
SigValue (0), { Value at which autocorrelation is significant }
ii (0); { loop counter }
{ Accumulate price differences }
for ii = 0 to NBars - 1 begin
DeltaP[ii] = C[ii] - C[ii + 1];
DeltaPLag[ii] = C[ii + Lag] - C[ii + Lag + 1];
end;
{ Calculate autocorrelation and significance level }
Value1 = CovarArray(DeltaP, DeltaPLag, NBars);
Value2 = VarianceArray(DeltaP, NBars, 1);
AutoCorr = 0;
If Value2 > 0 then
AutoCorr = Value1/Value2;
SigValue = 2./SquareRoot(NBars);
{ Plot autocorrelation and significance bands and color based on significance }
if AbsValue(AutoCorr) >= SigValue then
SetPlotColor(1, Green)
else
SetPlotColor(1, Blue);
SetPlotColor(2, Red);
SetPlotColor(3, Red);
Plot1(AutoCorr, "Autocorr");
Plot2(SigValue, "+Sig");
Plot3(-SigValue, "-Sig");
{-----------------------------------------------------------------------------------------------------}
{
Indicator: AutocorrMax
This indicator plots the largest autocorrelation of the price differences of
the most recent NBars bars for lags from 1 to MaxLag.
The autocorrelation is the correlation coefficient of the price differences
(close to close) with the same series offset by the lag. For example, for
a lag of 1, the correlation coefficient is calculated between the following
two series: (1) the price differences starting from the current bar, and
(2) the price differences starting one bar back.
The autocorrelation is calculated as the covariance divided by the variance.
A value of the autocorrelation sufficiently different from 0 indicates
non-randomness in the price differences.
Michael R. Bryant
Breakout Futures
www.BreakoutFutures.com
Copyright 2010 Breakout Futures
November 2010
}
Input: NBars (30), { Look-back length }
MaxLag (20); { Largest lag to look at }
Array: DeltaP[200](0), { Price differences on most recent NBars bars }
DeltaPLag[200](0); { Price differences offset by the lag }
Var: AutoCorr (0), { Value of the autocorrelation }
SigValue (0), { Value at which autocorrelation is significant }
MaxAuto (0), { Largest absolute value of autocorrelation for different lags }
AutoPlot (0), { Value of autocorrelation to plot }
iLag (0), { loop counter }
ii (0); { loop counter }
{ Calculate autocorrelation over lags 1 to 5 }
SigValue = 2./SquareRoot(NBars);
for ii = 0 to NBars - 1 begin
DeltaP[ii] = C[ii] - C[ii + 1]; { Accumulate price differences }
end;
Value2 = VarianceArray(DeltaP, NBars, 1);
MaxAuto = 0;
for iLag = 1 to MaxLag begin
{ Accumulate price differences }
for ii = 0 to NBars - 1 begin
DeltaPLag[ii] = C[ii + iLag] - C[ii + iLag + 1];
end;
{ Calculate autocorrelation and significance level }
Value1 = CovarArray(DeltaP, DeltaPLag, NBars);
AutoCorr = 0;
If Value2 > 0 then
AutoCorr = Value1/Value2;
If AbsValue(AutoCorr) > MaxAuto then begin
MaxAuto = AbsValue(AutoCorr);
AutoPlot = AutoCorr;
end;
end;
{ Plot autocorrelation and significance bands and color based on significance }
if MaxAuto >= SigValue then
SetPlotColor(1, Green)
else
SetPlotColor(1, Blue);
SetPlotColor(2, Red);
SetPlotColor(3, Red);
Plot1(AutoPlot, "AutoMax");
Plot2(SigValue, "+Sig");
Plot3(-SigValue, "-Sig");