Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Defining secondary time frame causing object reference error

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

    Defining secondary time frame causing object reference error

    Hi,

    I have a working strategy that I wanted to add a secondary time frame to. The only line I have added is within Initialize()...

    Add(PeriodType.Day, 1);

    The strategy compiles fine but then when running a backtest I get the following error message.

    **NT** Error on calling 'OnBarUpdate' method for strategy 'XXX': Object reference not set to an instance of an object.

    The strategy is set to run on the 1 minute time frame as it's primary frame. I'm sure that's not enough info but any pointers as to where I should look for issues?

    Thanks,
    darmbk.

    #2
    darmbk, do you run a CurrentBars check for all bars series then in your script?

    Comment


      #3
      Hi Bertrand,

      Thanks for your quick response. I had not included those lines. However, I get the same result. I am including them as the first lines in OnBarUpdate().

      I note that my code contains a test of CurrentBar == 20. I imagine I need to change this to CurrentBars[0] == 20. Are there other changes that I may need to make? How would I edit High[0] or Time[0] for instance?
      Last edited by darmbk; 06-28-2013, 05:54 AM.

      Comment


        #4
        Ok, that's good, would keep them still in place. Do you filter your logic then by BarsInProgress for the script?

        Comment


          #5
          No I have not employed a test on which bar type is closing. I now have done the following to OnBarUpdate() but unfortunately I get the same error...

          protected override void OnBarUpdate()
          {
          if(CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
          return;

          if(BarsInProgress == 0)
          {
          // All code from original strategy in here
          }

          Do I need to edit High[0] to High(CurrentBars[0])[0]? even though I have tested on BarsInProgress?
          Last edited by darmbk; 06-28-2013, 06:10 AM.

          Comment


            #6
            I would then suggest isolating out exactly where in your OnBarUpdate() that empty object is being attempted to be accessed, C#'s try / catch techniques could help here - http://www.ninjatrader.com/support/f...ead.php?t=9825

            Do you work with the IOrder objects in this script?

            Comment


              #7
              Thanks Bertrand,

              I will break everything down as you suggest to pinpoint the problem. Yes I have IOrder objects in the script.

              Comment


                #8
                Then after the things we've been through, would pay extra attention to those and making sure not accessing their properties without a check for null.

                Comment


                  #9
                  Hi Bertrand,

                  I have used 'try' and have found so far at at least that the script throws an error on the following code with OnBarUpdate...

                  if (ToTime(Time[0]) == ToTime(startHour, 0, 0))
                  {
                  // Trade signal variable values
                  volumeSumOverPeriod = SUM(Volume, volumePeriod)[0];
                  highOverPeriod = MAX(High, volumePeriod)[0];
                  lowOverPeriod = MIN(Low, volumePeriod)[0];
                  volumePeriodRange = Math.Round((highOverPeriod - lowOverPeriod) / TickSize);

                  // Output trade signal variables
                  tradeSignalLog = Time[0] + " Start: Volume = " + volumeSumOverPeriod + ", Range = " +
                  volumePeriodRange + " ticks" + ", Range High = " + highOverPeriod +
                  ", Range Low = " + lowOverPeriod;
                  Print(tradeSignalLog); // Print to Output Window
                  WriteStream(swLog, tradeSignalLog, "Failed to write tradeSignalLog to log file", optimization);
                  }

                  Note WriteStream is a user defined method that employs 'try' itself.

                  Comment


                    #10
                    You will need to go even further then, same issue if the custom writestream is commented out?

                    Comment


                      #11
                      Ok, so I use the WriteStream in many places. When I commented it out the strategy worked. Here is the code for the WriteStream, what is the problem here?

                      private void WriteStream(StreamWriter sw, String str, String err, Boolean optimization)
                      {
                      if(!optimization) // Output to log/data files
                      {
                      try
                      {
                      sw.WriteLine(str);
                      }
                      catch (Exception e)
                      {
                      Log(err,NinjaTrader.Cbi.LogLevel.Error);
                      throw;
                      }
                      }
                      }

                      Comment


                        #12
                        My best guess would be then needs a null check for swLog in your WriteStream call?

                        Comment


                          #13
                          Hi Bertrand,

                          Thanks for the solution, my code now works.

                          Finally then, could you explain why I might need the test of null for the swLog object here? Why was it not causing a problem prior to me adding another bar type to CurrentBars?

                          Thanks,
                          darmbk.

                          Comment


                            #14
                            Ok, glad to hear it - would tip on the code call event timing / sequence to be changed by adding the other series in, so ran into a race condition with it that had not happened before, the call for null certainly helps.

                            Comment


                              #15
                              Hi Bertrand,

                              Sorry you lost me after "glad to hear it"! Could you dumb that down for me?

                              Thanks,
                              darmbk.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CarlTrading, 03-31-2026, 09:41 PM
                              1 response
                              157 views
                              1 like
                              Last Post NinjaTrader_ChelseaB  
                              Started by CarlTrading, 04-01-2026, 02:41 AM
                              0 responses
                              91 views
                              1 like
                              Last Post CarlTrading  
                              Started by CaptainJack, 03-31-2026, 11:44 PM
                              0 responses
                              143 views
                              2 likes
                              Last Post CaptainJack  
                              Started by CarlTrading, 03-30-2026, 11:51 AM
                              0 responses
                              131 views
                              1 like
                              Last Post CarlTrading  
                              Started by CarlTrading, 03-30-2026, 11:48 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post CarlTrading  
                              Working...
                              X