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

SUM DataSeries

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

    SUM DataSeries

    Either it Is it too early in the morning for me or I am not understanding some basics. I am doing some testing to see where my thinking is going astray and getting some values I don't understand.

    In my indicator I have created a data series named testSeries.

    In OnBarUpdate() I have

    testSeries.Set(0,5);
    testSeries.Set(
    1,3);
    Value.Set(SUM(testSeries,2)[0]);

    Why does my indicator value show 352 and not 8? What am I misunderstanding?

    #2
    Hello dkrumholz,

    Thank you for your post.

    I have tested the small sample of code you provided and I do not receive the same results.

    Is there additional calculations used to set the Data Series?

    How is this Data Series used in conjunction with the indicator and/or plots?

    I look forward to your response.

    Comment


      #3
      Indicator File for Testing

      The indicator .cs file is in the attached ZIP. Thanks!
      Attached Files

      Comment


        #4
        Hello dkrumholz,

        Thank you for your response.

        You are getting the summation of testSeries over the last 2 bars, which happens to be 2. You will notice the indicator plot is increasing by two.

        To add the two values of the testSeries together you can simply use '+' as in the following code below:
        Code:
        Value.Set(testSeries[0] + testSeries[1]);
        Please let me know if you have any questions.

        Comment


          #5
          So when SUM takes a period parameter it is specifying what periods should be summed for all elements in the DataSeries? That sounds kind of useless. Specifying the periods to sum would be much more useful. Suppose I want to sum the values in the data series for the last n periods. Do I need to use a for loop?

          Comment


            #6
            Originally posted by dkrumholz View Post
            So when SUM takes a period parameter it is specifying what periods should be summed for all elements in the DataSeries? That sounds kind of useless. Specifying the periods to sum would be much more useful. Suppose I want to sum the values in the data series for the last n periods. Do I need to use a for loop?
            No. SUM() is, in your case, correctly attempting to sum the last 2 values of the DataSeries. However, because the SUM() class uses a running accumulation method, it will correctly sum the values, if and only if, past values do not change.

            Your method is changing the past value by a difference of -2. What that means is that every time the code runs, the accumulated value will be off by a difference of +2, so the SUM() class output will be off by values given by an arithmetic progression with a difference of +2. Simply put, on each run of the code, it is adding 5, (your most recent value), and subtracting 3, (your modified value: you changed "5" to "3" for the past value, on each run). Ergo, every run actually increases the value by the difference, 5 - 3 = 2.

            To get correct values, you could Dispose() of the SUM() class on each BarUpdate, this forcing the class to reinitialize each time. Be warned that doing so will put your historical bars into a state that will make your code take an enormous part of the NT messaging loop, which will appear to hang NT until your code finishes doing all that Disposing() on the historical bars. If you go down that road, you may want to force realtime calculation only. All in all, a rather unsatisfactory solution IMHO.

            The alternative is what you have correctly identified. Use a for loop, as that will work on the values that exist at the time that it is run, thus picking up your modified values, with no side-effects from history. If you are changing historical values, it turns out that a runtime loop is the only way to get correct results. (i.e, this is one of those times when a loop may well be unavoidable).
            Last edited by koganam; 02-05-2013, 01:26 PM.

            Comment


              #7
              Thanks!! Any guess as to where I would find the docs on which functions perform similar accumulations? I didn't see that when I checked the help - but I figured I was misunderstanding something about the function. BTW - that code was only the simplest way of demonstrating the question - I realize I wouldn't want to update the data series that way.

              Comment


                #8
                Originally posted by dkrumholz View Post
                Thanks!! Any guess as to where I would find the docs on which functions perform similar accumulations? I didn't see that when I checked the help - but I figured I was misunderstanding something about the function. BTW - that code was only the simplest way of demonstrating the question - I realize I wouldn't want to update the data series that way.
                Unfortunately, no. The classes that NT ships by default, have self-contained documentation, if any. The only way to know what scheme is used for calculations that involve using a sum of past values is to examine the code by using the editor.
                Last edited by koganam; 02-05-2013, 02:07 PM. Reason: Corrected grammar.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by GussJ, 03-04-2020, 03:11 PM
                15 responses
                3,271 views
                0 likes
                Last Post xiinteractive  
                Started by Tim-c, Today, 02:10 PM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by Taddypole, Today, 02:47 PM
                0 responses
                2 views
                0 likes
                Last Post Taddypole  
                Started by chbruno, 04-24-2024, 04:10 PM
                4 responses
                51 views
                0 likes
                Last Post chbruno
                by chbruno
                 
                Started by TraderG23, 12-08-2023, 07:56 AM
                10 responses
                403 views
                1 like
                Last Post beobast
                by beobast
                 
                Working...
                X