Hello everybody,
I am going crazy trying to do what I thought would be something simple but whatever I do I end up with weird results.
I have some code below that is basically a linear regression channel. I want to incorporate it into my trading system as a trend indicator.
I simply want the linear regression line to
1. turn red when it slopes down by say a 20% from start to finish
2. turn green when it slopes up by say 20% from start to finish
3. turn white in between
It's driving me crazy why I can't do this so I am hoping someone here can help me out. Thank You
// 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.
P = ParamField("Price field",-1);
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 );
// ========================== 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) ); // 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 ); //Inside Regression Lines
Plot( SDL , "Lower Lin Reg", SDColor,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) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
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 , "Upper Lin Reg", SDColor2,SDStyle2 ); //OutSide Regression Lines
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 ); //OutSide Regression Lines
//LRLineDown = IIf(LRLine < Ref(SDU2,-50),colorRed,IIf(LRLine > Ref(SDU2,-50),colorGreen,colorWhite));
Plot( LRLine , "LinReg", LRColor, LRSTYLE ); // styleDots );
// ============================ End Indicator Code ==============================================================
I am going crazy trying to do what I thought would be something simple but whatever I do I end up with weird results.
I have some code below that is basically a linear regression channel. I want to incorporate it into my trading system as a trend indicator.
I simply want the linear regression line to
1. turn red when it slopes down by say a 20% from start to finish
2. turn green when it slopes up by say 20% from start to finish
3. turn white in between
It's driving me crazy why I can't do this so I am hoping someone here can help me out. Thank You
// 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.
P = ParamField("Price field",-1);
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 );
// ========================== 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) ); // 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 ); //Inside Regression Lines
Plot( SDL , "Lower Lin Reg", SDColor,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) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
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 , "Upper Lin Reg", SDColor2,SDStyle2 ); //OutSide Regression Lines
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 ); //OutSide Regression Lines
//LRLineDown = IIf(LRLine < Ref(SDU2,-50),colorRed,IIf(LRLine > Ref(SDU2,-50),colorGreen,colorWhite));
Plot( LRLine , "LinReg", LRColor, LRSTYLE ); // styleDots );
// ============================ End Indicator Code ==============================================================