Announcement

Collapse
No announcement yet.

Linear Regression / Least Squares Fit Code?

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

  • Linear Regression / Least Squares Fit Code?

    Hello,

    I am looking for a piece of code that returns a linear regression value for a given bar, as opposed to an array of values for the "n" lowest-numbered bars returned by /builtin/LinearRegressionStudy.efs

    The S&C web site (http://www.traders.com/Documentation...ps.html#wealth) contains a movtrend.efs file that presumably does the job. However, the code does not look much like least squares fit implementations that I have seen, contains a typo ("wt" vs. "mt"), and what appears to be hardcoded constants ("3" and "6"). Could someone confirm that the S&C source works as advertised, or post one that does?

    Thanks,
    dis
    ---
    P.S. movtrend.efs

    function preMain()
    {
    setStudyTitle("Movtrend");
    setCursorLabelName("Movtrend", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setPriceStudy(true);
    }
    function main(n) {
    if(n == null)
    n = 20;
    var sum = 0;
    var i = 0;
    var mt = 0;

    for(i = n; i > 0; i--)
    sum += (i - (n + 1) / 3) * close(i - n);
    wt = 6 / (n * (n + 1)) * sum
    return wt;
    }

  • #2
    Hello dis,

    After correcting the typo in movetrend.efs, I verified that it is producing the linear regression value for the current bar. The attached zip file is my code, which uses the traditional least squares method. If you apply the two files, you will see that they produce the same line.
    Attached Files
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X