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

BarsArray[1].Count is 3 bars less than BarsArray[0].Count?

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

    BarsArray[1].Count is 3 bars less than BarsArray[0].Count?

    This has me stumped...

    I have a Daily Heiken-Ashi chart open.
    I have added the native data series with AddDataSeries (ie plain old Daily bars).in my Indicator's State.Configure, and I see that data just fine.

    So... BarsArray[0] is daily Heiken-Ashi, BarsArray[1] is just daily, on the same symbol.

    I am getting occasional Object Ref error.s on this expression...BarsArray[1].GetOpen(bar_number), where bar_number is CurrentBar.
    Debugging in VS, I see this on the exception breakpoint:

    BarsArray[0].Count is 650
    BarsArray[1].Count is 647

    So calling BarsArray[1].GetOpen(bar_number) craps out and throws the exception... well, yeah, but...

    Why the 3 bar/3 day difference?

    FYI:
    • my chart's bar series does NOT have Break at EOD set, if that is relevant.
    • this occurs on a bar that is a Tuesday in my timezone...August 3rd 2021... and
    • just now occurred on a different chart on bar for Wednesday August 4th. (Hard to see how even a weekend could be involved on a Wednesday bar...even allowing for Australia being one day ahead of US. Just like the Olympic Gold medal count ;-) )
    Cheers,
    T.
    Last edited by tgn55; 08-05-2021, 01:55 AM.

    #2
    What is your Calculate setting?

    Comment


      #3
      OnBarClose. Very vanilla.
      Oh... and if you were wondering, yes of course I have this in OBU:
      Code:
      [COLOR=#569cd6]if[/COLOR] (BarsInProgress [COLOR=#b4b4b4]==[/COLOR] [COLOR=#b5cea8]1[/COLOR])   [COLOR=#569cd6]return[/COLOR];
      My final bullet point in the prev post, which I added some hours back, was because on one of my charts, I had just got a new daily bar. This was 5 PM local time, on Sugar (/SB) Would have been I think 3:30 AM EST, when Sugar session opened for the day. I got a fresh bar for my Thursday, the indicator choked on the close of my bar for Wednesday. And still had 3 bars difference between BarsArray [0] and [1] ... 648 vs 645, CurrentBar is 646, ie one less than the max ID of 647 on the primary series.

      Cheers,
      T.
      Last edited by tgn55; 08-05-2021, 06:47 AM.

      Comment


        #4
        Hi tgn55, thanks for posting.

        If you need to control the number of bars in each series please use this overload:

        AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chris

          Problem is, I run this indicator ONLY on Heiken-Ashi charts... and want to load the equivalent raw data as the secondary series.
          I do this by capturing some of the master instrument properties, then pass these to AddDataSeries, (Some I use later for other purposes)
          Code:
          [COLOR=#569cd6]case[/COLOR] State[COLOR=#b4b4b4].[/COLOR]Configure:[INDENT].
          .[/INDENT][INDENT]MI [COLOR=#b4b4b4]=[/COLOR] Bars[COLOR=#b4b4b4].[/COLOR]Instrument[COLOR=#b4b4b4].[/COLOR]MasterInstrument;
          
          MI_BB_periodtype [COLOR=#b4b4b4]=[/COLOR] BarsPeriod[COLOR=#b4b4b4].[/COLOR]BaseBarsPeriodType;     // BaseBarsPeriodType is the key thing here...
          MIperiod [COLOR=#b4b4b4]=[/COLOR] BarsPeriod[COLOR=#b4b4b4].[/COLOR]BaseBarsPeriodValue;
          
          AddDataSeries(Bars[COLOR=#b4b4b4].[/COLOR]Instrument[COLOR=#b4b4b4].[/COLOR]FullName, MI_BB_periodtype , MIperiod);[/INDENT][INDENT].
          .[/INDENT]
           
           break;
          Now, the signature you suggested does not include periodtype... only BarsPeriod (and my BarsPeriod is set to type 9 for Heiken-Ashi). But I need non-Heiken-Ashi as my secondary series, so it looks like I need an almost clone of the BarsPeriod object, but with a few properties tweaked??

          Please confirm, or advise how else to get both the bars type, period and number of bars loaded... all in synch.

          Surely the default behaviour should be to load the same number of bars as the primary series ?? That it clearly does not do that seems like a bug...

          Cheers,
          T.
          Last edited by NinjaTrader_ChrisL; 08-05-2021, 09:26 AM.

          Comment


            #6
            Hi tgn55,

            The default behavior is to load the same number of bars. I uploaded my test script which works on Daily Heiken-Ashi and the additional 1-day series, both series have the same amount of bars loaded in State.DataLoaded. I'm testing on Kinetick data. The overload I recommended in my first post requires you to build the BarsPeriod e.g.

            AddDataSeries("", new BarsPeriod() { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 50, "", true);

            The difference you are seeing might be data-feed specific, or a lack of historical data, which would be unlikely as Daily data should go back many years. So first, test my script and see what the count of each BarsArray object is. Also, try loading a Daily chart of this instrument to confirm you can load the data on a normal Daily chart. After this, test the aforementioned overload for AddDataSeries to request a specific number of bars from the data provider.

            Kind regards,
            -ChrisL
            Attached Files
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Quick answers... I am pulling CQG data, custom dates from 1/1/19 to present, so 2 1/2 years. Confirmed a separate chart on daily normal bars gets same number of bars as my HA chart. So... yeah it still seems weird for there to be 3 days difference. That's just not cool.

              It's late here... I'll test your script and code tomorrow... thanks for the help.

              T.

              Comment


                #8
                Curiosity got the better of me... I played. Confirmed your script return same bar counts.
                Now it gets a bit weird... now my chart/indicator also works fine. (This is where support guy rolls eyes and says, "yeah... driver error" ;-)

                So... at best it is intermittent... I have the logs that show Obj Ref errors happening a few hours ago! More info... this problem only manifests under certain conditions (I draw a bunch of trade info on my chart mimicking Strategy Analyser...problem appears when I show a closed trade, and then open a new daily bar... so, it is not happening all the time ;-) But, when it does bite (ie Object ref error...) VS tells me the counts are different... which I assume is reliable. Perhaps not ???

                Anyway, pushing it further now really is a bit much... heading for 2 AM.. I'm off the bed! Will resume tomorrow.

                Cheers,
                T.

                Comment


                  #9
                  Originally posted by tgn55 View Post
                  I am getting occasional Object Ref error.s on this expression...BarsArray[1].GetOpen(bar_number), where bar_number is CurrentBar.
                  So, you're doing this in context of BarsInProgress == 0, right?

                  When BIP == 0, using CurrentBar always reflects the primary bar series.

                  If your secondary data series (for whatever reason) is not in sync, then I
                  can see why the error itself occurs when too few secondary bars exist.

                  Have you tried using CurrentBars[1] instead?
                  Code:
                  BarsArray[[B][COLOR=#e74c3c]1[/COLOR][/B]].GetOpen(CurrentBars[[B][COLOR=#e74c3c]1[/COLOR][/B]])
                  Perhaps that would be avoid the intermittent error you're seeing.

                  Comment


                    #10
                    Also, I believe the primary bar series is processed first, before your secondary series.

                    This could be important if you assume too much about the current state of your secondary
                    bars when processing BarsInProgress == 0 ... perhaps NT Support can comment further.

                    Comment


                      #11
                      Btw, have you considered using the underlying primary data series to get your Day data,
                      rather than an additional secondary data series?

                      What I mean is,
                      I presume you're using the Heiken-Ashi BarType to build your chart. Don't do that.
                      Try using a regular Day chart instead -- then add the Heikan-Ashi indicator to the chart.

                      Why do this?
                      Because this allows the values in the underlying primary data series (eg, Open/Close, etc)
                      to remain unmolested.

                      The Heiken-Ashi indicator maintains it own OHLC data series (eg, HAOpen/HAClose, etc)
                      and uses OnRender to redraw the candlesticks based upon the HA* data series.

                      Perhaps that approach would work better for you?
                      Good reading here.

                      Comment


                        #12
                        Hi tgn55, thanks for your reply.

                        I would recommend reducing the code you are working on to identify where the exception is coming from. The bars are loading properly once State.DataLoaded has been reached so additional debugging on your end will be required.

                        Kind regards,
                        -ChrisL
                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by tgn55 View Post

                          So... at best it is intermittent...
                          Hi TGN,

                          Just an FYI. Not sure this applies to purely Daily Bar types.

                          While developing custom bar types for intraday use I noticed intermittent dis-synchronization in period bar counts would occur between bar types on the same underlying base type as a result of session iteration, and a few other behaviors that make me want to 'upvote' a test using bltdavid's suggestions in post number 11 above.

                          "have you considered using the underlying primary data series to get your Day data, rather than an additional secondary data series?"

                          HedgePlay

                          Comment


                            #14
                            Thanks all for the suggestions.

                            Firstly, Chris, I don't believe this is related to data not being reliable until State.DataLoaded, as the aberrant behaviour only manifests under certain circumstances on the penultimate bar of the chart... in my case, around bar ID 650. By then my indicator has done a bunch stuff prior. Can't see how it can be an initialisation thing, and I can't see why on the almost last call to OnBarUpdate, the counts should be different. And I know exactly where the exception is coming form... the call to GetOpen with a parameter that exceeds the (incorrect) bar count!

                            Now... general comments.
                            1. I can't debug this at present... at the behaviour changes when new bars are created (see below)
                            2. CurrentBar... Up until the final bar, I believe the GetOpen(tradebar) is pulling the correct raw Daily data from series 1. You can actually see this in my pic, where trade opens/closes do NOT draw at the open of the HA bars, which would be wrong ;-)

                              Might need to reveal more of what I actually doing. The pic below is a screenshot of my indicator... yes, my indicator, not an NT tool . (I'll come back to WHY? later...)

                              Click image for larger version

Name:	PDL 1.png
Views:	373
Size:	8.9 KB
ID:	1166785

                              Prior to the exchange open (I think), ie when the final bar was still forming, I was getting an ObjectRef error on the previous bar. (on the GetOpen expression) .. where the trade exit is now showing, and VS would indicate the discrepancy in BarsArray.Count numbers... like this

                              Click image for larger version

Name:	Immediate 1.png
Views:	312
Size:	4.3 KB
ID:	1166786
                              This pic was taken whilst VS was in break mode when the exception occurred. 650 vs 647...
                            3. After the final bar is completed, I get NO object ref exception, my code runs just fine, and it shows the pic above, and the BarsArray counts are now in agreement ! Yup... And all I did was F5 to refresh the indicator. Do we all agree that is just weird?
                            4. I call OnRender to adjust the placement of my drawing objects, but can't easily see how this would impact BarsArray[1].Count...?
                            I might play around swapping my data series as bltdavid suggested... but, that isn't a preferred option, but right now, I need to wait until one of my charts shows a closing trade... and then jump onto it before the next bar closes.

                            Thanks all for your interest.

                            T.
                            Last edited by tgn55; 08-06-2021, 09:50 AM. Reason: Fixed image upload issue

                            Comment


                              #15
                              Hi tgn55, thanks for your reply.

                              The image files you posted did not go through. Please re-upload those when you get the chance.
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trilliantrader, Today, 08:16 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post trilliantrader  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              9 responses
                              174 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by funk10101, Today, 08:14 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post funk10101  
                              Started by adeelshahzad, Today, 03:54 AM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by RookieTrader, Today, 07:41 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X