on my site I have a polynomial fit plugin, see:
https://sites.google.com/view/contentfortrading/tools/polynomial-fit?authuser=0
if you use that then I have some code below to plot the non-repainting COG. If you use an “order” of 1 in the param window then also the Amibroker LinearReg is shown.
For higher orders I only calculate my PolyReg() for maxl bars because it is slow. I think this also might be the reason Amibroker only uses the linear fit because for higher order it becomes real slow, since for every bar you need to do calculate a fit with a length of “maxl”.
In the param window you can toggle between “Repainting” and “Non-Repainting”
You will need the plugin to run the code
/*
© AFL code by E.M.Pottasch, 12/2017
polynomial fit using:
https://github.com/natedomin/polyfit/blob/master/polyfit.c
non-repainting COG indicator
*/
SetBarsRequired( sbrAll, sbrAll );
order = Param( "Least Squares order", 1, 1, 10, 1 );
maxl = Param( "Max Length", 100, 10, 3000, 1 );
dd = ParamToggle( "Display", "Repainting|Non-Repainting", 0 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
dn = DateTime();
sd = SelectedValue( dn );
start = dn == sd;
stidx = LastValue( ValueWhen( start, BarIndex() ) );
fvb = Max( 0, stidx - maxl );
SetChartOptions( 0, chartShowDates );
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
Plot( Close, "Price", colorDefault, styleCandle, Null, Null, 0, 0, 1 );
lsft0 = lsftExt = Null;
errp1 = errm1 = errp2 = errm2 = errp3 = errm3 = Null;
sdp1 = sdm1 = sdp2 = sdm2 = sdp3 = sdm3 = Null;
cnt = 0;
prc = ( H + L ) / 2;
x = bi - fvb - int( maxl / 2 );
xarr = yarr = Null;
// fill the arrays for the empPolyFit function
for( i = fvb + 1; i <= stidx; i++ )
{
xarr[cnt] = x[i];
yarr[cnt] = prc[i];
cnt = cnt + 1;
}
// calculate coeficients
aa = empPolyFit( xarr, yarr, cnt, order );
// calculate fit
for( i = fvb + 1; i <= stidx; i++ )
{
lsft0[i] = aa[0];
for( j = 1; j <= order; j++ )
{
lsft0[i] += aa[j] * x[ i ] ^ j;
}
}
// calculate standard deviation
sdp = 0;
for( i = fvb + 1; i <= stidx; i++ )
{
sdp = sdp + ( prc[i] - lsft0[i] ) ^ 2;
}
sd = sqrt( sdp / ( maxl - 2 ) ); // devide by ( maxl - 2 ) corresponding to StdErr function
errp1 = lsft0 + sd * 1;
errm1 = lsft0 - sd * 1;
errp2 = lsft0 + sd * 2;
errm2 = lsft0 - sd * 2;
errp3 = lsft0 + sd * 3;
errm3 = lsft0 - sd * 3;
// calculate extension
for( i = stidx + 1; i <= lvb; i++ )
{
lsftExt[i] = aa[0];
for( j = 1; j <= order; j++ )
{
lsftExt[i] += aa[j] * x[ i ] ^ j;
}
}
// non repainting function similar to LinearReg()
function PolyReg()
{
result = Null;
for( ii = fvb + 1; ii <= stidx; ii++ )
{
xarr = yarr = Null;
lsft = Null;
cnt = 0;
for( i = ii - maxl + 1; i <= ii; i++ )
{
xarr[cnt] = x[i];
yarr[cnt] = prc[i];
cnt = cnt + 1;
}
aa = empPolyFit( xarr, yarr, cnt, order );
for( i = ii - maxl + 1; i <= ii; i++ )
{
lsft[i] = aa[0];
for( j = 1; j <= order; j++ )
{
lsft[i] += aa[j] * x[ i ] ^ j;
}
}
result[ii] = lsft[ii];
// calculate standard deviation
sdp = 0;
for( i = ii - maxl + 1; i <= ii; i++ )
{
sdp = sdp + ( prc[i] - lsft[i] ) ^ 2;
}
sd = sqrt( sdp / ( maxl - 2 ) );
sdp1[ii] = lsft[ii] + sd;
sdm1[ii] = lsft[ii] - sd;
sdp2[ii] = lsft[ii] + sd * 2;
sdm2[ii] = lsft[ii] - sd * 2;
sdp3[ii] = lsft[ii] + sd * 3;
sdm4[ii] = lsft[ii] - sd * 3;
}
return result;
}
Plot( lsft0, "LSF", colorYellow, styleLine | styleNoRescale, Null, Null, 0, 0, 2 );
Plot( lsftExt, "", ColorRGB( 0, 255, 255 ), styleLine | styleNoRescale, Null, Null, 0, 0, 2 );
Plot( bi == stidx, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 10 );
if( dd == 0 )
{
Plot( errp1, "+1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( errm1, "-1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( errp2, "+2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( errm2, "-2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( errp3, "+3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( errm3, "-3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}
if( dd == 1 )
{
rr = PolyReg();
rr1 = LinearReg( prc, maxl );
err = StdErr( prc, maxl );
Plot( rr, "PolyReg", colorYellow, styleLine | styleNoRescale, Null, Null, 0, -1, 10 );
if( order == 1 )
{
Plot( rr1 , "", ColorRGB( 0, 0, 255 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 3 );
Plot( rr1 + err * 1, "+1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( rr1 - err * 1, "-1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( rr1 + err * 2, "+2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale | styleNoLabel , Null, Null, 0, 0, 1 );
Plot( rr1 - err * 2, "-2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( rr1 + err * 3, "+3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( rr1 - err * 3, "-3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( sdp1, "+1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdm1, "-1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdp2, "+2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdm2, "-2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdp3, "+3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdm3, "-3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}
else
if( order > 1 )
{
Plot( sdp1, "+1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdm1, "-1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdp2, "+2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdm2, "-2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdp3, "+3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( sdm3, "-3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
}
}