# Repainting and vanishing signals

The below code produces repainting/vanishing signals. the screen shot below explains my point clearly

Please suggest/help to make them stable.

``````_SECTION_BEGIN(" CURVED  TREND CHANNEL");
sx=Null;b=Null;ai=Null;x=Null;
reg=x1=x2=x3=z1=z2=z3=Null;
rega=x1a=x2a=x3a=z1a=z2a=z3a=Null;
bi=BarIndex();
eb=LastValue(bi);
order=Param("n-th Order",3,1,8,1);
bars=Param("Lookback Period",15,0,0,0);
ecart=1.61803399;
eb=SelectedValue(bi);
bb=Max(0,eb-bars);
fin=eb;
nn=order+1;
sx[1]=bars+1;

if(fin>bars)
{
for(mi=1;mi<=2*nn-2;mi++)
{
suml=0;
for(n=0;n<=bars;n++)
{
suml=suml+n^mi;
}
sx[mi+1]=suml;
}
for(mi=1;mi<=nn;mi++)
{
suml=0;
for(n=0;n<=bars;n++)
{
if (mi==1)
suml=suml+Close[fin-n];
else
suml=suml+Close[fin-n]*n^(mi-1);
b[mi]=suml;
}
}
for(jj=1;jj<=nn;jj++)
{
for(ii=1;ii<=nn;ii++)
{
kk=ii+jj-1;
ai[(ii-1)*nn+jj]=sx[kk];
}
}
for(kk=1;kk<=nn-1;kk++)
{
ll=0;
mm=0;
for(ii=kk;ii<=nn;ii++)
{
if(abs(ai[(ii-1)*nn+kk])>mm)
{
mm=abs(ai[(ii-1)*nn+kk]);
ll=ii;
}
}
if(ll==0) break;
if(ll!=kk)
{
for(jj=1;jj<=nn;jj++)
{
tt=ai[(kk-1)*nn+jj];
ai[(kk-1)*nn+jj]=ai[(ll-1)*nn+jj];
ai[(ll-1)*nn+jj]=tt;
}
tt=b[kk];
b[kk]=b[ll];
b[ll]=tt;
}
for(ii=kk+1;ii<=nn;ii++)
{
qq=ai[(ii-1)*nn+kk]/ai[(kk-1)*nn+kk];
for(jj=1;jj<=nn;jj++)
{
if(jj==kk)
ai[(ii-1)*nn+jj]=0;
else
ai[(ii-1)*nn+jj]=ai[(ii-1)*nn+jj]-qq*ai[(kk-1)*nn+jj];
}
b[ii]=b[ii]-qq*b[kk];
}
}
x[nn]=b[nn]/ai[nn*nn];
for(kk=1;kk<=nn-1;kk++)
{
tt=0;
ii=nn-kk;
for(jj=1;jj<=nn-ii;jj++)
{
tt=tt+ai[(ii-1)*nn+ii+jj]*x[ii+jj];
if(ai[(ii-1)*nn+ii]!=0)
x[ii]=(b[ii]-tt)/ai[(ii-1)*nn+ii];
}
}
for(n=0;n<=bars;n++)
{
suml=0;
for(kk=1;kk<=order;kk++)
{
suml=suml+x[kk+1]*n^kk;
}
reg[fin-n]=x[1]+suml;
}
}

dev=StDev(Close,bars);
sd=ecart*dev[fin];
x3=reg+sd;
z3=reg-sd;

LineUpx3 = x3 > Ref(x3,-1);//A slope higher than 45 DEGREE=0.785398 radians will turn green, less than -0.05 will turn red and anything in between will be white.
LineDnx3 = x3 < Ref(x3,-1);
LineUpz3 = z3 > Ref(z3,-1);
LineDnz3 = z3 < Ref(z3,-1);
Trendxz = IIf(LineUpx3 OR LineUpz3,colorLime,colorRed);

Plot(x3,"x3",Trendxz,styleThick);
Plot(z3,"z3",Trendxz,styleThick);
Plot( Close, "C", colorWhite, styleCandle);

TCUPTREND = IIf( x3 > Ref(x3,-1), 1, 0 ) OR IIf( x3a > Ref(x3a,-1), 1, 0 );
TCDOWNTREND = IIf( z3 < Ref(z3,-1), 1, 0 ) OR IIf( z3a < Ref(z3a,-1), 1, 0 );

SELL= TCDOWNTREND  ;

HaClose = ( O + H + L + C ) / 4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );

PlotShapes( IIf( BUY , shapeUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -22);
PlotShapes( IIf( SELL , shapeDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -22);

_SECTION_END();
``````

Where is the reference to the original AUTHOR of the formula?

Got it from internet , and I do not remember exactly the source.

Below is the source. Also I made certain changes fo the chage in colour.

// COG: Center of Gravity indicator
// AFL code by E.M.Pottasch, 2011
// translated from: http://chartstudio.whselfinvest.com/files/CenterGravity_0.ctl

that is in the nature of this method.

here I posted similar code: AFL to draw Trendline Between Previous day High and Previous day Low

it is intended for intraday but if you use EOD data then set the separator at week or higher

the light-blue lines do not repaint, see video here: https://sites.google.com/view/contentfortrading/tools/channels?authuser=0

1 Like

WOW!!! A REPLY FROM A LEGEND!!!

Thank you Sir.

Basically I an in search of a setting which can generate stable signals for intraday.

Could you suggest any, please?

So you can clearly see that making sure you give credit to original author may yieldâ€¦ the reply from the author of the formula himself. If you are using other peopleâ€™s formula please always give proper reference/credit in the future.

5 Likes

couldnâ€™t agree more!

Is there any way , if we mix this COG code with any other indicator like simple EMA crossovers and make THE SIGNALS STABLE?

I mean for example: when the down side EMA crossover happen if at that time if the COG curve is also in down trend then only the sell signal should generate and it should not change or vanish with the furure changing values of the COG curve.

can we please make it possible, Sir?

no because it is a least squares fit and when a new data point comes in it changes the entire function. You could rewrite the function so that it would return similar values like Amibroker LinearReg() function. But then it will not look that smooth and nice. Also you can use the extension of the fit. As long as the data used to calculate the fit stays the same the extension can be used to generate signals.

The thing is that in the beginning we saw this function and no-one knew how it was calculated and there were many that claimed it worked really well. That is when I got interested but it is just a polynomial fit of the data. It is more like something that looks nice but can not be used for trading system development, or like I said you 1) need to rewrite the function so that the output is similar as LinearReg() or 2) use the extension

on my site I have a polynomial fit plugin, see:

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 );
}
}
``````

I downloaded the SplineGroup plugin and tried to run the non-repainting COG code given by you. But the below error comes: please help.

I downloaded the SplineGroup plugin and tried to run the
non-repainting COG code given by you. But the

screenshot attached on forum.

i assumed you run the latest version of Amibroker which uses Visual Studio 2017 redistributables (from version 6.22). If you have a version before that you need to install them yourself:

I will later add the link to my site. If you use 32 bit version you need the 32 bit redistributable.

Microsoft Visual C++ Visual Studio 2017

X64

x86

you can find some instructions. Also posted the latest AFL code at the bottom of that page. I improved a few things and also show some signals of a simple system when you are in â€śNon-repaintâ€ť mode.

Very very happy to get genuine help from a LEGEND !!!

I have downloaded all and it is working fine for me.

The repainting and vanishing part I will check in live market and confirm here.

Thanks a TON again from the bottom of my heart.

that is good to hear.

1 question: On my google sites development computer I can see the plugins but all of a sudden on my laptop they disappeared. I have set them to visible and down-loadable for all but for some reason I can not see them.

So for a test I put a picture of the Milky Way (not sure if that is on the pic). Can you see the plugins displayed? I cant see them at this moment. Should be visible.

so do you see any files displayed inside the â€śMilky Wayâ€ť picture?