Announcement

Collapse
No announcement yet.

Linear Regression Line Calc

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Linear Regression Line Calc

    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;
    }

  • #2
    Linear Regression Line Calc

    One more note, I also have code to calculate a linear regression line from the open, high, low, close/4 and displaced three periods into the future. Perhaps it would be easier to modify that code to get a regression line that is calculated from the HLC/3 and not displaced? I have included that code below.

    Thanks again for any help you can give,

    David Smith

    ----

    var basis_Arr = new Array();
    var nCount = 0;

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Linear Regression Line");
    setCursorLabelName("Reg Value ", 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(1, 0);
    }

    function main(nLength, displace)

    {
    if (nLength == null) {nLength = 21;}
    if (displace == null) {displace = -3;}

    var nBarState = getBarState();
    if (nBarState == BARSTATE_NEWBAR) {nCount++;} // start of new bar

    var OValues = new Array(nLength-1);
    var HValues = new Array(nLength-1);
    var LValues = new Array(nLength-1);
    var CValues = new Array(nLength-1);
    var yValues = new Array(nLength-1);

    OValues = getValue("Open", 0, -nLength);
    HValues = getValue("High", 0, -nLength);
    LValues = getValue("Low", 0, -nLength);
    CValues = getValue("Close", 0, -nLength);

    if (OValues == null || HValues == null || LValues == null || CValues == null ) {return;}

    for (i = 0; i < nLength; ++i)
    {yValues[i] = ((OValues[i] + HValues[i] + LValues[i] + CValues[i])/4);}

    //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;

    basis_Arr[nCount+3] = a; // store the calculated 2nd 21 and display value from 3 minutes ago.

    return basis_Arr[nCount];
    }

    Comment


    • #3
      Try This (I am away from my home set-up at present but I think it should work and I did the one you reference below :

      function preMain() {
      setPriceStudy(true);
      setStudyTitle("Linear Regression Line");
      setCursorLabelName("Reg Value ", 0);
      setDefaultBarFgColor(Color.red, 0);
      setDefaultBarThickness(1, 0);
      }

      function main(nLength, displace)

      {
      if (nLength == null) {nLength = 21;}

      var yValues = new Array(nLength-1);

      var HValues = getValue("High", 0, -nLength);
      var LValues = getValue("Low", 0, -nLength);
      var CValues = getValue("Close", 0, -nLength);

      if (HValues == null || LValues == null || CValues == null ) {return;}

      for (i = 0; i < nLength; ++i)
      {yValues[i] = ((HValues[i] + LValues[i] + CValues[i])/3);}

      //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


      • #4
        Nice!

        You're a genious! Well, anyway you are much appreciated -- it worked like a charm.

        I simply cut the code from here and pasted it into the editor and it worked like a charm.

        Many thanks!

        David

        Comment

        Working...
        X