Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

troubles DataSeries

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

    troubles DataSeries

    Hi

    I want modify DataSeries and get forecast with LinReg
    I wrote next code
    protectedoverridevoid Initialize(){
    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = false;

    myDS = new DataSeries(this);}

    protectedoverridevoid OnBarUpdate(){
    int m = 5;
    if (CurrentBar < m) return;

    if (CurrentBar % m == 0){
    for (int i=0; i<m; i++) {
    myDS.Set( i, m+i);
    Print("i= "+i+" m+i="+(m+i)+" myDS.Get(myDS.Count-i)= "+myDS.Get(myDS.Count-i));
    }
    double value = LinReg(myDS, m-1)[0];
    Print(" The current LinReg "+m+" value is " + value.ToString());
    }
    and after a few bars got printed
    i= 0 m+i=5 myDS.Get(myDS.Count-i)= 0
    i= 1 m+i=6 myDS.Get(myDS.Count-i)= 5
    i= 2 m+i=7 myDS.Get(myDS.Count-i)= 6
    i= 3 m+i=8 myDS.Get(myDS.Count-i)= 7
    i= 4 m+i=9 myDS.Get(myDS.Count-i)= 8

    The current LinReg 5 value is -37
    Why if i= 1 and m+i=6 the myDS.Get(myDS.Count-i)= 5 if code
    myDS.Set( i, m+i);

    and
    how I can get forecast for only the last members myDS
    example 5 from myDS.Count- 4 to myDS.Count-0 ?

    Thkx
    Last edited by Nikoli; 04-03-2010, 11:40 AM.

    #2
    Nikoli, from what I can tell, everthing is behaving as expected. You are using the .Set overload (DataSeries.Set(int barsAgo, double value)) that specifies how many bars back you want to set a value. In the for loop, the code just fills your DataSeries with data (up to i bars back) and then prints the information.

    Keep in mind that DataSeries are reverse indexed, that is the most recent value is stored at the index value of 0 and the oldest value is stored at DS.Count - 1.

    You may want to review the documentation for DataSeries.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Austin View Post
      Nikoli, from what I can tell, everthing is behaving as expected. You are using the .Set overload (DataSeries.Set(int barsAgo, double value)) that specifies how many bars back you want to set a value. In the for loop, the code just fills your DataSeries with data (up to i bars back) and then prints the information.

      Keep in mind that DataSeries are reverse indexed, that is the most recent value is stored at the index value of 0 and the oldest value is stored at DS.Count - 1.
      Austin, from the first I wrote
      for (int i=0; i<m; i++) {
      myDataSeries.Set( i, m+i);
      Print("i= "+i+" m+i="+(m+i)+" myDataSeries.Get(i)= "+myDataSeries.Get(i)+" myDataSeries[i].ToString()= " + myDataSeries[i].ToString()
      );}
      double value = LinReg(myDataSeries, m-1)[0];
      Print(
      " The current LinReg "+m+" value is " + value.ToString());
      and got next
      i= 0 m+i=5 myDataSeries.Get(i)= 0 myDataSeries[i].ToString()= 5
      i= 1 m+i=6 myDataSeries.Get(i)= 9 myDataSeries[i].ToString()= 6
      i= 2 m+i=7 myDataSeries.Get(i)= 4 myDataSeries[i].ToString()= 7
      i= 3 m+i=8 myDataSeries.Get(i)= 3 myDataSeries[i].ToString()= 8
      i= 4 m+i=9 myDataSeries.Get(i)= 2 myDataSeries[i].ToString()= 9
      The current LinReg 5 value is 10243,2
      where error?
      example
      myDataSeries.Set( 1, 6);
      Print(
      "i= "+1+" m+i="+6+" myDataSeries.Get(i)= "+myDataSeries.Get(1));
      in penultimate member set 6
      Why penultimate .Get(1) return 9 and [1] return 6 ?

      and

      how I can get forecast for only the last 5 members myDataSeries?
      Last edited by Nikoli; 04-04-2010, 01:48 AM.

      Comment


        #4
        Nikoli, please only use the brackets to access DataSeries values, not the .Get methods. I believe that will help clear up confusion. In addition, unless absolute necessary, setting historical DataSeries values in a for loop isn't recommended. Wrong values can be set very easily.

        Now I'm not exactly sure what you mean by forecasting only the last 5 members of myDataSeries, but if you wanted to run a linear regression on the most recent 5 values of your DataSeries you could do this:
        Code:
        double forecasted_value = LinReg(myDataSeries, 5)[0];
        AustinNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Austin View Post
          Nikoli, please only use the brackets to access DataSeries values, not the .Get methods. I believe that will help clear up confusion. In addition, unless absolute necessary, setting historical DataSeries values in a for loop isn't recommended. Wrong values can be set very easily.

          Now I'm not exactly sure what you mean by forecasting only the last 5 members of myDataSeries, but if you wanted to run a linear regression on the most recent 5 values of your DataSeries you could do this:
          Code:
          double forecasted_value = LinReg(myDataSeries, 5)[0];
          as a result we have very simple code
          protectedoverridevoid OnBarUpdate(){
          int
          m = 5;
          if (CurrentBar < m) return;

          if (CurrentBar % m == 0){
          Print(Time[0].ToLongTimeString()+" ============");
          for (int i=0; i<m; i++) { // input and output for check m=5 rececent value
          myDataSeries.Set( i, m+i);
          Print(
          "i= "+i+" m+i="+(m+i)+" myDataSeries[i].ToString()= " + myDataSeries[i].ToString() );
          }
          // output forecast for check
          double value = LinReg(myDataSeries, m)[0];
          Print(
          " The current LinReg "+m+" value is " + value.ToString());
          }
          and print-results of the code
          3:14:38 ============
          i= 0 m+i=5 myDataSeries[i].ToString()= 5
          i= 1 m+i=6 myDataSeries[i].ToString()= 6
          i= 2 m+i=7 myDataSeries[i].ToString()= 7
          i= 3 m+i=8 myDataSeries[i].ToString()= 8
          i= 4 m+i=9 myDataSeries[i].ToString()= 9
          The current LinReg 5 value is 5
          3:14:48 ============
          i= 0 m+i=5 myDataSeries[i].ToString()= 5
          i= 1 m+i=6 myDataSeries[i].ToString()= 6
          i= 2 m+i=7 myDataSeries[i].ToString()= 7
          i= 3 m+i=8 myDataSeries[i].ToString()= 8
          i= 4 m+i=9 myDataSeries[i].ToString()= 9
          The current LinReg 5 value is -13
          3:14:58 ============
          i= 0 m+i=5 myDataSeries[i].ToString()= 5
          i= 1 m+i=6 myDataSeries[i].ToString()= 6
          i= 2 m+i=7 myDataSeries[i].ToString()= 7
          i= 3 m+i=8 myDataSeries[i].ToString()= 8
          i= 4 m+i=9 myDataSeries[i].ToString()= 9
          The current LinReg 5 value is -31
          We can see:
          - 5 the most recent values is stable (9, 8, 7, 6, 5)
          - but forecast on 5 the most recent values change on each step - why ?
          - linear regression from (9, 8, 7, 6, 5) must be 4 - why we have 5 as better result ?
          Last edited by Nikoli; 04-04-2010, 09:11 PM.

          Comment


            #6
            Nikoli, have you tried another method as such as the SMA on your custom dataseries for example as well? With which CalculateOnBarClose setting are you running this test?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Yes, I tried different metods, but results was bad.
              Look, It is all code
              protectedoverridevoid Initialize(){
              CalculateOnBarClose = true;
              Overlay = false;
              PriceTypeSupported = false;
              myDataSeries = new DataSeries(this);
              }

              protectedoverridevoid OnBarUpdate(){
              int m = 5;
              if (CurrentBar < m || (Historical)) return;

              if (CurrentBar % m == 0){
              Print(Time[0].ToLongTimeString()+" ============");
              for (int i=0; i<m; i++) { // input and check m=5 rececent value
              myDataSeries.Set( i, m+i);
              Print("i= "+i+" m+i="+(m+i)+" myDataSeries[i].ToString()= " + myDataSeries[i].ToString() );
              }
              double value = LinReg(myDataSeries, m)[0];
              Print(" The current LinReg "+m+" value is " + value.ToString());
              Print(" The current LinReg "+m+" SMA(myDataSeries,1) value is " + LinReg(SMA(myDataSeries,1), m)[0].ToString());
              }
              We get next print-results
              16:24:02 ============
              i= 0 m+i=5 myDataSeries[i].ToString()= 5
              i= 1 m+i=6 myDataSeries[i].ToString()= 6
              i= 2 m+i=7 myDataSeries[i].ToString()= 7
              i= 3 m+i=8 myDataSeries[i].ToString()= 8
              i= 4 m+i=9 myDataSeries[i].ToString()= 9
              The current LinReg 5 value is 5
              The current LinReg 5 SMA(myDataSeries,1) value is 5
              16:24:07 ============
              i= 0 m+i=5 myDataSeries[i].ToString()= 5
              i= 1 m+i=6 myDataSeries[i].ToString()= 6
              i= 2 m+i=7 myDataSeries[i].ToString()= 7
              i= 3 m+i=8 myDataSeries[i].ToString()= 8
              i= 4 m+i=9 myDataSeries[i].ToString()= 9
              The current LinReg 5 value is -13
              The current LinReg 5 SMA(myDataSeries,1) value is -0,6
              16:24:12 ============
              i= 0 m+i=5 myDataSeries[i].ToString()= 5
              i= 1 m+i=6 myDataSeries[i].ToString()= 6
              i= 2 m+i=7 myDataSeries[i].ToString()= 7
              i= 3 m+i=8 myDataSeries[i].ToString()= 8
              i= 4 m+i=9 myDataSeries[i].ToString()= 9
              The current LinReg 5 value is -31
              The current LinReg 5 SMA(myDataSeries,1) value is -6,6
              Pls, copy the code on Y comp and test indicator.

              I think, CalculateOnBarClose not must influence on calculation.
              And straight use myDataSeries must get the most correct results.
              Last edited by Nikoli; 04-05-2010, 07:24 AM.

              Comment


                #8
                Thanks Nikoli, I'm getting the same results here on my end, checking into.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  Thanks Nikoli, I'm getting the same results here on my end, checking into.
                  Thanks Bertrand, I'm waiting Y results.
                  Can I help Y ?

                  Comment


                    #10
                    Nikoli, you're weclome - you will need to check into your loop logic for assigning the custom data series values, as for example my snippet below works as expected for me on ES in testing -

                    Code:
                    myDataSeries.Set(Bars.BarsSinceSession);
                    		
                    		double value0 = SMA(myDataSeries, 10)[0];
                    		double value1 = LinReg(myDataSeries, 10)[0];
                    		
                    		Print(" The current value is " + myDataSeries[0].ToString());
                    		Print(" The current SMA value is " + value0.ToString());
                    		Print(" The current LinReg value is " + value1.ToString());
                    Can you please confirm if this is also the case on your end?

                    Thanks
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      I pasted Y's code in my
                      protectedoverridevoid OnBarUpdate(){
                      int m = 10;
                      if (CurrentBar < m || (Historical)) return;

                      if (CurrentBar % m == 0){
                      Print(Time[0].ToLongTimeString()+" ============");
                      for (int i=0; i<m; i++) { myDataSeries.Set( i, m+i); }

                      double value = LinReg(myDataSeries, m)[0];
                      Print(" The current LinReg "+m+" value is " + value.ToString());
                      Print(" The current LinReg "+m+" SMA(myDataSeries,1) value is " + LinReg(SMA(myDataSeries,1), m)[0].ToString());

                      myDataSeries.Set(Bars.BarsSinceSession);

                      double value0 = SMA(myDataSeries, 10)[0];
                      double value1 = LinReg(myDataSeries, 10)[0];

                      Print(" The current value is " + myDataSeries[0].ToString());
                      Print(" The current SMA value is " + value0.ToString());
                      Print(" The current LinReg value is " + value1.ToString());
                      }


                      correct?

                      and got print

                      21:22:12 ============
                      The current LinReg 10 value is 10
                      The current LinReg 10 SMA(myDataSeries,1) value is 10
                      The current value is 13442
                      The current SMA value is 1357,7
                      The current LinReg value is 10
                      21:22:40 ============
                      The current LinReg 10 value is -4676,78181818182
                      The current LinReg 10 SMA(myDataSeries,1) value is -13432,3454545455
                      The current value is 13452
                      The current SMA value is 1345,2
                      The current LinReg value is -4676,78181818182
                      21:23:04 ============
                      The current LinReg 10 value is -9367,01818181818
                      The current LinReg 10 SMA(myDataSeries,1) value is -26885,3454545455
                      The current value is 13462
                      The current SMA value is 1332,7
                      The current LinReg value is -9367,01818181818

                      Comment


                        #12
                        Nikoli, my point was simply that your custom loop filling the dataseries is the issue here, when you don't use this but for example the generic one I provided the values hit home as expected.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          Nikoli, my point was simply that your custom loop filling the dataseries is the issue here, when you don't use this but for example the generic one I provided the values hit home as expected.
                          Bertrand, pls give me full code Y's indicator.

                          Comment


                            #14
                            Sure, please try for example the attached on 6.5
                            Attached Files
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              Nikoli, my point was simply that your custom loop filling the dataseries is the issue here, when you don't use this but for example the generic one I provided the values hit home as expected.
                              Bertrand, task is:
                              1. modify several recent memberS ( NOT ONE THE LAST )
                              2. and forecast from them

                              Pls, write Your indicator with the conditions.

                              I think, not importance how we modify several recent members - in loop or in few lines of code. Because we checked it in Print() - values is correct.
                              We can use code with modify in few lines from Your example indicator with LinReg().
                              Note: modify several recent members is correct and legal.

                              But, what happen next when we try forecast with LinReg() ?

                              Where error ?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Kaledus, Today, 01:29 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post Kaledus
                              by Kaledus
                               
                              Started by PaulMohn, Today, 12:36 PM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by yertle, Yesterday, 08:38 AM
                              8 responses
                              36 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by rdtdale, Today, 01:02 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X