Announcement

Collapse
No announcement yet.

LinearRegressionCalc.efs

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • LinearRegressionCalc.efs

    File Name: LinearRegressionCalc.efs


    Description:
    Example formula for demonstrating the Linear Regression calculations.

    Formula Parameters:
    nLength: 10

    Notes:
    The green circles highlight the price series the regression line is based on for visual reference.

    Download File:
    LinearRegressionCalc.efs




    EFS Code:
    PHP Code:
    /*********************************
    Provided By : eSignal. (c) Copyright 2004
    *********************************/
    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Linear Regression ");
        
    setShowCursorLabel(false);
        
    }

    var 
    aY null;
    var 
    null;

    function 
    main(nLength) {
        var 
    nState getBarState();
        var 
    nIndex getCurrentBarIndex();
        var 
    0;
        
        if (
    nLength == nullnLength 10;
        if (
    aY == nullaY = new Array(nLength);
        
        if (
    nState == BARSTATE_NEWBAR && != null) {
            
    aY.pop();
            
    aY.unshift(c);
        }
        
        
    close();
        
    aY[0] = c;

        
        if (
    nState == BARSTATE_NEWBAR) {
            if (
    aY[nLength-1] != null) {
                
    1;
                for (
    1nLength; ++i) {
                    
    drawShapeRelative(-iaY[i], Shape.CIRCLEnullColor.limenull"dot"+i);
                }
            }
        }
        if (
    aY[0] != null) {
            
    drawShapeRelative(0aY[0], Shape.CIRCLEnullColor.limenull"dot0");
        }
        
        
    // y = Ax + B;
        // A = SUM( (x-xAVG)*(y-yAVG) ) / SUM( (x-xAVG)^2 )
        // A = slope
        // B = yAVG - (A*xAVG);
        
        
    if (aY[nLength-1] != null) {
            var 
    xSum 0;
            var 
    ySum 0;
            
    0;
            for (
    0nLength; ++i) {
                
    xSum += i;
                
    ySum += aY[i];
            }
            var 
    xAvg xSum/nLength;
            var 
    yAvg ySum/nLength;
            var 
    aSum1 0;
            var 
    aSum2 0;
            
    0;
            for (
    0nLength; ++i) {
                
    aSum1 += (i-xAvg) * (aY[i]-yAvg); 
                
    aSum2 += (i-xAvg)*(i-xAvg);
            }
            var 
    = (aSum1 aSum2);
            var 
    yAvg - (A*xAvg);
            
    //debugPrintln(nIndex + "  Slope: " + -A.toFixed(4) + "  Y-intercept: " + B.toFixed(4));
            
    drawLineRelative(0B, -(nLength-1), (A*(nLength-1)) + BPS_SOLID2Color.red"reg");
        }
        
        return;

    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
Working...
X