Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trapped exception - BarsArray in 'Initialize' method

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

    Trapped exception - BarsArray in 'Initialize' method

    I've been getting a lot of fatal errors recently, and I plucked the following message out of the trace file:

    "...OnUnhandledThreadException
    ************exception trapped ************
    'BarsArray' property can't be accessed from within 'Initialize' method.
    at NinjaTrader.Indicator.IndicatorBase.get_BarsArray( )

    This exception was thrown when the following was executed:
    (DTN-IQ) Data.Bars.GetBarsNow: instrument = 'YM 12-12' from (all the stuff it normally asks for; it's on a different computer so I'm not going to type it all out).

    All this was done on an optimization run.

    Now, this is something I'm aware of - I do not, in fact, attempt to access the "BarsArray" property anywhere in my code on Initialize().

    I do access the following, and have been doing so without error for weeks now:
    PeriodType
    BarsPeriod

    That's it.

    So, if somebody has an idea why I would get the "BarsArray" exception, please let me know -

    #2
    cgeorgan, please do not access those properties in the Initialize(), as they would have not been defined in there, as there's no bars object yet.

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      cgeorgan, please do not access those properties in the Initialize(), as they would have not been defined in there, as there's no bars object yet.
      Wait, I don't follow - I can consistently access this information, and I've done so literally thousands of times through live trading.

      I know the base property of my strategy - it knows what the base property of its own bars is. I access that value so that I can pass it to multiple new series that each have multiples of the base series.

      This exception is rare - if it were a real problem I'd get hit with it literally dozens of times an hour. What's the story?

      Comment


        #4
        Correct, it might or might have not worked - the bottom line is - it should not be accessed in the Initialize() as there may be times when there's no object yet and thus you run into issues as you've seen -

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          Correct, it might or might have not worked - the bottom line is - it should not be accessed in the Initialize() as there may be times when there's no object yet and thus you run into issues as you've seen -

          http://www.ninjatrader.com/support/h...barsperiod.htm
          Again, this is something that I have a hard time making sense of. Programming isn't magic, things occur regularly and in sequence.

          When, exactly, is this information available for use to the programmer?

          Comment


            #6
            You can either work in OnStartUp() or the first OnBarUpdate() bar (CurrentBar == 0).

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              You can either work in OnStartUp() or the first OnBarUpdate() bar (CurrentBar == 0).
              Wow, I had no idea there was an additional method that essentially replicated "Initialize()". OnStartUp is exactly where this needs to be, thanks!

              Comment


                #8
                Originally posted by NinjaTrader_Bertrand View Post
                You can either work in OnStartUp() or the first OnBarUpdate() bar (CurrentBar == 0).
                Alright, here's why I realize now this didn't work the first time I tried it.

                I access the "PeriodType" and "BarsPeriod" values in the Initialize() method because I use that to create higher time frame series based on the original series fed to the strategy. These higher time frame series are then added with the add() method within the Initialize() method, because that's the only place a programmer can add data series.

                I honestly can't think of another way to programmatically add different time series based off the initial time series without doing it this way. If your programmers know another way that does not include hard-coding the base time frame type and period, I'm interested in hearing it.

                Thanks!

                Comment


                  #9
                  cgeorgan, your findings are accurate - we would unfortunately not support the dynamic adding of series 'on the fly' - you could just add a range of series per se and then access the bars array needed dynamically from the strategy (for example base 1 min, then add 2-15 in 1 min steps, drive strategy by BIP context then).

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    cgeorgan, your findings are accurate - we would unfortunately not support the dynamic adding of series 'on the fly' - you could just add a range of series per se and then access the bars array needed dynamically from the strategy (for example base 1 min, then add 2-15 in 1 min steps, drive strategy by BIP context then).
                    Well, truth be told, you actually can do this...you're just subject to the occasional (and I want to stress that "occasional" means once out of every 100 instances) exception being thrown.

                    Comment


                      #11
                      Originally posted by cgeorgan View Post
                      Well, truth be told, you actually can do this...you're just subject to the occasional (and I want to stress that "occasional" means once out of every 100 instances) exception being thrown.
                      Correct, it might or might not work for you - officially the dynamic adding of series is unfortunately not supported by us.

                      Comment


                        #12
                        Originally posted by cgeorgan View Post
                        Wow, I had no idea there was an additional method that essentially replicated "Initialize()". OnStartUp is exactly where this needs to be, thanks!
                        No, you can't rely on OnStartUp for bars 100% of the time. See post #13 here


                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        597 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        343 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        103 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        556 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        555 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X