Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Input string was not in a correct format.

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

    Input string was not in a correct format.

    I noticed my strategy had quit / crashed and, viewing the output window, I found the error in the subject:

    Error on calling 'OnBarUpdate' method on bar X Input string was not in a correct format

    I don't know the exact bar number -not important- but it had been working before.

    I tried it times to re-enable, and the same error kept coming up. I couldn't imagine what had changed in the code. But...

    Seemed like the major thing taking a string parameter is my Log function, which is called plenty from OnBarUpdate() :

    Code:
            private void Log(string message, [CallerMemberName] string memberName = "")
            {
                if (DisableLogging  ||  lastLogMsg == message)
                    return;
    
                // Set this scripts Print() calls to the second output tab?
                if (UseOutput2)        PrintTo = PrintTo.OutputTab2;
                else                          PrintTo = PrintTo.OutputTab1;
                
                string id = StrategyName + " " + UniqueID;
                
                DateTime date = (State == State.Historical) ? Time[0] : DateTime.Now;
                string dateStr = (State == State.Historical) ? date.ToString() : date.ToString("HH:mm:ss");
                
                if (lastCaller != memberName)
                {
                    Print($"\n{id} - {chartInstrument} - [{memberName}]  -  {dateStr}  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
                    Print(message);
                }
                else if (lastLogMsg != message)
                {
                    if (lastLogTime != dateStr)    // Output just time if diff time but not new caller
                        Print(message + $"   ( {dateStr} )");
                    else
                        Print(message);
                }
    
                lastLogMsg = message;
                lastCaller = memberName;
                lastLogTime = dateStr;
            }            
    ​

    So I commented out the middle part, like this:

    Code:
                /*
                if (lastCaller != memberName)
                {
                    Print($"\n{id} - {chartInstrument} - [{memberName}]  -  {dateStr}  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
                    Print(message);
                }
                else if (lastLogMsg != message)
                {
                    if (lastLogTime != dateStr)    // Output just time if diff time but not new caller
                        Print(message + $"   ( {dateStr} )");
                    else
                        Print(message);
                }
                */
    Started it up again, and no more error. I couldn't really see anything wrong with it, so I commented it back in. And the error doesn't happen anymore. Do you see anything wrong with that Log function?

    #2
    Hello DerkWehler,

    You would need to know what the string was that was incorrect format for what your logic is doing with it to answer this question. i would suggest making a more simple logging method or just use Print directly which supports a generic object to be printed as a string.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello DerkWehler,

      You would need to know what the string was that was incorrect format for what your logic is doing with it to answer this question. i would suggest making a more simple logging method or just use Print directly which supports a generic object to be printed as a string.
      You don't find it strange that just enabling the strategy, I got the same error repeatedly, then commented out code and it was fine, even when uncommented?

      Regarding the Log function, thanks for the suggestion, but I need the logging to present that information in the best format that allows me to scan it as quickly as possible to find what I need. If NT8 had more than 2 output windows (god knows why it's limited to 2), then maybe I could get by without some of that stuff. I run 5 sim tests at the same time, and with only 2 output windows, it gets quite tedious to try to separate which output comes from which chart. I'm sure you are aware how voluminous logging can get -which is another problem I have had: the output window only holding a limited amount of data, so it's even more important to keep the logging as terse as possible. It's a pretty slow method, but without a debugger, not much else I can do. Motivates me to learn how to get it working with visual studio. :-)

      Comment


        #4
        Hello DerkWehler,

        We dont have any details on what the problem was so no it would not be strange, that was a result of some specific use case where the string being used was not in a correct format. Without debugging the code and knowing exactly what string caused the error it would be a guess as to what the problem was.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Today, after it was running almost 2 hours, I got the error again which crashed the Strategy:

          Strategy 'Ladder': Error on calling 'OnBarUpdate' method on bar 9371306: Input string was not in a correct format.
          Disabling NinjaScript strategy 'Ladder/332258921'

          The code above in the Log function was commented out (it wasn't logging anything). So apparently when it seemed to fix it yesterday, I was not correct.

          Is there any recommendations to where I might look? It says "input string", so can I assume it means a string parameter in some function call? I could post the whole OnBarUpdate(), but it's ~400 lines...

          Comment


            #6
            Hello DerkWehler,

            If you are still having a problem you have to debug the code and find what the string was that was incorrect. I couldn't tell you by just looking at the code what may be the problem, you may need to add additional prints and see which specific line of code has the problem to continue.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you for the quick reply. I'm happy to do that, but not sure where to start. That is why I asked if I can assume it's a string parameter to a method call.

              It would be helpful if there an actual specification to the error codes, so I could understand what specifically throws that error. Without that, I don't know where to begin; if it crashed every time I ran it, I would put in Print statements till I narrowed down what exact line stops at. But I'm certainly not trying to waste your time, so if you don't have any further tips, I will log and wait for it to crash to see if I can learn anything.

              Comment


                #8
                Hello DerkWehler,

                From the error all we can tell is that a string was not in the right format, you would have to trace how your strategy works and what part of the logic is called when that error happens to know why. The error you are getting is a specific error but you also need to know what line of code you executed that had an error, that is why debugging is such a crucial part of developing C# code, to find runtime errors you need to use Prints to find the line having a problem so you know what is involved that may need corrected.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thanks again for your time. Another crash, and using Prints, appears narrowed it to this code. Does anything look wrong here?

                  Code:
                  if (State != State.Historical)
                      Print($" EST Post Time / Current Time : {sig.Est_time.ToString()} / {DateTime.Now.AddHours(-3).TimeOfDay.ToString("HH:mm:ss")}");
                  else
                      Print($" EST Post Time / Hist. Time : {sig.Est_time.ToString()} / {Time[0].AddHours(-3).TimeOfDay.ToString("HH:mm:ss")}");
                  It was live, not historical, so it's the first Print() call.​

                  Comment


                    #10
                    Hello DerkWehler,

                    You are using string interpolation so if the string being used is not in a correct format you would get that error. You would have to debug that code using regular print statements and just print out the values to see what strings you are using there. You'll need to narrow it down to 1 specific line and then figure out which variable is causing that to happen.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by nrgful, Today, 01:15 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post nrgful
                    by nrgful
                     
                    Started by nrgful, Today, 01:13 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post nrgful
                    by nrgful
                     
                    Started by Kraken29, Yesterday, 04:16 PM
                    2 responses
                    14 views
                    0 likes
                    Last Post Kraken29  
                    Started by shepherdangelique, Yesterday, 07:27 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post shepherdangelique  
                    Started by kvakana, Yesterday, 03:38 PM
                    1 response
                    18 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Working...
                    X