Announcement

Collapse
No announcement yet.

Slope of Linear Regression Bars

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

  • Slope of Linear Regression Bars

    I would like to plot out the slop of the linear regression lines for each bar. Does anybody know how to do this? Thanks

  • #2
    bpschoch
    I think the slope is the current value minus the prior value divided by the prior value and multiplied by 100.
    Alex

    Comment


    • #3
      The problem with the linear regression function is that it only returns the lines to plot that are relevant to the last bar. I would like to display the slope for all bars as the linear regression function changes slope

      Comment


      • #4
        bpschoch
        You could create an array or use ref() to retrieve the prior values.
        If you post the efs you are using perhaps someone can help.
        Alex

        Comment


        • #5
          I've attached the study.
          Attached Files

          Comment


          • #6
            bpschoch
            When using builtin studies you can retrieve historical values directly using getValue().
            For example in the case of your efs to retrieve the prior bar's value of study.getValue(LinearRegressionStudy.BASIS) you would just use study.getValue(LinearRegressionStudy.BASIS,-1). If you wanted the value of two bars ago it would be -2, etc.
            Alex

            Comment


            • #7
              I'm not sure how that's different than saving the value obtained at the previous bar and saving it?

              Comment


              • #8
                bpschoch
                From the point of view of returned values these would be exactly the same. However with the syntax I suggested you can directly address/retrieve the values for any point of the Regression Channel (for example 20 bars back) whereas in the other way you would have to use either ref() or an array at some point.
                Alex

                Comment


                • #9
                  The linear regression function is 3 parallel lines. What I find that I get for previous bars, is a value to plot that allows me to plot the parallel line for the last bar. I never am able to obtain the values in order for me to plot the parallel lines for previous bars. You will see this yourself if you run the previously submited study.

                  Comment


                  • #10
                    bpschoch
                    I have seen the efs, never the less I am not sure as to what exactly you are trying to do with it. As far as I can see it is plotting a ratio between the current value and the prior value which should be a constant throughout the length of the Regression Channel. Hence any point on that line will return the same ratio.
                    Alex

                    Comment


                    • #11
                      unlike almost every other study, the linear regression study plots 3 parallel lines that are the study as of the last bar. As each new bar comes out, a new set of lines is plotted. When I go back in history, I seem to get the values that plot out the current bar. I want to 'see' what the lines look like for each bar and plot out the slope changes include what happened in the past.

                      Comment


                      • #12
                        bpschoch
                        There are no historical values past the length of Regression Channel. As new bars get added the Regression Channel traslates forward removing the historical values. To see this set up a Basic Studies Linear Regression (not the efs but the one in Basic Studies) and set the length to 37 which is the same as the one used in your efs.
                        Alex

                        Comment


                        • #13
                          Well that brings up the crux of the original problem and that is how do I plot the slope changes of the linear Regression funtion from bar to bar?

                          Comment


                          • #14
                            Here are some snippets of code from some of the stuff I have written which will provide the information you want. I had to modify it slightly because I use it a little differently, but it will provide you the information you are requesting with respect to the values of Linear Regression. If you want any channels, you will have to add.

                            The way it is written you can send any parameter to the linear regression function and you will be able to track those variables


                            PHP Code:
                            //identify nArray as a global variable above premain
                            var LrArray;
                            var 
                            nArray;
                            var 
                            ni 5;//period of linear regression
                            var n1 20;//how much history you want to save


                            //in premain;

                            nArray Initialize_1X_Array(ni);
                            LrArray Initialize_2X_Array(n1,ni);

                            //in main
                                
                            var close();
                                
                            nArray.pop(); nArray.unshift(a); //populating data
                                
                            if (barcount ni){
                                    
                            //-------------------------------------------------------------------------------------------------------------------
                                    //lsf = LSF(nv1,fnArray) where lsf[0] = slope, lsf[1] = Int, lsf[2] = RR, lsf[3] = pred1, lsf[4] = pred2,
                                    
                            btest LSF((ni), nArray);//varying length of initial trend length between lb1 and le1 
                                    
                            LrArray.pop(); LrArray.unshift(btest); //populating data
                                    
                            slopebtest[0];// or LrArray[0][0]~ slope 
                                    
                            RR btest[2];// or LrArray[0][2]~ R squared  
                                
                            }


                            //after main
                            //---------------------------------------------------------------------------------------
                            function LSF(nv1,fnArray){//Linear Least Squares Fit routine
                                
                            if (nv1 2){nv1 2;}//prevents division by zero "ouch!!" for nv1 = 0
                                
                            var sXX 0.0;  var sYY 0;var sXY 0; var sX 0.0; var sY 0;var i;
                                for(
                            0nv1i++) {//y = nInt + nSlope*x
                                    
                            sX  += nv1-i;sY  += fnArray[i];
                                    
                            sYY += Math.pow(fnArray[i],2);sXX += Math.pow((nv1-i),2);
                                    
                            sXY += (nv1-i)*fnArray[i];
                                }
                                var 
                            xAve sX /nv1;var yAve sY nv1;
                                var 
                            nSlope = (nv1*sXY sX*sY) / (nv1*sXX sX*sX);
                                var 
                            nInt = (sY nSlope sX)/nv1;//if(debug)debugPrintln("a =  "+a);
                                
                            var RR= (Math.pow((sXY-nv1*yAve*xAve),2))/(((sXX-xAve*xAve*nv1)*(sYY-yAve*yAve*nv1)));
                                var 
                            pred1 nSlope*(nv1+1) + nInt;var pred2 nSlope*(nv1+2) + nInt;
                                return new Array (
                            nSlopenIntRR,pred1,pred2);
                            //lsf = LSF(nv1,fnArray) where lsf[0] = slope, lsf[1] = Int, lsf[2] = RR, lsf[3] = pred1, lsf[4] = pred2,
                            }//End  Linear Least Squares Fit routine


                            function Initialize_1X_Array(n1){//initialize 1 dim array
                                
                            var i;    var newArray = new Array();
                                if (
                            n1 == null){n1 =2;}
                                for (
                            0;n1i++){newArray[i] = 0;}
                                return 
                            newArray;
                            }
                                
                            function 
                            Initialize_2X_Array(n1,n2){//initialize 2 dim array
                                
                            var i; var j;var newArray = new Array;
                                if (
                            n1 == null){n1 =2;}if (n2 == null){n2 =2;}
                                for (
                            i=0;i<n1i++){newArray[i] =  new Array();
                                    for (
                            j=0;j<n2j++){newArray[i][j]=0;
                                }}
                                return 
                            newArray;

                            Comment


                            • #15
                              here's the file
                              Attached Files

                              Comment

                              Working...
                              X