Hello,
I've tried to modify Jason's (or whomever produced it) version of R-Squared that he redistributed based on the recent S&C article.
1) I'm trying to apply an instantcolorline so that I can show red down and green up. It works slightly but doesn't change at the turning points correctly. Not sure what else needs to be done.
2) Also, not sure if I set up the getValue from the efsInternal correctly. Any help would be appreciated.
kz
modified lines 21, 22; added lines 70 and 79-87
I've tried to modify Jason's (or whomever produced it) version of R-Squared that he redistributed based on the recent S&C article.
1) I'm trying to apply an instantcolorline so that I can show red down and green up. It works slightly but doesn't change at the turning points correctly. Not sure what else needs to be done.
2) Also, not sure if I set up the getValue from the efsInternal correctly. Any help would be appreciated.
kz
modified lines 21, 22; added lines 70 and 79-87
PHP Code:
/*****************************************************************
Provided By : eSignal. (c) Copyright 2004
Study: R-Squared
Version: 1.0
11/5/2006
Formula Parameters: Defaults:
Periods 8
Thickness 2
Color red
Display Line
Upper Band 0.75
Lower Band 0.20
*****************************************************************/
function preMain() {
setStudyTitle("R-Squared_InstantColorline ");
setCursorLabelName("R-Squared", 0);
// setDefaultBarFgColor(Color.red, 0);
setPlotType(PLOTTYPE_INSTANTCOLORLINE, 0);//changed from LINE
setDefaultBarThickness(2, 0);
setShowTitleParameters(false);
var fp10 = new FunctionParameter("nLRlen", FunctionParameter.NUMBER);
fp10.setName("Periods");
fp10.setLowerLimit(1);
fp10.setDefault(8);
var fp20 = new FunctionParameter("nLRThickness", FunctionParameter.NUMBER);
fp20.setName("Thickness");
fp20.setLowerLimit(1);
fp20.setDefault(2);
var fp30 = new FunctionParameter("nLRColor", FunctionParameter.COLOR);
fp30.setName("Color");
fp30.setDefault(Color.red);
var fp40 = new FunctionParameter("sDisplay", FunctionParameter.STRING);
fp40.setName("Display");
fp40.addOption("Line");
fp40.addOption("Histogram");
fp40.setDefault("Line");
var fp50 = new FunctionParameter("nUpper", FunctionParameter.NUMBER);
fp50.setName("Upper Band");
fp50.setDefault(0.75);
var fp60 = new FunctionParameter("nLower", FunctionParameter.NUMBER);
fp60.setName("Lower Band");
fp60.setDefault(0.2);
}
var bInit = false;
var xClose = null;
var xLinReg = null;
function main(nLRlen, nLRThickness, nLRColor, sDisplay, nUpper, nLower) {
if (bInit == false) {
setDefaultBarThickness(nLRThickness, 0);
setDefaultBarFgColor(nLRColor, 0);
if (sDisplay == "Histogram") {
setPlotType(PLOTTYPE_HISTOGRAM, 0);
} else {
setPlotType(PLOTTYPE_LINE, 0);
}
addBand(nUpper, PS_SOLID, 1, Color.blue, "upperBand");
addBand(nLower, PS_SOLID, 1, Color.blue, "lowerBand");
xClose = close();
xLinReg = efsInternal("LinReg", nLRlen, xClose);
xR = getSeries(xLinReg,2);//added
bInit = true;
}
if (xLinReg.getValue(0) != null) {
var A = getSeries(xLinReg, 0); // Slope
var B = getSeries(xLinReg, 1); // y-intercept
var R = getSeries(xLinReg, 2); // R-Squared
}
///////////////////////added to show InstantColorline
if( xR.getValue(0) > xR.getValue(-1))
{
setBarFgColor(Color.green);
}
if( xR.getValue(0) < xR.getValue(-1))
{
setBarFgColor(Color.red);
}
return R;
}
function LinReg(nLRlen, x) {
if (x.getValue(-nLRlen) == null) return;
var xSum = 0;
var ySum = 0;
var sumXY = 0;
var sumX2 = 0;
var sumY2 = 0;
i = 0;
for (i = 0; i < nLRlen; ++i) {
var xVal = x.getValue(-i);
xSum += (i+1);
ySum += xVal;
sumXY += ((i+1) * xVal);
sumX2 += ((i+1) * (i+1));
sumY2 += (xVal * xVal);
}
var xAvg = xSum/nLRlen;
var yAvg = ySum/nLRlen;
var aSum1 = 0;
var aSum2 = 0;
i = 0;
for (i = 0; i < nLRlen; ++i) {
aSum1 += (i-xAvg) * (x.getValue(-i)-yAvg);
aSum2 += (i-xAvg)*(i-xAvg);
}
// y = Ax + B;
// A = SUM( (x-xAVG)*(y-yAVG) ) / SUM( (x-xAVG)^2 )
// A = slope
// B = yAVG - (A*xAVG);
// B = y-intercept
// R2 = r-squared or correlation coefficient
var A = (aSum1 / aSum2);
var B = yAvg - (A*xAvg);
var R2 = Math.pow( (nLRlen * sumXY - xSum * ySum) /
Math.sqrt( (nLRlen*sumX2- (xSum*xSum)) *
(nLRlen*sumY2 - (ySum*ySum)) ) , 2);
return new Array(A, B, R2);
}
Comment