Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error : sequence contains no elements

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

    Error : sequence contains no elements

    Hi,

    i am working on an indicator that uses dictionaries for each bar of each candle.

    i have a piece of code that checks the previous bar's dictionary values, it works properly :

    Code:
    if ( Low[0] < Low[1] )
    {
    qw = Low[1];
    while ( qw >= Low[0] )
    {
    if ( sa[0].ContainsKey(qw) ) sumsells0+= sa[0][qw];
    qw = qw - 0.25;
    cnts0 = cnts0 + 1;
    }
    
    qw2 = Low[1];
    int f = 0;
    while ( f <= cnts0 )
    {
    if ( sa[1].ContainsKey(qw2) ) sumsells1+= sa[1][qw2];
    f++;
    qw2 = qw2 + 0.25;
    
    }
    
    }
    
    if ( sumsells0 >= sumsells1 * 1.5 && GetCurrentBid(0) > Low[0] && Low[0] < Low[1] )
    Draw.Square(this, @"trs"+CurrentBar, false, 0, Low[0] , Brushes.Blue);
    
    else RemoveDrawObject("trs"+CurrentBar);
    I am trying to create another signal for my indicator, that also checks the previous bar's disctionary :

    Code:
        if ( CurrentBar > 6 )
                {
                    if ( IsFirstTickOfBar  && Cr[1] == 1)
                    {
                        
                        //   BULLISH STACKED IMBALANCE
                        
                        
                        
                        //Print(rt+ " > "+rt2);
                        //if ( rt == 0 || rt-rt2 < 1) return;
                        if ( sa[1].Keys.Min() > 0 && ba[1].Keys.Min() > 0)
                        {
                            
                            double rt = ba[1].Keys.Max();
                        double rt2 = ba[1].Keys.Min();
                        while ( rt >= (ba[1].Keys.Min())+0.75 )
                        {
                            if (
                                (
                                    ba[1].ContainsKey(rt)
                                    && (( !sa[1].ContainsKey(rt-0.25)) || (sa[1].ContainsKey(rt-0.25) && ba[1][rt]/sa[1][rt-0.25] >=2.5 ) )
                                )
                                &&
                                (
                                    ba[1].ContainsKey(rt-0.25)
                                    && (( !sa[1].ContainsKey(rt-0.5)) || (sa[1].ContainsKey(rt-0.5) && ba[1][rt-0.25]/sa[1][rt-0.5] >=2.5 ) )
                                )
                                &&
                                (
                                    ba[1].ContainsKey(rt-0.5)
                                    && (( !sa[1].ContainsKey(rt-0.75)) || (sa[1].ContainsKey(rt-0.75) && ba[1][rt-0.5]/sa[1][rt-0.75] >=2.5 ) )
                                )
                                )
                            {
                                Print("imb found on bar" + (CurrentBar-1));
                                // Draw.Rectangle(this, "BSI"+CurrentBar, false, 1, rt , 0, rt-0.75 , Brushes.Transparent, Brushes.Blue, 10);
                                break;
                            }
                            rt = rt-0.25;
                            
                        }
                        }
                        
                        
                        
                        
                        
                    }
                    
                    
                    
                    
                }​
    The problem is that, no matter what i do, with the second code I always get the error : Sequence contains no elements.

    if i disable the second sequence, the indicator works properly.
    Am i missing something?

    Thanks for your hlp


    Last edited by gyilaoliver; 03-25-2025, 08:35 AM.

    #2
    Hello gyilaoliver,

    Which specific line gives the error? The error you are seeing is a generic C# error saying the collection has no elements in it so either nothing has been added to that collection or the criteria used to search for the element yielded no results.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      in my code i added a line to check on every candle if the dictionary was created, also added a print showing "1" if it was created.

      I then added a print after these lines :

      Code:
      double rt = ba[1].Keys.Max();
      double rt2 = ba[1].Keys.Min();
      Print(rt+ " > "+rt2);​
      and after that, in the "if" function, i can not add prints, since it is not executable.

      In the output window i have the following information :

      the first number represents the CurrentBar, the "1" represents that the dictionary was created. Then the two numbers come from the print of my problematic code. "imb found on bar x " means the the code was working well.

      But given that i can not add prints to the "if" part, unfortunately i am unable to identify the line which generates the error.

      I did my best to check if any given key is present in the dictionary so it would not return another error.

      This is my output :


      4 1
      5 1
      6 1
      7 1
      20435 > 20431.25
      8 1
      20431.75 > 20430.25
      9 1
      20433.5 > 20429.5
      10 1
      20434.75 > 20431.5
      11 1
      20436 > 20435
      imb found on bar10
      12 1
      20437.25 > 20436
      13 1
      20438.5 > 20437.25
      imb found on bar12
      14 1
      20439.75 > 20438.5
      imb found on bar13
      15 1
      20440.25 > 20437
      16 1
      20438.75 > 20435
      17 1
      20438.5 > 20434.25
      18 1
      20439.75 > 20437.25
      19 1
      20441 > 20439.5
      imb found on bar18
      20 1
      20442.25 > 20440.25
      21 1
      20443.5 > 20441.75
      22 1
      20444.75 > 20442.75
      23 1
      20446 > 20441.25
      imb found on bar22
      24 1
      20444.75 > 20441.25
      25 1
      20446 > 20443
      26 1
      20446.25 > 20442.25
      27 1
      20446 > 20442.25
      imb found on bar26
      28 1
      20447.25 > 20445.75
      imb found on bar27
      29 1
      20448.5 > 20444.75
      30 1
      20447.25 > 20442.5
      31 1
      20448.5 > 20447.5
      imb found on bar30
      32 1
      20449.75 > 20447.25
      33 1
      20451 > 20447.75
      34 1
      20452.25 > 20449
      35 1
      20453.5 > 20452.25
      imb found on bar34
      36 1
      20454.25 > 20449.75
      37 1
      20453.5 > 20449.5
      imb found on bar36
      38 1
      20454.75 > 20453.25
      imb found on bar37
      39 1
      Indicator 'FootprintProba5': Error on calling 'OnBarUpdate' method on bar 39: Sequence contains no elements
      State: Terminated​

      Comment


        #4
        Hello gyilaoliver,

        The dictionary being created does not mean items were added to it. Thats just a requirement to have a dictionary for that bar. It looks like that is probably the Min and Max methods, those are linq expressions meaning they search the collection for an item that matches the criteria. Based on the error no items were found. You would have to make sure you added something to the dictionary first.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I check if the key is present in the dictionary for every single key value that i am working with. if any given key is not present, shouldn't the code ignore ( or return false ) the whole "if" function?
          i don't know how else to check and what to check...

          Code:
          while ( rt >= (ba[1].Keys.Min())+0.75 )
          {
          if (
          (
          ba[1].ContainsKey(rt)
          && (( !sa[1].ContainsKey(rt-0.25)) || (sa[1].ContainsKey(rt-0.25) && ba[1][rt]/sa[1][rt-0.25] >=2.5 ) )
          )
          &&
          (
          ba[1].ContainsKey(rt-0.25)
          && (( !sa[1].ContainsKey(rt-0.5)) || (sa[1].ContainsKey(rt-0.5) && ba[1][rt-0.25]/sa[1][rt-0.5] >=2.5 ) )
          )
          &&
          (
          ba[1].ContainsKey(rt-0.5)
          && (( !sa[1].ContainsKey(rt-0.75)) || (sa[1].ContainsKey(rt-0.75) && ba[1][rt-0.5]/sa[1][rt-0.75] >=2.5 ) )
          )
          )
          {
          Print("imb found on bar" + (CurrentBar-1));
          // Draw.Rectangle(this, "BSI"+CurrentBar, false, 1, rt , 0, rt-0.75 , Brushes.Transparent, Brushes.Blue, 10);
          break;
          }
          rt = rt-0.25;
          
          }

          Comment


            #6
            Just noticed somthing.

            maybe the problem comes from here :


            Code:
            double rt = ba[1].Keys.Max();
            double rt2 = ba[1].Keys.Min();
            if the dictionary has no values, this could return an error.
            But how can i check if a dictionay has values? i googled a lot but couldn't find a function​

            Comment


              #7
              Hello gyilaoliver,

              Checking if it contains a key is just a safeguard to not try and use a key that doesn't exist, that doesn't add anything to the dictionary.

              Yes the Min and Max methods will throw that error if no elements are found.

              JesseNinjaTrader Customer Service

              Comment


                #8
                Thank you Jesse. Is there any function that checks if a given dictionary contains at least one element?

                Comment


                  #9
                  Hello gyilaoliver,

                  You can do that however if you are looking for a specific key that is not really useful. You likely need to look earlier in your logic where you actually add values to the dictionary. The code you have shown is only using the dictionary.

                  For example this line while ( rt >= (ba[1].Keys.Min())+0.75 ) needs to come after you added at least 1 element to the dictionary, that loop and the Min will not work until you add 1 item. The code you have shown so far has no point where it adds anything, you are just using the dictionary in various ways.
                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by raysinred, Today, 10:32 AM
                  1 response
                  15 views
                  0 likes
                  Last Post rockmanx00  
                  Started by cbadr, Today, 08:19 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post cbadr
                  by cbadr
                   
                  Started by DayTradingDEMON, Today, 07:23 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post DayTradingDEMON  
                  Started by XXfea, Today, 07:01 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post XXfea
                  by XXfea
                   
                  Started by RedPothos, Today, 05:07 PM
                  0 responses
                  12 views
                  0 likes
                  Last Post RedPothos  
                  Working...
                  X