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;              // <-- [URL="https://www.google.com/search?q=c%23+throw+exception+in+catch"]re-throw exception[/URL] [URL="https://stackoverflow.com/questions/22623/best-practices-for-catching-and-re-throwing-net-exceptions"]very important[/URL]
        }
    }
    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; // <-- [URL="https://www.google.com/search?q=c%23+throw+exception+in+catch"]re-throw exception[/URL] [URL="https://stackoverflow.com/questions/22623/best-practices-for-catching-and-re-throwing-net-exceptions"]very important[/URL]
          }
          }
          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.


            You could also use Visual Studio debugging which will show you the line.


            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 giulyko00, Yesterday, 12:03 PM
            2 responses
            10 views
            0 likes
            Last Post giulyko00  
            Started by r68cervera, Today, 05:29 AM
            0 responses
            3 views
            0 likes
            Last Post r68cervera  
            Started by geddyisodin, Today, 05:20 AM
            0 responses
            6 views
            0 likes
            Last Post geddyisodin  
            Started by JonesJoker, 04-22-2024, 12:23 PM
            6 responses
            36 views
            0 likes
            Last Post JonesJoker  
            Started by GussJ, 03-04-2020, 03:11 PM
            12 responses
            3,241 views
            0 likes
            Last Post Leafcutter  
            Working...
            X