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

can i get more error information in log?

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

    can i get more error information in log?

    Strategy 'XXXX': Error on calling 'OnBarUpdate' method on bar 2149: Object reference not set to an instance of an object.
    Disabling NinjaScript strategy 'XXXX/251559081'

    i have get the error msg in the log file. i know this error meaning, but i don't know it happen in which line. can i know which line or more information for the error?

    #2
    You'll need to catch the exception and print the stack trace.

    Try this idea,

    Rename your 'protected override void OnBarUpdate()' to
    'private void RealOnBarUpdate()' and then add this code,

    Code:
    protected override void OnBarUpdate()
    {
        try {
            RealOnBarUpdate();  // <-- your renamed OnBarUpdate
        }
        catch (Exception ex) {
            Print(ex.ToString());
            throw;              // <-- re-throw exception very important
        }
    }
    The idea is to wrap the entirety of all OnBarUpdate processing
    in a try/catch block -- so that you are guaranteed a full and compete
    stack trace any time OnBarUpdate generates an exception.

    Comment


      #3
      Oops, as you state in the subject line, you'll also want
      the exception message sent to the Ninja log file.

      Add this line inside the catch block,

      Log(ex.ToString(), LogLevel.Error);
      Last edited by bltdavid; 01-29-2022, 06:04 AM.

      Comment


        #4
        You should be aware of a warning from NinjaTrader.

        Huh? What warning?

        If you look at NT's own NinjaScript Best Practices, the
        'Error handling practices' section specifically advises
        against using a giant try/catch block.

        I would argue this 'giant' try/catch technique is actually
        quite manageable and can be extremely useful.

        It is very specific. The purpose is to catch all unhandled
        exceptions (which, by definition, are all bad) and the only
        thing it does with that exception is print the stack trace,
        but then it re-throws the exception so NinjaTrader's own
        exception handling can occur normally.

        Re-throwing the exception properly should negate any of
        the unmentioned problems this 'best practice' help page is
        warning against.

        As far as performance issues, there are zero, zilch, none,
        nada -- the try/catch is dormant code until an exception
        occurs -- at which point, the paramount concern is printing
        the stack trace and re-throwing the exception.

        Printing the stack trace (using this giant try/catch technique)
        in production code is completely safe -- and in my opinion,
        is the exact kind of production code you want.

        Simple. Effective. Brief. Fail-safe.

        Think about it, when an unhandled exception occurs, what
        happens? NinjaTrader stops the strategy. Is it that large
        of a 'performance' issue that you get a stack trace before
        the strategy is stopped? How else are you going to catch
        an unknown and unhandled error in production?

        I suggest you embrace my example code wholeheartedly
        and leave the code in production.

        Just my 2˘.

        Comment


          #5
          Originally posted by bltdavid View Post
          You'll need to catch the exception and print the stack trace.

          Try this idea,

          Rename your 'protected override void OnBarUpdate()' to
          'private void RealOnBarUpdate()' and then add this code,

          Code:
          protected override void OnBarUpdate()
          {
          try {
          RealOnBarUpdate(); // <-- your renamed OnBarUpdate
          }
          catch (Exception ex) {
          Print(ex.ToString());
          throw; // <-- re-throw exception very important
          }
          }
          The idea is to wrap the entirety of all OnBarUpdate processing
          in a try/catch block -- so that you are guaranteed a full and compete
          stack trace any time OnBarUpdate generates an exception.
          thanks your reply. i will try this.

          Comment


            #6
            Hello moneyexe,

            In general I recommend adding prints above each line. The last print to appear is above the line causing the error.
            https://ninjatrader.com/support/foru...121#post791121

            You could also use Visual Studio debugging which will show you the line.
            https://ninjatrader.com/support/help..._debugging.htm

            The error is telling you one of the variables you are using has a null value.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by emillieany, Today, 01:28 AM
            0 responses
            2 views
            0 likes
            Last Post emillieany  
            Started by GilbeCarra, Today, 01:22 AM
            0 responses
            3 views
            0 likes
            Last Post GilbeCarra  
            Started by oprahwinfreyketogummies, Today, 01:20 AM
            0 responses
            3 views
            0 likes
            Last Post oprahwinfreyketogummies  
            Started by ntbone, Today, 01:02 AM
            0 responses
            1 view
            0 likes
            Last Post ntbone
            by ntbone
             
            Started by nikholasjain, Today, 12:52 AM
            0 responses
            2 views
            0 likes
            Last Post nikholasjain  
            Working...
            X