Linear Regression dynamic length

Hi Guys,

I have some Linear regression code that I'm struggling to amend. I would like to change the length of the Channel from a fixed number of bars to the number of bars a moving average for example is sloping down.

Any help would be much appreciated.

MA50        = MA(C,50);
MA50ZIG     = Zig(MA50,0.1);
MA50colour  = IIf(MA50ZIG == Ref(MA50ZIG,-1),colorWhite,IIf(MA50ZIG > Ref(MA50ZIG,-1),colorBrightGreen,colorRed));
Plot(MA50ZIG,"50 MA",MA50colour,styleLine,0,0,0,0,5); 

//================================================Start Chart Configuration======================================================
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +
           " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
//SetChartBkColor(colorBlack);
 
Plot( C, "", colorBlack, styleCandle, Zorder = 1);
SetChartOptions(0,chartShowArrows | chartShowDates);
 
//====================================Start of Linear Regression Code============================================================
 
P = ParamField("Price field",-1);
MA50 = MA(C,50); 
Length = 150;

Daysback = Param("Period for Liner Regression Line",Length,1,240,1);
shift = Param("Look back period",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 );
 
LRStyle = ParamStyle("LR Style");
Angle = Param("Angle", 0.05, 0, 1.5, 0.01);// A slope higher than 0.05 radians will turn green, less than -0.05 will turn red
                                           // AND anything in between will be white.
 
LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
 
Pi = 3.14159265 * atan(1); // Pi
SlopeAngle = atan(bb)*(180/Pi);
 
LineUp = SlopeAngle > Angle;
LineDn = SlopeAngle < - Angle;
 
if(LineUp)
{
//Plot(LRLine, "Lin. Reg. Line Up", IIf(LineUp, colorLime, colorWhite), LRStyle);
}
else
{
//Plot(LRLine, "Lin. Reg. Line Down", IIf(LineDn, colorRed, colorWhite), LRStyle);
}
 
//==========================  Plot 1st SD Channel =============================================================================
 
SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
SD = SDP/2;
 
width = LastValue( Ref(SD*StDev(p, Daysback),-shift) ); //Set width of inside chanels here.
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 , "", colorBlue,SDStyle ); //Inside Regression Lines
Plot( SDL , "", colorBlue,SDStyle ); //Inside Regression Lines
 
//==========================  Plot 2d SD Channel ===============================================================================
 
SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;
 
width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) ); //Set width of outside chanels here.
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;
 
SDColor2 = ParamColor("2 SD Color", colorCycle );
SDStyle2 = ParamStyle("2 SD Style");
 
Plot( SDU2 , "", colorRed,SDStyle2 ); //OutSide Regression Lines
Plot( SDL2 , "", colorRed,SDStyle2 ); //OutSide Regression Lines
 
Trend = IIf(LRLine > Ref(LRLine,-1),colorLime,colorRed);//Changes LR line to green if sloping up and red if sloping down.
 
Plot( LRLine , "LinReg", Trend, styleDots );
 
//============================ End Indicator Code =======================================================================

```![Channel%20length|690x411](upload://jSYt3GHxLD3jHA6emBnVFQT248b.jpg)

@Kevin although I don't fully understand your goal, this post may be of help.

1 Like

@portfoliobuilder - it is not needed at all. LinearReg supports variable period out of the box:

Plot( LinearReg( C, BarIndex() ), "LR dynamic", colorRed ); 
2 Likes