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

How to acertain valid data in Series?

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

    How to acertain valid data in Series?

    Let me describe my observation of the Series as background info.

    The default Series size is 256. When the size bound is crossed, it behaves as a round/circular buffer. But the older data/position is not initializes to default (like 0, false as is usually in C#), whatever older data still remain.

    Is my description above as far correct?

    If yes, how to initialize to default values (like 0, false) before working on it? I note that I can't use null check (assuming Series contains a class var), as it'll be always not null after the 1st pass through the 256 size.
    I also note that I can get around the problem by setting the Series to MaximumBarsLookBack.Infinite, but assuming I want to minimize memory usage & leave it at 256 size, how?

    Appreciate any idea you can share. Thanks.

    #2
    Hello Tiang,

    Thank you for your post.

    Can you clarify on the following?
    Originally posted by Tiang View Post
    But the older data/position is not initializes to default (like 0, false as is usually in C#), whatever older data still remain.
    Are you referring to the data behind the 256 bars from the current bar? How are you trying to access this data? What do you mean by initialize, default, and 0?

    I look forward to your response.

    Comment


      #3
      I've never worried about this. Maybe you are overthinking the situation.

      You simply just don't look back further than you have bars for.

      Comment


        #4
        Thanks Patrick & sledge for your reply even on Martin King holiday!
        Let me elaborate on my use case.
        1) I'm not using data more than 256 bars back.
        2) My Series variable usage is enum Flags (doing bit-wise operations on it), data/strategy running OnEachTick basis in OnMarketData().
        So after the 1st round through the 256 size, need to initialize the Series otherwise bit-wise operations generate wrong result due to old data from 1st round.
        3) I tried experimenting with using null check but it doesn't work in this case since after 1st round, the Series never null (assuming Series is a class although my flag variable isn't class).

        My main question is this:
        How to best know that this [location] in Series[] was from a previous round (hence contain old data) so that I can initialize it to a known state (eg. 0) before I start performing bit-wise operation on it?

        Comment


          #5
          Hello Tiang,

          Thank you for your response.

          From my understanding you are stating that the prior bars that are outside the 256 should be reset/initialized to a default value. This is not true. However, your inquiry suggestions that they are somehow skewing the current values?

          Can you provide an example of what you are doing in code, what behavior you are seeing and what behavior you expect?

          I look forward to your response.

          Comment


            #6
            Hi Patrick,

            Thanks for your clarification that Series outside of 256 size isn't auto initialized to a default value. I've a better understanding of why my code didn't work after the 256 size. Let me depict a simplified structure of my code:
            class X
            During State.DataLoad :
            myXseries = new Series<X>(this);
            OnBarUpdate(){
            if (myXseries[0]==null){ myXseries[0]=new X(); }
            ......
            }
            /*my code happens to work within the 256 size (which I now understand) due to the side-effect of new X() which initialize to default value. But after the 256 size, the null check would always failed. I erroneously thought myXseries[0] would be null since it for the lastest new bar (which would be correct if the series is set to MaximumBarsLookBack.Infinite). My code also uses myXseries in OnMarketData() with bit-wise OR. I realized now I need to initialize myXseries, & I'm using something like: */

            if (IsFirstTickOfBar){ ...init myXSeries[0] to default value....} in OnBarUpdate(). The default value for my case is zero so that when bit-wise OR operation in OnMarketData() won't be corrupted by value from previous 256 size rounds.

            My remaining question is: Is it for-sure OnMarketData() is always invoked after OnBarUpdate()?

            Thanks Patrick for your patience; I'm a beginner coder.
            Tiang
            Last edited by Tiang; 01-21-2018, 10:27 PM.

            Comment


              #7
              Hello Tiang,

              Thank you for your response.

              Please correct me if I am wrong but you are saying that the current bar for your series is not null after 256 bars but is null before 256 bars until you provide it a value?
              Is it for-sure OnMarketData() is always invoked after OnBarUpdate()
              OnMarketData() is called after OnBarUpdate() for the Last event when you are running Calculate.OnEachTick. However, OnMarketData() is called when other market data events occur which means it is called at times that OnBarUpdate() is not.

              Please provide a sample script that demonstrates what you are seeing. You can export your indicator by going to Tools > Export > NinjaScript Add On > Add > select your indicator > OK > Export > name the file 'NTsupport' > Save. The file will be located under Documents\NinjaTrader 8\bin\Custom\ExportNinjaScript. Please attach the file to your response.

              I look forward to your response.

              Comment


                #8
                Hi Patrick,

                I've attached a simplified indicator code to show what I'm driving at. My original code is not as indicator but used in strategy.
                The Output window should shows my points:
                1) new X in OnBarUpdate at CurrentBar 20 ... 275, which is the 1st round of 256 size.
                I assign 3.0 to the newly created (& only newly created) seriesA[0].xd
                2) The print of seriesA[0] for CurrentBar from, say, 280 show also 3.0, which was the value assigned during the 1st round. Hence I was saying those values from the previous round are still there. This won't be a problem if new value is assigned (which overwrite the previous value), but it was an error for my case b'cos my variable (xd) is a flag var & I'm doing bit-wise operation. Hence my bit-wise operation result is corrupted by the previous round value. I can avoid the problem by seriesA = new Series<X>(this,MaximumBarsLookBack.Infinite).

                Hope I describe the problem clearly,
                Tiang
                Attached Files

                Comment


                  #9
                  Hello Tiang,

                  Thank you for your patience.

                  So potentially you need an Array that has it's elements reset per bar and only have the Array use a specific max size?

                  The Series is going to contain a value for each bar on the chart and going back and resetting values is possible but not recommended as an Array with a specific amount of elements would be better utilized in this case.

                  Please let me know if you have any questions.

                  Comment


                    #10
                    Hi Patrick,

                    I don't have further questions; thanks, but let me summarize my understanding for the benefit of others.
                    The default Series size is 256 units, & it behave like a circular buffer/storage. Each unit of the Series is linked to a bar in the chart, but when the chart has more than 256 bars (typically), when a unit is assigned data in the newer bar, the old data is overwritten, & the program code should be good. Problem would occurs if you depend on the unit to hold certain data (some initial state) before working on it, as the old data in the previous cycle of the circular buffer remains. If this is your use-case (mine is), then I know of 2 ways to overcome (although not sure they're the best way):
                    1) Use MaximumBarLookBack.Infinite for the Series.
                    2) Initialize each Series unit to your correct initial state b4 working on it.

                    Thank-you Patrick for your attention,
                    Tiang

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by prdecast, Today, 06:07 AM
                    1 response
                    4 views
                    0 likes
                    Last Post NinjaTrader_LuisH  
                    Started by Christopher_R, Today, 12:29 AM
                    1 response
                    14 views
                    0 likes
                    Last Post NinjaTrader_LuisH  
                    Started by chartchart, 05-19-2021, 04:14 PM
                    3 responses
                    577 views
                    1 like
                    Last Post NinjaTrader_Gaby  
                    Started by bsbisme, Yesterday, 02:08 PM
                    1 response
                    15 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by i019945nj, 12-14-2023, 06:41 AM
                    3 responses
                    60 views
                    0 likes
                    Last Post i019945nj  
                    Working...
                    X