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 try to create a new indicator, but encountered the following error, I will copy my code and result below:

    protected override void OnBarUpdate()

    {

    if (CurrentBar < 30) return;

    else

    {

    Print(string.Format("{0} : {1}", Time[0], High[0]));

    double span= ((High[0]==Low[0])? 0.001: High[0]-Low[0]);

    Print(string.Format("{0} : {1} } ", Time[0],span.ToString("N4") ));

    spanline[0]=span;

    Print(string.Format("{0} : {1} : {2}: {3} } ", Time[0], High[0].ToString("N4"), Low[0].ToString("N4"),spanline[0].ToString("N4") ));

    }

    }

    Print Results:

    10/9/2023 6:07:45 PM : 4367.5

    Indicator 'XX: Error on calling 'OnBarUpdate' method on bar 30: Input string was not in a correct format.

    ​I added this indicator to ES chart in playback mode with 10/13 15 second data, I do not understand two issues:
    1) why the first print out come with 10/9 datestamp while I was using 10/13 ES data ( If I add other indicators , they will print out correct 10/13 datestamp for my other indicator)
    2) Is there anything wrong with span definition, what I want to achieve is non-zero difference between high and low

    Anyone can help?

    #2
    Hello plant,

    Thanks for your post.

    When testing the code you shared I see the same error occur. This is because you have an extra closing curly brace in the second and third print statements.

    Print(string.Format("{0} : {1} } ", Time[0],span.ToString("N4") ));

    Print(string.Format("{0} : {1} : {2}: {3} } ", Time[0], High[0].ToString("N4"), Low[0].ToString("N4"),spanline[0].ToString("N4") ));


    The code below should be used for prints instead of the code you shared. When testing the code below, I see the script running correctly without errors.

    Code:
    Print(string.Format("{0} : {1}", Time[0], High[0]));
    
    double span= ((High[0]==Low[0])? 0.001: High[0]-Low[0]);
    
    Print(string.Format("{0} : {1}", Time[0], span.ToString("N4")));
    
    ​spanline[0]=span;
    
    Print(string.Format("{0} : {1} : {2}: {3}", Time[0], High[0].ToString("N4"), Low[0].ToString("N4"),spanline[0].ToString("N4") ));
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,

      Thank you for fixing the issue for me. I continue to code, however encountered another error.

      protected override void OnBarUpdate()

      {

      if (CurrentBar < 30) return;

      else

      {

      Print(string.Format("{0} : {1}", Time[0], High[0]));

      double span= ((High[0]==Low[0])? 0.001:High[0]-Low[0]);

      Print(string.Format("{0} : {1}", Time[0],span.ToString("N4") ));

      spanline[0]=span;

      double emaspan_num=EMA(spanline, 9)[0] ;

      double emaspan_denom= EMA(EMA(spanline, 9),9)[0];

      double emaspan =emaspan_num/emaspan_denom;

      Print(string.Format("{0} : {1} : {2}: {3}", Time[0], emaspan_num, emaspan_denom,emaspan ));

      ​}

      }

      Bold fonts are the newly added

      Results:
      10/9/2023 6:07:45 PM : 4367.5

      10/9/2023 6:07:45 PM : 0.0010

      10/9/2023 6:07:45 PM : 0.0002 : 4E-05: 5

      Indicator 'XX': Error on calling 'OnBarUpdate' method on bar 30: Index was outside the bounds of the array.

      ​This time three prints works fine, but it also come with an outside the bounds error.

      Do you have any idea why it will be outside of bounds?

      Comment


        #4
        Hello plant,

        Thanks for your notes.

        This error message means an invalid index was used.

        When testing the code you shared on my end, I see the indicator working without any errors. See the attached image.

        With the error:
        "Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart"

        This message indicates an invalid index was used.

        As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error.

        The reason for this is because there will not be a bar ago; essentially there is not enough data at bar 0 look back one bar. The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling OnBarUpdate() for each bar.

        In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call a index 1 bar ago. This is especially important when multiple series are added to a single script as the data for all series may not start at the same time.

        Below is a link to the help guide on this specific example of an indexing error.
        https://ninjatrader.com/support/help...nough_bars.htm

        Also, below are links to the help guide on CurrentBar and CurrentBars.
        http://ninjatrader.com/support/helpG...currentbar.htm
        https://ninjatrader.com/support/help...urrentbars.htm

        Indexing errors can also occur when using an invalid index with arrays or collections.

        Below are public links to 3rd party educational sites on arrays and index out-of-range errors.


        Ultimately, you should add debugging prints to the script that print out CurrentBar, Time, and the index you are trying to access that is invalid to see how your logic is evaluating.​ The CurrentBar index must be equal to or greater than the index you are accessing.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121
        Attached Files
        Last edited by NinjaTrader_BrandonH; 10-15-2023, 05:19 PM.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hi Brandon,

          Thank you for your help. I successfully solved my problem. There are private variables missing definition which might caused the out of range error.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          647 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          369 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          108 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          572 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X