# Regression Trend channels

##### Active Member
Hello seniors,

we have regression channels in amibroker but those are too wide or big ones.

The one shown in picture is Advanceget (Regression trend channels) which are too effective for trading.

I would be really thanxful if someone helps me to create afl for this.

Last edited:

#### ford7k

##### Banned

I am trying to begin something like step1.
Here is a simple afl for linear regression trend channels
Original afl code as per -courtesy-Geoff Mulhall 25-May-2002
Request senior experts to take it forward as I am novice.
regards & thanks
ford
HTML:
``````_SECTION_BEGIN("Linear Regression Line & Bands");
//SET PEAK TROUGH CHANGE %=2,EMA TEST=20,CLOSE ABOVE%=1//USE TO TEST ON 4 HR CHART
SetChartBkColor(64);
/* Linear Regression Slope */
/* Geoff Mulhall 25-May-2002 */
/* Refer www.equis.com/free/taaz/linearregression.html */
/* for a mathematical explanation of the formulae */

N = LastValue(LLVBars(Low,190));   // lookback period  - can be set by the user if necessary
//N = LastValue(LLVBars(Low,20));   // lookback period  - can be set by the user if necessary

Start = 1;

X = Cum(Start);    // Set up the x cordinate array of the Linear Regression Line
Y = Close;         // Set the y co-ordinate of the Linear Regression line

/* Calculate the slope (bconst) and the y intercept (aconst) of the line */

SUMX    = LastValue(Sum(X,N));
SUMY    = LastValue(Sum(Y,N));
SUMXY   = LastValue(Sum(X*Y,N));
SUMXSqd = LastValue(Sum(X*X,N));
SUMSqdX = LastValue(SUMX * SUMX);

bconst  = (N * SUMXY - SUMX * SUMY)/(N * SUMXSqd - SUMSqdX);
aconst  = (SUMY - bconst * (SUMX))/N;

/* Force the x value to be very negative so the graph does not apear before the lookback period */

Domain =  IIf ( X > LastValue(X) - N, 1 , -1e10);
Xvar = X * Domain;

/* Linear Regression Line */

Yvar = aconst + bconst * Xvar;

/* Plot the graphs */

GraphXSpace = 10;
MaxGraph=4;

/* Candle chart */
Graph0 = Close;
Graph0Style = 64;
Graph0Color = 1;

/* Linear Regression Lines */
Graph1 = Yvar;
Graph2 = Yvar - LastValue(HHV(Yvar - Low,N));
Graph3 = Yvar + LastValue(HHV(High - Yvar,N));

Graph1Style = 1;
Graph1Color = 2;
Graph2Style = 1;
Graph2Color = 2;
Graph3Style = 1;
Graph3Color = 2;
_SECTION_END();``````

#### ford7k

##### Banned
Hi friends
here is another afl
One thing to notice is

the reference adget chart has linreg lines starting from high and going to low.
I cant figure out how to get it right.
seniors help requested.
HTML:
``````//  Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below
//  Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support
//      Designed for use with AB 4.63 beta and above, using drag and drop feature.
//  Permits plotting a linear regression line of any price field available on the chart for a period determined by the user.
//     2 Channels, based on a standard deviation each determined by the user, are plotted above and below the linear regression line.
// 		A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.

Plot( C, "Close", colorBlue, styleBar | styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

P = ParamField("Price field",-1);
Daysback = Param("Period for Liner Regression Line",21,1,240,1);
shift = Param("Look back period",0.30,0,0,240,1);

//  =============================== Math Formula =============================================================

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );

// ==================Plot the Linear Regression Line ==========================================================

LRColor = ParamColor("LR Color", colorCycle );
LRStyle = ParamStyle("LR Style");

LRLine =  IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE ); //  styleDots );

// ==========================  Plot 1st SD Channel ===============================================================

//SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);//1.5 default
//SDP = Param("Standard Deviation" ,0.30, 0, 6, 0.1);//1.5 default
SDP = Param("Standard Deviation" ,0.30, 0, 6, 0.1);//1.5 default

SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle );
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle );

// ============================ End Indicator Code ==============================================================``````
chart here

Last edited:

##### Active Member
Hi friends
here is another afl
One thing to notice is

the reference adget chart has linreg lines starting from high and going to low.
I cant figure out how to get it right.
seniors help requested.
HTML:
``````//  Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below
//  Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support
//      Designed for use with AB 4.63 beta and above, using drag and drop feature.
//  Permits plotting a linear regression line of any price field available on the chart for a period determined by the user.
//     2 Channels, based on a standard deviation each determined by the user, are plotted above and below the linear regression line.
// 		A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.

Plot( C, "Close", colorBlue, styleBar | styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

P = ParamField("Price field",-1);
Daysback = Param("Period for Liner Regression Line",21,1,240,1);
shift = Param("Look back period",0.30,0,0,240,1);

//  =============================== Math Formula =============================================================

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );

// ==================Plot the Linear Regression Line ==========================================================

LRColor = ParamColor("LR Color", colorCycle );
LRStyle = ParamStyle("LR Style");

LRLine =  IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE ); //  styleDots );

// ==========================  Plot 1st SD Channel ===============================================================

//SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);//1.5 default
//SDP = Param("Standard Deviation" ,0.30, 0, 6, 0.1);//1.5 default
SDP = Param("Standard Deviation" ,0.30, 0, 6, 0.1);//1.5 default

SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle );
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle );

// ============================ End Indicator Code ==============================================================``````
chart here

Hello Ford,

Thanx you for ur efforts.

2nd one is good but Adget Regression trend channel is all about High and low of the trend.

Adget regression trend works really good to be honest ..

Waiting for seniors like Johnny ji and Kelvinhand to help...

Thanx you

#### manojborle

##### Active Member
Seniors kindly help to draw regresssion trend channels.

Thanx you

##### Active Member
Hello seniors,

we have regression channels in amibroker but those are too wide or big ones.

The one shown in picture is Advanceget (Regression trend channels) which are too effective for trading.

I would be really thanxful if someone helps me to create afl for this.