Announcement

Collapse
No announcement yet.

modifying R-Squared

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

  • modifying R-Squared

    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

    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_INSTANTCOLORLINE0);//changed from LINE
        
    setDefaultBarThickness(20);
        
    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(nLRlennLRThicknessnLRColorsDisplaynUppernLower) {
        if (
    bInit == false) {
            
    setDefaultBarThickness(nLRThickness0);
            
    setDefaultBarFgColor(nLRColor0);
            if (
    sDisplay == "Histogram") {
                
    setPlotType(PLOTTYPE_HISTOGRAM0);
            } else {
                
    setPlotType(PLOTTYPE_LINE0);
            }
            
    addBand(nUpperPS_SOLID1Color.blue"upperBand");
            
    addBand(nLowerPS_SOLID1Color.blue"lowerBand");
            
    xClose close();
            
    xLinReg efsInternal("LinReg"nLRlenxClose);
            
    xR getSeries(xLinReg,2);//added
            
    bInit true;
        }    
            
        if (
    xLinReg.getValue(0) != null) {
            var 
    getSeries(xLinReg0);  // Slope
            
    var getSeries(xLinReg1);  // y-intercept
            
    var getSeries(xLinReg2);  // 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(nLRlenx) {
        if (
    x.getValue(-nLRlen) == null) return;
        
        var 
    xSum 0;
        var 
    ySum 0;
        var 
    sumXY 0;
        var 
    sumX2 0;
        var 
    sumY2 0;
        
    0;
        for (
    0nLRlen; ++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;
        
    0;
        for (
    0nLRlen; ++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 = (aSum1 aSum2);
        var 
    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(
    ABR2);

    Attached Files

  • #2
    kz
    1) The reason the INSTANTCOLORLINE plot set in line 22 is not being applied is because that property is then set in line 64 of the script. That is where you need to change the plot type from _LINE to _INSTANTCOLORLINE
    2) The addition in line 70 ie xR = getSeries(xLinReg,2) is not incorrect but it is redundant since the series for R-squared is being retrieved in line 77 and assigned to the variable R. This means that you can remove line 70 and in lines 79-87 use R.getValue(n) instead of xR.getValue(n)
    Alex


    Originally posted by zeller4
    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
    ...

    Comment


    • #3
      Thanksgiving...

      Alex,

      Your help is much appreciated! I was able to get it working, thanks to you.


      kz

      Comment


      • #4
        kz
        You are most welcome
        Alex

        Comment

        Working...
        X