Rsquare for amibroker

#1
_SECTION_BEGIN("Chart Settings");
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue));
SetChartBkGradientFill(ParamColor("Upper Chart",colorBlack),ParamColor("Lower Chart",colorBlack));
_SECTION_END();

Length=Param("Length",20,5,200,1);

function Trend(Price,Length) {

Result = 0;



for (k=Length;k<BarCount;k++) {

S = 0;

for (i=k-Length+1;i<k;i++) {
for (j=k-Length;j<i-1;j++) {
Signum = sign(Price-Price[j]);
S += Signum;
}
}

Variance =( Length*(Length-1.0)*(2*Length+5.0))/18.0;
StdDev = sqrt(Variance);

zScore = 0;
if (S >= 0) {
zScore=((S-1)/StdDev);
}else {
zScore=(S+1)/StdDev;
}

Result[k] = 0;

isTrend = False;
if ((zScore>=1.65)||(zScore<=-1.65)) {
isTrend=True;
}

if (isTrend) {
if (S<0){
Result[k] = -1;
}else {
Result[k] = 1;
}
}

}

return Result;
}


AA=Trend(C,Length);

uptrend=AA==1;
downtrend=AA==-1 ;
NOTR=AA==0;
Plot(AA,"",colorYellow,styleLine|styleThick);

Plot(1,"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed,IIf(NOTR, colorYellow, 0 ))),
styleOwnScale|styleArea|styleNoLabel, -1, 100 );



R2=Correlation(Cum( 1 ),C,Length)*Correlation(Cum( 1 ),C,Length);
slope=LinRegSlope(C,Length);

Crit=IIf(Length==5,.77,IIf(Length==10,.40,IIf(Length==14,.27,IIf(Length==20,.20,IIf(Length==25,.16,IIf(Length==30,.13,IIf(Length==50,.08,IIf(Length==60,.06,IIf(Length==120,.03,0)))))))));

Plot(r2,"R Squared",colorWhite,styleLine+styleDots|styleThick|styleOwnScale);
Plot(slope,"Slope",IIf(slope<0,4,5),2|styleOwnScale);
Plot(Crit,"",7,1|styleOwnScale);
 

Similar threads