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

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?

    BertrandNinjaTrader Customer Service

    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?

        BertrandNinjaTrader Customer Service

        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?
            BertrandNinjaTrader Customer Service

            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.
                BertrandNinjaTrader Customer Service

                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?
                    BertrandNinjaTrader Customer Service

                    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?
                        BertrandNinjaTrader Customer Service

                        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.
                            BertrandNinjaTrader Customer Service

                            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 andrewtrades, Today, 04:57 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post FAQtrader  
                              Started by rocketman7, Today, 09:41 AM
                              5 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X