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 jxs_xrj, 01-12-2020, 09:49 AM
    6 responses
    3,290 views
    1 like
    Last Post jgualdronc  
    Started by Touch-Ups, Today, 10:36 AM
    0 responses
    9 views
    0 likes
    Last Post Touch-Ups  
    Started by geddyisodin, 04-25-2024, 05:20 AM
    11 responses
    62 views
    0 likes
    Last Post halgo_boulder  
    Started by Option Whisperer, Today, 09:55 AM
    0 responses
    8 views
    0 likes
    Last Post Option Whisperer  
    Started by halgo_boulder, 04-20-2024, 08:44 AM
    2 responses
    25 views
    0 likes
    Last Post halgo_boulder  
    Working...
    X