Saturday, 20 June 2015

Linear Regression Channel AFL For Amibroker



Linear Regression Channel
Here is a screenshot of how the indicator looks:

SetChartBkColor(ParamColor("Background Color",colorBlack));
SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP
Color",colorGreen),IIf(C<=O,ParamColor("Candle Down
Color",colorRed),colorLightGrey)));

Plot(C,"Price",IIf(C>O,ParamColor("Wick UP
Color",colorDarkGreen),IIf(C<=O,ParamColor("Wick Down
Color",colorDarkRed),colorLightGrey)),64,0,0,0,0);

_SECTION_BEGIN("Linear Reg Channel");
//Plot(Close, "", 55, GetPriceStyle());
P        = ParamField("Price field", -1);
Daysback = Param("Period for Linear Regression Channel", 50, 1, 300, 1);
SDP1     = Param("Standard Deviation 1", 1.0, 0, 6, 0.05);
SDP2     = Param("Standard Deviation 2", 2.0, 0, 6, 0.05);
SDP3     = Param("Standard Deviation 3", 3.0, 0, 6, 0.05);
ext      = Param("extend Linear Regression Channel", 15, 0, 50, 1);
shift    = Param("Look back period", 0, 0, 240, 1);
Collg    = ParamColor("Color Linear Reg Line", colorRed);
Colsd1   = ParamColor("Color StDev 1", colorBlue);
Colsd2   = ParamColor("Color StDev 2", colorLime);
Colsd3   = ParamColor("Color StDev 3", colorGold);
SDP1     = SDP1/2;
SDP2     = SDP2/2;
SDP3     = SDP3/2;
pds      = Daysback;
x        = BarIndex() + 1;
sx       = SelectedValue(x)-shift;
aa       = SelectedValue(Ref(LinRegIntercept(P, pds), -shift));
bb       = SelectedValue(Ref(LinRegSlope(P, pds), -shift));
StDev0   = StDev(P, pds);
fd1      = SelectedValue(Ref(SDP1 * StDev0, -shift) );
fd2      = SelectedValue(Ref(SDP2 * StDev0, -shift) );
fd3      = SelectedValue(Ref(SDP3 * StDev0, -shift) );
ys       = SelectedValue(ValueWhen(x, aa, 1));
yi       = SelectedValue(ValueWhen(x, bb, 1));
xs       = sx - pds;
d        = ext;//Shift Line Right
xe       = xs + pds + d;
ye       = ys + yi * (xe - xs);
sty      = 1|32|2048;
Plot(LineArray(xs-d,ys,xe-d,ye,1),"",Collg,sty,0,0,d);
Plot(LineArray(xs-d,ys-fd1,xe-d,ye-fd1,1),"",Colsd1,sty,0,0,d);
Plot(LineArray(xs-d,ys+fd1,xe-d,ye+fd1,1),"",Colsd1,sty,0,0,d);
Plot(LineArray(xs-d,ys-fd2,xe-d,ye-fd2,1),"",Colsd2,sty,0,0,d);
Plot(LineArray(xs-d,ys+fd2,xe-d,ye+fd2,1),"",Colsd2,sty,0,0,d);
Plot(LineArray(xs-d,ys-fd3,xe-d,ye-fd3,1),"",Colsd3,sty,0,0,d);
Plot(LineArray(xs-d,ys+fd3,xe-d,ye+fd3,1),"",Colsd3,sty,0,0,d);
_SECTION_END();

No comments:

Post a Comment

Thanks