Repainting and vanishing signals

yes this is because I only calculate the signals inside “maxl” bars. Else it will be real slow. But you can use higher orders for system development, I just only show “maxl” bars in my code.

But if you are happy with just the Amibroker LinearReg function it can be fast. So then you will only use “least squares order” = 1 functions. I will give example using just Amibroker function, … mañana

Ok I will do all the try for this new code and get back.

Just I want to tell you one idea for the old COG code as I am trying that for a long time (please excuse me , if I am bothering a lot with my little knowledge.)

In the old COG code , if once signal generated — we can store that in a variable and that value of the variable should stay independent of the future signals of the COG till the EMA cross-over trade is not complete. and then the variable should take the fresh COG signal value and the cycle may continue…

I mean , if we set the COG look back period lets say 200 then the signals will not change very frequently and till that changes again if we get any same direction EMA crossover we ca capitalize the trade and that will help to reduce the whipsaws(noises) of the EMA crossovers—there by more winning trades…

That is what i am thinking for quite a few days with my little brain and knowledge.

please let me know your comments…

yes I know what you are saying but this is exactly what the “non-repainting” version does. The non-repainting code calculates the “old COG” and only stores the last point of the channel. Then it calculates the next “old COG” at the next bar and again only stores the last point of the channel, etcetc.

So your idea is possible but would give the same result. And the way I did it enables you to calculate a backtest, although it is slow and the code needs to be adjusted a bit.

But first have a look at the code below. What it does is the same as the “non-repainting” code only it is just for the “first order”. So that is why I can use an Amibroker function for this.

I added simple system for futures as an example, but gives bad results

/*
© AFL code by E.M.Pottasch, 12/2017

non-repainting COG indicator

1-st order polynomial only
*/

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 3 );
SetOption( "FuturesMode", True );
NumContracts = 1;
PositionSize = NumContracts * MarginDeposit;
SetOption( "MaxOpenPositions", 1 );

maxl = Param( "Max Length", 100, 10, 3000, 1 );

prc = ( H + L ) / 2;
rr1 = LinearReg( prc, maxl );
err = StdErr( prc, maxl );

sdp1 = rr1 + err * 1;
sdm1 = rr1 - err * 1;
sdp2 = rr1 + err * 2;
sdm2 = rr1 - err * 2;
sdp3 = rr1 + err * 3;
sdm3 = rr1 - err * 3;

SetChartOptions( 0, chartShowDates );
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
Plot( Close, "Price", colorDefault, styleCandle, Null, Null, 0, 0, 1 );

Plot( rr1, "LinearReg", colorYellow, styleLine | styleNoRescale, Null, Null, 0, -1, 2 );
Plot( sdp1, "+1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( sdm1, "-1 Sigma", ColorRGB( 255, 0, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( sdp2, "+2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale | styleNoLabel , Null, Null, 0, 0, 1 );
Plot( sdm2, "-2 Sigma", ColorRGB( 255, 160, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( sdp3, "+3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( sdm3, "-3 Sigma", ColorRGB( 255, 160, 200 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );

Buy = L < sdm2 AND !IsEmpty( sdm3 );
BuyPrice = Min( O, sdm2 );
Sell = H > rr1 AND !IsEmpty( rr1 );
SellPrice = Max( O, rr1 );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Short = H > sdp2 AND !IsEmpty( sdp3 ) AND !Buy;
ShortPrice = Max( O, sdp2 );
Cover = L < rr1 AND !IsEmpty( rr1 );
CoverPrice = Min( O, rr1 );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorRed, 0, H, IIf( Short AND Sell, -30, -15 ) );
PlotShapes( IIf( Short, shapeSmallCircle, shapeNone ), colorWhite, 0, ShortPrice, 0 );
PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, IIf( Cover AND Buy, -30, -15 ) );
PlotShapes( IIf( Cover, shapeSmallCircle, shapeNone ), colorWhite, 0, CoverPrice, 0 );

before getting too excited about the systems results, prc should be:

prc = Ref( ( H + L ) / 2, -1 );

also system better written using:

Buy = L < sdm3;
BuyPrice = Min( O, sdm3 );
Sell = H > sdp1 AND !Buy;
SellPrice = Max( O, sdp1 );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Short = H > sdp3 AND !Buy;
ShortPrice = Max( O, sdp3 );
Cover = L < sdm1 AND !Short;
CoverPrice = Min( O, sdm1 );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

Whatever code I got from you til now , I have made little amendments to them to suit my need . I will post here tomorrow, please let me know if it is logically correct as I do not have that level of knowledge.

Thanks for your help and support.

Whatever code I got from you til now , I have made little amendments
to them to suit my need . I will post here tomorrow, please let me
know if it is logically correct as I do not have that level of
knowledge.

Thanks for your help and support.

The below screen shots are the results of the below pasted codes which I have amended to suit my need from all the codes I got from you on COG (both old and new).

My idea is to catch the trade when the same direction signals happen both from the repainting (plain lines) and non-repainting (doted lines) channels/lines.

image
image
image
image

SetChartBkGradientFill(colorBlack,colorBlack,colorBlack);
Plot( Close, "C", colorWhite, styleCandle); 
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );


_SECTION_BEGIN(" OLD COG---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);

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

_SECTION_END();


_SECTION_BEGIN("COLOR CHANGIG NON-REPAINTING COG");
/*AFL code by E.M.Pottasch, 12/2017---polynomial fit , non-repainting COG indicator */

SetBarsRequired( sbrAll, sbrAll );

order = Param( "Least Squares order",2, 1, 10, 1 );
maxl = Param( "Max Length",150, 10, 3000, 1 );

bi = BarIndex();
lvb = LastVisibleValue( bi );
sd = SelectedValue( bi );
start = bi == sd;
stidx = LastValue( ValueWhen( start, BarIndex() ) );
fvb = Max( 0, stidx - maxl );

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

// non repainting function similar to LinearReg()
function PolyReg()
{
    result = Null;

    for( ii = fvb + 1; ii <= stidx; ii++ )
    {
        xarr = yarr = Null;
        lsft = Null;
        cnt = 0;
        st = -int( maxl / 2 );

        for( i = ii - maxl + 1; i <= ii; i++ )
        {
            x[i] = st + cnt;
            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];

        
    }

    return result;
}
rr = PolyReg();
LineUprr = rr > Ref(rr,-1);
LineDnrr = rr < Ref(rr,-1);
Trendrr = IIf(LineUprr ,colorLime,colorRed);
Plot( rr, "PolyReg", Trendrr, styleDots | styleNoRescale, Null, Null, 0, -1, 2 );


_SECTION_END();

please guide with your comments.

ok yes that seems good but you did not implement it correctly. The last bar of the repainting and non-repainting should always be the same. You were using different settings for each one of them.

below the corrected code:

// E.M.Pottasch, 12/2017

//SetBarsRequired( sbrAll, 0 );
order = Param( "Least Squares order", 3, 1, 10, 1 );
maxl = Param( "Max Length", 100, 2, 3000, 1 );

SetChartBkGradientFill( colorBlack, colorBlack, colorBlack );
Plot( Close, "C", colorWhite, styleCandle );
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );

bi = BarIndex();
lvb = LastVisibleValue( bi );
sd = SelectedValue( bi );
//sd = BarCount - 1;
start = bi == sd;
stidx = LastValue( ValueWhen( start, BarIndex() ) );
fvb = Max( 0, stidx - maxl );

lsft0 = lsftExt = Null;
errp1 = errm1 = errp2 = errm2 = errp3 = errm3 = Null;
cnt = 0;
prc = Close;
x = bi - fvb - int( maxl / 2 );
xarr = yarr = Null;


_SECTION_BEGIN( " OLD COG---CURVED  TREND CHANNEL" );
// 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;

LineUperrp3 = errp3 > Ref( errp3, -1 );
LineDnerrp3 = errp3 < Ref( errp3, -1 );
LineUperrm3 = errm3 > Ref( errm3, -1 );
LineDnerrm3 = errm3 < Ref( errm3, -1 );
Trendxz = IIf( LineUperrp3 OR LineUperrm3, colorLime, colorRed );

Plot( lsft0, "LSF", colorYellow, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
Plot( errp1, "+1 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errm1, "-1 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errp2, "+2 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errm2, "-2 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errp3, "+3 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
Plot( errm3, "-3 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );

TCUPTREND = IIf( errp3 > Ref( errp3, -1 ), 1, 0 ) OR IIf( errp3 > Ref( errp3, -1 ), 1, 0 );
TCDOWNTREND = IIf( errm3 < Ref( errm3, -1 ), 1, 0 ) OR IIf( errm3 < Ref( errm3, -1 ), 1, 0 );
_SECTION_END();



_SECTION_BEGIN( "COLOR CHANGIG NON-REPAINTING COG" );
// non repainting function similar to LinearReg()
function PolyReg()
{
    result = Null;

    for( ii = fvb + 1; ii <= stidx; ii++ )
    {
        xarr = yarr = Null;
        lsft = Null;
        cnt = 0;
        st = -int( maxl / 2 );

        for( i = Max( 0, ii - maxl + 1 ); i <= ii; i++ )
        {
            x[i] = st + cnt;
            xarr[cnt] = x[i];
            yarr[cnt] = prc[i];
            cnt = cnt + 1;
        }

        aa = empPolyFit( xarr, yarr, cnt, order );

        for( i = Max( 0, 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];
    }

    return result;
}

rr = PolyReg();
LineUprr = rr > Ref( rr, -1 );
LineDnrr = rr < Ref( rr, -1 );
Trendrr = IIf( LineUprr , colorLime, colorRed );
Plot( rr, "PolyReg", Trendrr, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 3 );
_SECTION_END();

Thanks for correcting and guiding.

Just a doubt:

The non repainting COG code you gave for order 1 — it shows for all the available bars. Similarly can we show the non repainting COG for order <1 (I know you told that it will make Amibroker slow, in that case can we make it for last 5 days or so…) ?

yes you can expand that.

But indeed it is slow. Because when the order is 1 you have a special case and therefor it can be programmed more efficiently (I think). If I am wrong a math wiz may correct me.

but you can calculate the entire thing if you wish. I added a 3-rd parameter “nbars”. That will determine the number of non-repainting bars calculated

// E.M.Pottasch, 12/2017

order = Param( "Least Squares order", 3, 1, 10, 1 );
channelLength = Param( "Lenght Channel", 100, 2, 3000, 1 );
nbars = Param( "Number Bars Non-repainting", 500, 2, 3000, 1 );

SetBarsRequired( channelLength, 0 );

SetChartBkGradientFill( colorBlack, colorBlack, colorBlack );
Plot( Close, "C", colorWhite, styleCandle );
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );

bi = BarIndex();
lvb = LastVisibleValue( bi );
sd = SelectedValue( bi );
//sd = BarCount - 1;
start = bi == sd;
stidx = LastValue( ValueWhen( start, BarIndex() ) );
fvb = Max( 0, stidx - channelLength );

lsft0 = lsftExt = Null;
errp1 = errm1 = errp2 = errm2 = errp3 = errm3 = Null;
cnt = 0;
prc = Close;
x = bi - fvb - int( channelLength / 2 );
xarr = yarr = Null;


_SECTION_BEGIN( " OLD COG---CURVED  TREND CHANNEL" );
// 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 / ( channelLength - 2 ) ); // devide by ( channelLength - 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;

LineUperrp3 = errp3 > Ref( errp3, -1 );
LineDnerrp3 = errp3 < Ref( errp3, -1 );
LineUperrm3 = errm3 > Ref( errm3, -1 );
LineDnerrm3 = errm3 < Ref( errm3, -1 );
Trendxz = IIf( LineUperrp3 OR LineUperrm3, colorLime, colorRed );

Plot( lsft0, "LSF", colorYellow, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
Plot( errp1, "+1 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errm1, "-1 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errp2, "+2 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errm2, "-2 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
Plot( errp3, "+3 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
Plot( errm3, "-3 Sigma", Trendxz, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );

TCUPTREND = IIf( errp3 > Ref( errp3, -1 ), 1, 0 ) OR IIf( errp3 > Ref( errp3, -1 ), 1, 0 );
TCDOWNTREND = IIf( errm3 < Ref( errm3, -1 ), 1, 0 ) OR IIf( errm3 < Ref( errm3, -1 ), 1, 0 );
_SECTION_END();



_SECTION_BEGIN( "COLOR CHANGIG NON-REPAINTING COG" );
// non repainting function similar to LinearReg()

function PolyReg()
{
    result = Null;
    
    for( ii =  Max( 0, stidx - nbars ); ii <= stidx; ii++ )
    {
        xarr = yarr = Null;
        lsft = Null;
        cnt = 0;
        st = -int( channelLength / 2 );

        for( i = Max( 0, ii - channelLength + 1 ); i <= ii; i++ )
        {
            x[i] = st + cnt;
            xarr[cnt] = x[i];
            yarr[cnt] = prc[i];
            cnt = cnt + 1;
        }

        aa = empPolyFit( xarr, yarr, cnt, order );

        for( i = Max( 0, ii - channelLength + 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];
    }

    return result;
}

rr = PolyReg();
LineUprr = rr > Ref( rr, -1 );
LineDnrr = rr < Ref( rr, -1 );
Trendrr = IIf( LineUprr , colorLime, colorRed );
Plot( rr, "PolyReg", Trendrr, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 3 );
_SECTION_END();

I observe that in repainting mode the channel is perfect in curve shape, but in non repainting mode the curve of the channel is not perfect. is it correct or I am doing any mistake ?

i think I explained it all and posted all code necessary. Not going to start all over again

OK. I will go through all again and try to understand.

I have understood my above doubt.

I wrote some code based on linreg function in ami.

could you please guide me , if my logic and implementation is correct in below coding:

_SECTION_BEGIN("COLOR CHANGIG LINREG LINE---NON-VANISHING & NON-REPAINTING SIGNALS");
per = Optimize( "period",9, 1, 500, 1);
mm = C;
x = Cum(1);
lastx = LastValue(x); 
selv = SelectedValue(x);
aaa = LinRegIntercept(mm, per);
bbb = LinRegSlope(mm, per);
daa = SelectedValue(ValueWhen(x, aaa, 1));
dbb = SelectedValue(ValueWhen(x, bbb, 1));
xx = IIf(x > selv - per AND x <= selv, x - (selv - per),Null);
yy = daa + dbb * xx;
LRLine =  IIf( x > (lastx - selv) AND BarIndex() < Lastx, yy, Null );
Pi = 3.14159265 * atan(1); 
SlopeAngle = atan(bbb)*(180/Pi);
LineUp = SlopeAngle > 0.7;//A slope higher than 45 DEGREE=0.785398 radians will turn green, less than 45 DEGREE=0.785398 will turn red and anything in between will be white.
LineDn = SlopeAngle < -0.7;
Trend = IIf(LineUp,colorLime,colorRed);//LRLine > Ref(LRLine,-1)  LineUp///Changes LR line to green if sloping up and red if sloping down.
LineUprr = LineUp;
LineDnrr = LineDn;
TrendB=Flip(LineUprr,LineDnrr); 
TrendS=Flip(LineDnrr,LineUprr); 
Plot( LRLine , "", Trend, styleThick+styleDots );
_SECTION_END();

I do not understand what you exactly want. The last code I posted in this thread, did you look at that?

your code should be the same if you use the same channel length and the same order ( which should be 1).

so the code you posted is almost correct but should have:

xx = IIf( x > selv - per AND x <= selv, x - ( selv - per ) - 1, Null );

or the part x - ( selv - per ) - 1 is different

as I posted before the last point of the channel should coincide with linearReg() of my polyReg() function. I did not look at the slope and colors

_SECTION_BEGIN( "COLOR CHANGIG LINREG LINE---NON-VANISHING & NON-REPAINTING SIGNALS" );
per = Optimize( "period", 20, 1, 500, 1 );
mm = C;
x = Cum( 1 );
lastx = LastValue( x );
selv = SelectedValue( x );
aaa = LinRegIntercept( mm, per );
bbb = LinRegSlope( mm, per );
daa = SelectedValue( ValueWhen( x, aaa, 1 ) );
dbb = SelectedValue( ValueWhen( x, bbb, 1 ) );
xx = IIf( x > selv - per AND x <= selv, x - ( selv - per ) - 1, Null );
yy = daa + dbb * xx;
LRLine =  IIf( x > ( lastx - selv ) AND BarIndex() < Lastx, yy, Null );
Pi = 3.14159265 * atan( 1 );
SlopeAngle = atan( bbb ) * ( 180 / Pi );
LineUp = SlopeAngle > 0.7;//A slope higher than 45 DEGREE=0.785398 radians will turn green, less than 45 DEGREE=0.785398 will turn red and anything in between will be white.
LineDn = SlopeAngle < -0.7;
Trend = IIf( LineUp, colorLime, colorRed ); //LRLine > Ref(LRLine,-1)  LineUp///Changes LR line to green if sloping up and red if sloping down.
LineUprr = LineUp;
LineDnrr = LineDn;
TrendB = Flip( LineUprr, LineDnrr );
TrendS = Flip( LineDnrr, LineUprr );

SetChartOptions( 0, chartShowDates );
SetChartBkGradientFill( colorBlack, colorBlack, colorBlack );
Plot( Close, "C", colorWhite, styleCandle );
Plot( LRLine, "LSF", Trend, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
Plot( LinearReg( mm, per ) , "", coloryellow, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
_SECTION_END();

I wanted to explore the slope concept to deal with the whipsaws , but seems like it does not work.

Any advise please…

if you are working with the linear function you can just use the Amibroker function LinRegSlope().

Yes I want to expand it , but not for all visible bars.

is it possible to start the channel at the beginning of the day and let it progress till EOD. on a new day it should start afresh.

Just , thinking about it in order to keep away the gap up / downs away from intraday movements to make the live intra scalping trade decisions more effective.

May , be I am wrong…please guide me.

that code I already posted and have showed you. Maybe you should look at my replies first and then think about a new question to ask

https://sites.google.com/view/contentfortrading/tools/channels?authuser=0

Oh, Great!!!

Its already there in another thread. I will read and understand that first and then will get back.

Thanks.