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

unhandled exception message

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

    unhandled exception message

    Hello,
    I work in my script with handling my stop-orders with lines (instead of chart trader). The idea I have from price alert indicator from NinjaTrader-Patrick.

    And it works all fine for YM, MYM, ES, MES.... eg when I click my button the stoploss moves to the line (or any other conditions with other buttons)
    As it works ok for days I changed instruments in my used dataseries to trade with FXCM FRA40, UK100.

    Script is enabled without errormessage, entries done with conditions. All OK. But when I click my button to move the stoploss I get the attached error message. I have no idea why.

    (when writing these lines now I´m thinking if the reason could be that its not a "normal stoploss" if trading with Ninjatrader and fxcm, monitoring the trade in fxcm platform it shows there instead of a stoploss for a shortposition an entry long. But you will know better how your business-partner is working when trading with Ninjatrader).

    Thank you!
    Tony
    Attached Files
    Last edited by tonynt; 02-09-2024, 06:29 AM. Reason: translation error

    #2
    Hello tonynt,

    From the error message it appears some part of your code is using an index that is invalid at that time. You would need to debug the script to see what part of the code is affected in that use case to get a better idea on what problem is happening.

    You can use print statements in your buttons logic to see where the code gets to before encountering the error.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Runtime errors can be difficult.

      What does the stack trace from the exception say?

      Wait, what? You're not handling the exception?

      Tony, Tony, Tony ... my friend ... bro ...

      Come here, let's consider the obvious.

      (music stops, puts arm around shoulder, mood
      turns serious, in a low voice you hear ...)

      Handle the exception, and print the stack trace.

      -=o=-

      I suggest handling the exception at the highest level.

      I never 'use' the NT callbacks directly. I mean, I've
      decided that all they should do is call a subordinate
      handler method, which then contains all the code.

      For example, most code you see for OnBarUpdate
      is monolithic. But, I prefer this model,

      Code:
      protected override void OnBarUpdate()
      {
          try {
              OnBarUpdate_Handler();
          }
          catch (Exception ex) {
              HandleException(ex);
              throw;
          }
      }
      So, the monolith code can remain (if desired), but
      it's been moved to a subordinate handler, precisely
      so you can catch all the exceptions below it.

      And here's the real beauty, inside the catch block
      you have total control again.

      The point is:
      HandleException(ex) is your chance to print the
      exception error message and the stack trace.

      Something like this,

      Code:
      private void HandleException(Exception ex)
      {
          StringBuilder sb = new StringBuilder(1024);
      
          do {
              sb.AppendLine("<EXCEPTION: " + ex.Message.Trim() + ">");
              sb.Append(ex.StackTrace);
              ex = ex.InnerException;
          } while (ex != null);
      
          Print(sb.ToString());
          Log(sb.ToString(), LogLevel.Error);
      }
      This will give you a much better fighting chance to
      find the offending code.

      You're welcome.

      Now go slay that bug.

      Last edited by bltdavid; 02-09-2024, 01:09 PM. Reason: typo

      Comment


        #4
        Thanks all.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        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
        10 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        15 views
        0 likes
        Last Post AaronKoRn  
        Started by carnitron, Yesterday, 08:42 PM
        0 responses
        11 views
        0 likes
        Last Post carnitron  
        Started by strategist007, Yesterday, 07:51 PM
        0 responses
        14 views
        0 likes
        Last Post strategist007  
        Working...
        X