Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How does the LinReg() work?

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

    How does the LinReg() work?

    Dear NT developers,

    Below is the contents of a data series collection and the return values of LinRegSlope, LinRegIntercept and LinReg calculated on this collection:

    Code:
    myDataSeries[0]=5
    myDataSeries[1]=4
    myDataSeries[2]=3
    myDataSeries[3]=2
    myDataSeries[4]=1
    LinRegSlope(myDataSeries,myDataSeries.Count)=1
    LinRegIntercept(myDataSeries,myDataSeries.Count)=1
    LinReg(myDataSeries, myDataSeries.Count)=2
    LinReg(myDataSeries,3)=2,5
    Based on what LinRegSlope() and LinRegIntercept() return, the equation is y=x+1 , it is clear.

    But, I can't figure out why the predicted value is 2 when the regression is plotted using all five values. And why is it 2.5 when it is plotted based on the previous three values? I would think it should be 5 in both cases.

    Could you help with this please?

    Thank you,
    Evgeny.
    Last edited by ananas; 12-16-2013, 04:54 AM.

    #2
    Hi Evgeny,

    Thank you for your post.

    Can you clarify a few things.

    What are the dataseries values? What is being passed into them?

    What type of chart are you plotting this on? Instrument and time interval?

    Can you provide your whole code so I can review and test this on my end?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply.

      The instrument doesn't matter here, because the data series myDataSeries is a class member and is filled in the strategy class itself (not by NT behind the scenes) on every bar update. The data series values are printed out in the first message.

      See the code and the run settings attached.
      Attached Files

      Comment


        #4
        Ananas,

        What do you expect these values to be showing.

        Code:
        LinReg(myDataSeries, myDataSeries.Count)=2
        LinReg(myDataSeries,3)=2,5
        These two lines are calculating different LinReg. The first one will read out LinReg(myDataSeres, 5)

        While the bottom one is showing LinReg(myDataSeries,3)
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Sorry, your explanation doesn't make sense to me. Please look into the explanation below.

          Indeed, these two lines build different regressions equations, but the predicted value should be the same in both cases, it should be 5.

          So, we have the following data in myDataSeries :

          myDataSeries[0]=5
          myDataSeries[1]=4
          myDataSeries[2]=3
          myDataSeries[3]=2
          myDataSeries[4]=1
          then if we build regression using the last three values, i.e.:

          myDataSeries[0]=5
          myDataSeries[1]=4
          myDataSeries[2]=3
          then the regression equation is y=x+3

          Next, if we build regression based on five values, then the regression equation is y=x+1

          But, the predicted value should be the same in both cases, it is 5.

          I noticed that the predicted value of LinReg() is correct until this code is uncommented (please find the updated script version attached):

          // If this block is uncommented, then LinReg(myDataSeries,3) and LinReg(myDataSeries,5) below
          // return incorrect predicted values.
          // if (cnt>=3)
          // {
          // Print("Before LinReg(myDataSeries,count)=" + LinReg(myDataSeries,myDataSeries.Count)[0]);
          // }
          The output when the code above is commented out, see the predicted values by LinReg() is 5 and that is as expected:

          myDataSeries.Count=5
          myDataSeries[0]=5
          myDataSeries[1]=4
          myDataSeries[2]=3
          myDataSeries[3]=2
          myDataSeries[4]=1
          LinRegSlope(myDataSeries,myDataSeries.Count)=1
          LinRegIntercept(myDataSeries,myDataSeries.Count)=1
          LinReg(myDataSeries, myDataSeries.Count)=5
          LinReg(myDataSeries,3)=5
          LinRegSlope(myDataSeries,3)=1
          LinRegIntercept(myDataSeries,3)=3
          The output after the code above is uncommented:

          myDataSeries.Count=5
          Before LinReg(myDataSeries,count)=2
          myDataSeries[0]=5
          myDataSeries[1]=4
          myDataSeries[2]=3
          myDataSeries[3]=2
          myDataSeries[4]=1
          LinRegSlope(myDataSeries,myDataSeries.Count)=1
          LinRegIntercept(myDataSeries,myDataSeries.Count)=1
          LinReg(myDataSeries, myDataSeries.Count)=2
          LinReg(myDataSeries,3)=2,5
          LinRegSlope(myDataSeries,3)=1
          LinRegIntercept(myDataSeries,3)=3
          The predicted value by LinReg(myDataSeries, 5) is 2.
          The predicted value by LinReg(myDataSeries, 3) is 2.5.

          This doesn't make sense. Could you comment on this?

          Thanks.



          Originally posted by NinjaTrader_Cal View Post
          Ananas,

          What do you expect these values to be showing.



          These two lines are calculating different LinReg. The first one will read out LinReg(myDataSeres, 5)

          While the bottom one is showing LinReg(myDataSeries,3)
          Attached Files
          Last edited by ananas; 12-24-2013, 06:22 AM.

          Comment


            #6
            Hello,

            The reason for this is that NinjaTrader indicators are "smart" in that NinjaTrader will only ever calculate an indicator once per bar and as it calculates it will cache the value and just return that instead. This adds efficiency as each time you reference LinReg() in your code it doesn't have to recalculate it each time only the first time.

            This does not work in the case of when you call the value and cache its result and then change the DataSeries. You would not then get the result you expect.

            This is a core feature with Indicators in NinjaTrader and there is no way to disable this caching of the calculation unfortunately.

            Only thing you could do is feed it a new seperate dataSeries which would cause it to recalculate.

            -Brett
            BrettNinjaTrader Product Management

            Comment


              #7
              Thanks Brett.

              I came across this feature while was trying to understand how LinReg() works. Now it is clear.

              Is my understanding correct that LinReg() doesn't allow to make prediction outside a data series?

              E.g.: if we have the following data series: [1; 2; 3; 4]

              how can I predict the next value (which is 5) with LinReg() or some else function?

              Thanks.
              Last edited by ananas; 01-01-2014, 04:58 AM.

              Comment


                #8
                Well by default the indicators current calculated value would be the prediction price for the next bar.

                As far as predicting out more then one bar out that would take some custom programming.

                -Brett
                BrettNinjaTrader Product Management

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                649 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                370 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                109 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                573 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                576 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X