I have a formula for plotting a linear regression line on the price bars, but the formula only uses either the high or the low or the close for its calculations. I would like to calculate the line by using the HLC/3. Can anyone modify or suggest someone adept at code who can modify the following to do so?
Thanks in advance for your consideration,
David Smith
Existing code:
function preMain() {
setPriceStudy(true);
setStudyTitle("Linear Regression Line");
setCursorLabelName("Reg Value ", 0);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarThickness(1, 0);
}
function main(nLength, nSource)
{
if (nLength == null) {nLength = 10;}
if (nSource == null) {nSource = "Close";}
var yValues = new Array(nLength-1);
yValues = getValue(nSource, 0, -nLength);
if (yValues == null) {return;}
//slope
var xySum = 0;
var xSum = 0;
var ySum = 0;
var x2Sum = 0;
for (i = 0; i < nLength; ++i) {
xySum += (yValues[i] * i)
xSum += i;
ySum += yValues[i];
x2Sum += (i * i);
}
var b = (xySum - ((xSum * ySum) / nLength)) / (x2Sum - ((xSum * xSum) / nLength));
//intercept
var a = (ySum - (b * xSum)) / nLength;
return a;
}
Thanks in advance for your consideration,
David Smith
Existing code:
function preMain() {
setPriceStudy(true);
setStudyTitle("Linear Regression Line");
setCursorLabelName("Reg Value ", 0);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarThickness(1, 0);
}
function main(nLength, nSource)
{
if (nLength == null) {nLength = 10;}
if (nSource == null) {nSource = "Close";}
var yValues = new Array(nLength-1);
yValues = getValue(nSource, 0, -nLength);
if (yValues == null) {return;}
//slope
var xySum = 0;
var xSum = 0;
var ySum = 0;
var x2Sum = 0;
for (i = 0; i < nLength; ++i) {
xySum += (yValues[i] * i)
xSum += i;
ySum += yValues[i];
x2Sum += (i * i);
}
var b = (xySum - ((xSum * ySum) / nLength)) / (x2Sum - ((xSum * xSum) / nLength));
//intercept
var a = (ySum - (b * xSum)) / nLength;
return a;
}
Comment