Announcement

Collapse
No announcement yet.

the code behind LinearRegressionStudy

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

  • the code behind LinearRegressionStudy

    I am trying to duplicate/understand the code behind the LinearRegressionStudy. I can duplicate the Basis line, but I am unable to figure out how the upper and lower bands are calculated. Does anyone know how to access the code behind the study?

    Thx

  • #2
    The outside lines of a Linear Regression study is a specified number of Standard Deviations above and below the basis line. You can specify the amount of Standard Deviations in the Chart Options --> Edit Studies menu.
    Regards,
    Jay F.
    Product Manager
    _____________________________________
    Have a suggestion to improve our products?
    Click Support --> Request a Feature in eSignal 11

    Comment


    • #3
      the code behind LinearRegressionStudy

      Thanks, Jay. I appreciate it.

      I thought that was the case, so i tried it. Although my regression center line (that i have calculated in my own javascript) seems to always agree with the study, my standard deviations agree only occasionally, and are off considerably at other times. I concluded that it was my code for the standard deviations, so I copied an excellent utility posted by another member, rhbishop, which can be found here:



      Unfortunately, Bob's std dev and mine tend to always agree. Hence my conundrum, and desire to see the LinearRegressionStudy code.

      For the record, my mehtodology is to calculate the lin reg center line (basis) and then add 1 or 2 standard deviations, and subtract 1 or 2 standard deviations from it, using the same periodicity.

      Comment


      • #4
        The code for the LinearRegressionStudy is hard-coded in eSignal, so we won't be able to post that.

        Here's a few things to check.

        Is the built-in study taking into account the same number of bars? The default setting for the number of bars for the Linear Regression Study is 0, which means all data loaded is taken into account. If there is a specified number of bars, then are they equal?

        I've done some checking with Excel and the STDEV Worksheet Function, it looks like there may be an issue with the calculation we are using. I'll look further into this with our QA Team.
        Regards,
        Jay F.
        Product Manager
        _____________________________________
        Have a suggestion to improve our products?
        Click Support --> Request a Feature in eSignal 11

        Comment


        • #5
          LinearRegressionStudy

          Thanks, Jay.

          One thing that I really need to point out--the LinearRegressionStudy works very well for me as is. I dont necessarily think that it needs to be "fixed". I was just trying to understand and duplicate its results so that i could speed a few things up in my own analysis.

          Cheers,
          Bob

          Comment


          • #6
            I have found a problem with the standard deviations calculated by the linear regression study as per the posting:-



            I would like to know the methodolgy used as soon as possible as I want to finish coding a replacement auto regression tool of my own and want to clarify the Standard Deviation computation. Everything else checks out ie the regression line itself as well as the Raff channels.

            Robert

            Comment


            • #7
              i have now utilized a working algorithm for the LR channels which can be found in the following thread:-



              Hope it helps.

              Robert

              Comment


              • #8
                Thanks, Robert.

                Let me give that a whirl.

                Cheers

                Comment


                • #9
                  Robert and Bob,

                  Looks like everything's is as expected now (if I'm understanding the two threads correctly.) If any questions remain, please let me know, and I'll do what I can to answer or get an answer.
                  Regards,
                  Jay F.
                  Product Manager
                  _____________________________________
                  Have a suggestion to improve our products?
                  Click Support --> Request a Feature in eSignal 11

                  Comment


                  • #10
                    I would like to make some further comments in line with ongoing research that I am performing in the investigation of use of statistics for trading purposes, and invite anyone who can give a further insight to my points:-

                    1. Because we are using a LR line, are we indeed calculating Standard Deviation or Standard Error channels?

                    2. If it is Standard Deviation then instead of dividing by "Period", we should divide by (Period-1) which is used to correct the downward bias when deriving a Standard Deviation value. In fact eSignal's channels are drawn with this correction, so eSignal believes it to be a Standard Deviation.

                    3. The values used for Standard Deviation and Standard Error are the same i.e. 2 gives 95% confidence.

                    My own thinking is that a standard deviation should be based on the price data that was used to derive the linear regression line which is drawn from the end point backwards.

                    So my theory goes as follows:-

                    Calculate a Mean (say SMA) of Price Data (i.e. Close whatever exactly as that used to calculate the LR which is a least squares or best fit method). Perform a Standard Deviation i.e:-

                    i =0;
                    While i < Period
                    SumSqr = (PriceData[i] - Mean ) * (PriceData[i] - Mean )
                    i++
                    End

                    Standard Deviation = SumSqr(Period-1)
                    Upper channel = (No x Std Dev) + LR projected backwards
                    Lower channel = LR - (No x Std Dev) projected backwards
                    Use 2 to get 95% confidence values

                    Theoretically Standard Error = SD/SqRoot(Period)
                    so SE should just equal what was calculated previously and divided by the square root of Period
                    Upper channel = (No x Std Err) + LR projected backwards
                    Lower channel = LR - (No x Std Err) projected backwards
                    Again use 2 to get 95% confidence values

                    However what has been calculated within eSignal is something different altogether and maybe what I have described above only pertains to drawing bands around a basis line i.e. an SMA. Back to the LR line and what has been calcualted as channels, we calculate the intercept of the LR line at every bar using the slope at the end point, in which to find the SumSqr of the difference between the Price Data and the derived interecept at that point. We then divide that by (Period-1) and say that is the Standard Deviation. My point is that the case?

                    If indeed what has been calculated is the Standard Deviation what would be the calculation of Standard Error, that value divided by the square root of Period?

                    If there are any mathmaticians out there (or at least someone who has got an angle on this - sorry for the pun!) who could throw some light on this, it would be greatly helpful to understand what we are calculating, as it seems to me Standard Error should be the goal as this would find variability of the Standard Deviation to show how accurate this would be and therefore any prices outside of the SE bands could be considered reversal points.

                    Robert

                    Comment


                    • #11
                      LinearRegressionStudy

                      I would like to thank everyone for their help on this issue. My bands now match (or are very close to) the ending Linear Regression Study values.

                      For the record, my difficulties centered on which spread to use when computing the standard deviation. I had been computing a mean and squaring the difference between that mean and respective values. This code squares the dif between the regression line and values--a material difference.

                      I also had a coding error in these lines:
                      nLR = Intercept + (Slope * (nLength-1-i));
                      nSumDiff += (yValues[i] - nLR) * (yValues[i] - nLR);

                      The term "Slope*(nLength-1-i)" had been "Slope*(nLength-1)" which meant that I was transposing the regression line order when finding the dif between a value and the corresponding regression line value for a given point in time.

                      My finished code is:


                      function preMain()
                      {
                      setPriceStudy(true);

                      setStudyTitle("LRBands");
                      setPriceStudy(true);
                      setDefaultBarFgColor(Color.blue, 0); // upper
                      setDefaultBarFgColor(Color.red, 1); // basis
                      setDefaultBarFgColor(Color.blue, 2); // lower
                      setDefaultBarThickness(2, 0);
                      setDefaultBarThickness(2, 1);
                      setDefaultBarThickness(2, 2);
                      }
                      function main(nLength,nSource) {
                      if(nLength == null){
                      nLength = 64;
                      }
                      if(nSource == null){
                      nSource = "close";
                      }
                      var yValues = new Array(nLength-1);

                      yValues=getValue(nSource,0,-nLength);
                      if (yValues == null) {
                      return;
                      }


                      //basis

                      var SumBars=0;
                      var SumSqrBars=0;
                      var Sum1 = 0;
                      var Sum2 = 0;
                      var SumY = 0;
                      var Slope=0;
                      var Intercept=0;
                      var LRV=0;


                      var AvgSumSqr=0;
                      var LRTop=0;
                      var LRBot=0;

                      SumBars=nLength*(nLength-1)*.5;
                      SumSqrBars=nLength*(nLength-1)*(2*nLength-1) / 6;

                      for(i = 0; i <nLength; i++){
                      SumY=SumY+close(-i);
                      Sum1=Sum1+i*close(-i);
                      }
                      Sum2=SumBars*SumY;
                      Num1=nLength*Sum1-Sum2;
                      Num2=SumBars*SumBars-nLength*SumSqrBars;

                      if (Num2 != 0){
                      Slope=Num1/Num2;
                      }else{
                      Slope=0;
                      }
                      Intercept=(SumY-Slope*SumBars)/nLength;
                      LRV=Intercept+Slope*(nLength-1);

                      //Std Dev
                      SdDev = 0;
                      nLR = 0;
                      nSumDiff = 0;
                      nStdDevFactor = 2;

                      for ( i = (nLength- 1); i >= 0; i--){
                      nLR = Intercept + (Slope * (nLength-1-i));
                      nSumDiff += (yValues[i] - nLR) * (yValues[i] - nLR);
                      }

                      SdDev = Math.sqrt(nSumDiff / nLength);


                      //Bands
                      LRTop=LRV+2*SdDev;
                      LRBot=LRV-2*SdDev;

                      return new Array(LRTop,LRV,LRBot);
                      }

                      Comment


                      • #12
                        I have concluded my research into the use of Standard Deviation and Standard Error with Linear Regression and have discovered that eSignal is using the wrong methodology in computing what Standard Error channels not Standard deivation channels as given by your formula. Please see this thread for further information.



                        Robert

                        Comment

                        Working...
                        X