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

Input string was not in a correct format with a List (double to String)

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

    Input string was not in a correct format with a List (double to String)

    I have some loop results I need to use in a list outside the loop.

    Here is the loop only without the List which prints the results all right

    PHP Code:
    double sum 0;
    double mean 0;
    double ratio 0;

    double sum1 0;
    double mean1 0;
    double ratio1 0;

    for (
    int index 0index 10index++)
    {

         
    sum += (Math.Abs(Crude.MasterInstrument.RoundToTickSize(Closes[1][index] - SMA(BarsArray[1], 20)[index])) / Crude.MasterInstrument.TickSize);

         
    sum1 += (Math.Abs(YEN.MasterInstrument.RoundToTickSize(Closes[2][index] - SMA(BarsArray[2], 20)[index])) / YEN.MasterInstrument.TickSize);

    }

         
    mean sum 10;

         
    ratio = (Math.Abs(Crude.MasterInstrument.RoundToTickSize(CrudeSeries[0] - SMA(BarsArray[1], 20)[0])) / Crude.MasterInstrument.TickSize) / mean;

         Print(
    "ratio : " ratio);

         
    mean1 sum1 10;

         
    ratio1 = (Math.Abs(YEN.MasterInstrument.RoundToTickSize(YENSeries[0] - SMA(BarsArray[2], 20)[0])) / YEN.MasterInstrument.TickSize) / mean;

         Print(
    "ratio1 : " ratio1); 

    With successful prints the ratio and ratio1 values

    ratio : 0.844155844155844
    ratio1 : 2.43243243243243

    Here's my list added to the loop (outside after the loop)

    PHP Code:

    double sum 
    0;
    double mean 0;
    double ratio 0;

    double sum1 0;
    double mean1 0;
    double ratio1 0;

    for (
    int index 0index 10index++)
    {

         
    sum += (Math.Abs(Crude.MasterInstrument.RoundToTickSize(Closes[1][index] - SMA(BarsArray[1], 20)[index])) / Crude.MasterInstrument.TickSize);

         
    sum1 += (Math.Abs(YEN.MasterInstrument.RoundToTickSize(Closes[2][index] - SMA(BarsArray[2], 20)[index])) / YEN.MasterInstrument.TickSize);

    }

         
    mean sum 10;

         
    ratio = (Math.Abs(Crude.MasterInstrument.RoundToTickSize(CrudeSeries[0] - SMA(BarsArray[1], 20)[0])) / Crude.MasterInstrument.TickSize) / mean;

         Print(
    "ratio : " ratio);


         
    mean1 sum1 10;

         
    ratio1 = (Math.Abs(YEN.MasterInstrument.RoundToTickSize(YENSeries[0] - SMA(BarsArray[2], 20)[0])) / YEN.MasterInstrument.TickSize) / mean;

         Print(
    "ratio1 : " ratio1);


    List<
    stringresultsToList = new List<string> {
         
    ratio.ToString(),
         
    ratio1.ToString(),
    }; 

    Which does print the ratio and ratio1 values, but then it also throws this Input string was not in a correct format error

    ratio : 1.42857142857143
    ratio1 : 0.615384615384615
    Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 5262: Input string was not in a correct format.
    Why does it print all right without the list, but doesn't work with the list?

    What does it mean "Input string was not in a correct format?" How to check for the format? Isn't ratio.ToString() and ratio1.ToString() the correct format (i.e. from double to string)?

    How can I fix it? Thanks!

    A new related issue to my previous one
    NinjaTrader_ChelseaB
    Last edited by PaulMohn; 04-28-2022, 10:59 AM.

    #2
    Hello PaulMohn,

    If you see a print that means that line of code ran completely and output to the output window, the problem would be after the last print. We can see that ratio1 was successful and printed so your format error is after that.

    Why does it print all right without the list, but doesn't work with the list?
    That would be the problem if it works without that code. The format of your strings that you are trying to create the list with is not correct in some way.

    You could figure out what string that is by trying to input 1 of the two strings and see which throws the error.

    What does it mean "Input string was not in a correct format?" How to check for the format? Isn't ratio.ToString() and ratio1.ToString() the correct format (i.e. from double to string)?
    This is a C# error, for this type of error you would get a better explanation by researching this error in external C# related websites. This is not a NinjaScript error but a problem with the format of the string and how you used the list with that string.

    I would suggest trying to make the list and then add each element to it like the following:

    Code:
    List<string> resultsToList = new List<string>();
    
    resultsToList.Add(ratio.ToString());
    If that throws an error then that lets you know which line is having an issue with the format.

    You may also want to just avoid using a string at all here, you could just use a double:

    Code:
    List<double> resultsToList = new List<double>();
    
    resultsToList.Add(ratio);
    If you wanted to make that a string you could do that wherever you use the item from the list:

    Code:
    Print(resultsToList[0]);
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      Many thanks for the great explanation and very helpful solution points!

      You pointed very well it was about a following snippet.

      I had overlooked the data type int instead of double in a following sorting String List, Loop and Linq function

      I had it in int

      PHP Code:
      // Sort the Strings List By the Digits
      var pairedList = new List<Tuple<intstring>>();

      foreach (var 
      item in resultsToList)
      {
           var 
      items item.Split(':');
           
      pairedList.Add(new Tuple<intstring>(int.Parse(items[0].Trim()), items[1]));
      }

      var 
      result pairedList.OrderByDescending(=> x.Item1).Select(=> x.Item1 ":" x.Item2).ToList(); 

      I corrected to double

      PHP Code:
      // Sort the Strings List By the Digits
      var pairedList = new List<Tuple<doublestring>>();

      foreach (var 
      item in resultsToList)
      {
           var 
      items item.Split(':');
           
      pairedList.Add(new Tuple<doublestring>(double.Parse(items[0].Trim()), items[1]));
      }

      var 
      result pairedList.OrderByDescending(=> x.Item1).Select(=> x.Item1 ":" x.Item2).ToList(); 

      Now it works all right.
      Thanks again!


      Just a note about an indexing error with the .Add() to list method.

      PHP Code:
      List<stringresultsToList= new List<string>();

      resultsToList.Add(ratio.ToString()); 

      I tried using your .Add() to list method but for some reason it returned this error Index was outside of the bounds of the array.

      Time Category Message
      29/04/2022 17:28:51 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 6567: Index was outside the bounds of the array.
      But this error is not thrown with the regular list with elements enclosed within the curly brackets

      That prints the ratios all right
      PHP Code:
      List<stringresultsToList = new List<string> {
           
      ratio.ToString(),
           
      ratio1.ToString(),
      }; 

      Why doesn't the .Add() to list method work while the regular list with elements enclosed within the curly brackets does work in this case? Thanks for your insights!
      Last edited by PaulMohn; 04-29-2022, 09:51 AM.

      Comment


        #4
        Hello PaulMohn,

        From the given details I wouldn't be sure why you would get an indexing error using Add on a list, that would generally be for accessing an element where you see an index error.

        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by llanqui, Yesterday, 03:51 PM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by kujista, Yesterday, 12:39 AM
        5 responses
        19 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by nleitman, Yesterday, 11:46 AM
        9 responses
        28 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
        18 responses
        106 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Mindset, Yesterday, 06:19 AM
        2 responses
        15 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X