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