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

Strategy OnBarUpdate Error

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

    Strategy OnBarUpdate Error

    Hello,
    is there a way to "catch" error - I mean perform action when I get this error? I want to echo variables when I get error.
    Thanks
    Paul

    #2
    Originally posted by kujista View Post
    Hello,
    is there a way to "catch" error - I mean perform action when I get this error? I want to echo variables when I get error.
    Thanks
    Paul
    Have you ever used a try-catch statement?

    Below is the method I use:

    Rename your current OnBarUpdate to OnBarUpdate_Handler, and make
    it a private method (no override), then add this code,

    Code:
    protected override void OnBarUpdate()
    {
        try {
            OnBarUpdate_Handler();
        }
        catch (Exception ex) {
            HandleException(ex);
            throw;
        }
    }
    
    private void HandleException(Exception ex)
    {
        StringBuilder sb = new StringBuilder(1024);
    
        sb.AppendLine(Name + " has encountered an error");
    
        do {
            sb.AppendLine("<EXCEPTION: " + ex.Message.Trim() + ">");
            sb.Append(ex.StackTrace);
            ex = ex.InnerException;
        } while (ex != null);
    
        string s = sb.ToString();
        Print(s);
        Log(s, LogLevel.Error);
    }
    ​
    This will catch all exceptions anywhere in OnBarUpdate and print
    a stack trace to the NinjaTrader output window and log file. You
    can adjust to taste, add more Print statements, etc, but this code
    above should be enough.

    Why? Because the real purpose is to print that stack trace

    Let me explain.

    Yes, this code should help you catch the errors, but it only works
    if the error is generating an exception
    . No matter, such errors are
    unpredictable and need 'catching' anyways, so you can see where they
    occurred. How so? By inspecting the stack trace, now you know which
    specific method generated the exception.

    The stack trace is a beacon, telling you which method(s) need attention.
    It is one of those method(s) where you need to "echo variables when I
    get error".

    The last method in the stack trace that contains code you wrote, that's
    the method (or one or more before it) that needs some investigative
    Print statements.

    Btw, this is one advantage of separating your code into lots of short
    quick methods -- it makes them easy to debug.

    Make sense?

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ageeholdings, Today, 07:43 AM
    0 responses
    7 views
    0 likes
    Last Post ageeholdings  
    Started by pibrew, Today, 06:37 AM
    0 responses
    4 views
    0 likes
    Last Post pibrew
    by pibrew
     
    Started by rbeckmann05, Yesterday, 06:48 PM
    1 response
    14 views
    0 likes
    Last Post bltdavid  
    Started by llanqui, Today, 03:53 AM
    0 responses
    6 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Started by burtoninlondon, Today, 12:38 AM
    0 responses
    12 views
    0 likes
    Last Post burtoninlondon  
    Working...
    X