Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"Collection was modified; enumeration operation may not execute"

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

    "Collection was modified; enumeration operation may not execute"

    Once in a while I get "Collection was modified; enumeration operation may not execute" with that piece of code I think.
    It should read out the levels from a user drawn fibonacci retracement and add them to a list.

    Code:
                if (IsFirstTickOfBar && DrawObjects["MyFib"] as DrawingTools.FibonacciRetracements != null)
                {
                    dynamic temp = DrawObjects["MyFib"];
                    if (temp.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.FibonacciRetracements"))
                    {
                        foreach (PriceLevel p in temp.PriceLevels)
                        {
                            if (Time[0]>temp.EndAnchor.Time && !(levels.ContainsKey("FibLevel"+p.Value.ToString(""))))
                                levels.Add("FibLevel"+p.Value.ToString(""),p.GetPrice(temp.StartAnchor.Price, temp.EndAnchor.Price - temp.StartAnchor.Price, true));
                        }
                    }
                }
    Any ideas how can I change the code to prevent that?

    Many thanks

    #2
    Hello td_910,

    Thank you for your post.

    I believe the typical cause of this message has to do with attempting to modify a collection while it is being looped through. Please add Print() statements inside of each condition from this code snippet to further narrow down the offending line of code. For more details on using prints for debugging:


    Are there particular steps you are taking that result in this error being thrown? For example, does it happen when manually modifying the user-drawn fib object? It is typically better to loop through the DrawObjects collection in a thread-safe way as demonstrated in the snippet at the bottom of this page:

    Code:
      // Loops through the DrawObjects collection via a threadsafe list copy
      foreach (DrawingTool draw in DrawObjects.ToList())​
    Even working with DrawObjects.ToList() could likely prevent this error from happening.

    Please let us know if we may be of further assistance.

    Comment


      #3
      Originally posted by td_910 View Post
      Any ideas how can I change the code to prevent that?
      Maybe you could lock the collection?

      An example is here.

      It's possible something else is modifying the collection,
      after all, NinjaTrader is multi-threaded -- thus the above
      example locks Account.Positions even for a simple loop
      that does nothing but print the positions.

      If feasible1, I'd tend to suggest lock over ToList.

      Presumably, ToList does lots of extra work in making
      an independent copy of the collection.

      Just my 2˘.



      1. Remember, lock is a cooperative thread thing and
      requires the internal NT code to participate in using it.
      That is, Internally, NinjaTrader code may (or may not)
      be using lock for the DrawObjects, so even though
      you are using it (which prevents other instances of your
      indicator from entering the loop in their instance until
      your instance is finished) it's possible to still have
      problems if other internal parts of NT do not use the
      same lock. That may be why NT help guide suggests
      the ToList approach -- because under the hood the
      NT engrs did not use lock when adding/removing items
      from the DrawObjects collection.

      Comment


        #4
        Originally posted by NinjaTrader_Emily View Post
        Hello td_910,

        Thank you for your post.

        I believe the typical cause of this message has to do with attempting to modify a collection while it is being looped through. Please add Print() statements inside of each condition from this code snippet to further narrow down the offending line of code. For more details on using prints for debugging:
        https://ninjatrader.com/support/foru...ing#post791121

        Are there particular steps you are taking that result in this error being thrown? For example, does it happen when manually modifying the user-drawn fib object? It is typically better to loop through the DrawObjects collection in a thread-safe way as demonstrated in the snippet at the bottom of this page:
        https://ninjatrader.com/support/help...rawobjects.htm
        Code:
         // Loops through the DrawObjects collection via a threadsafe list copy
        foreach (DrawingTool draw in DrawObjects.ToList())​
        Even working with DrawObjects.ToList() could likely prevent this error from happening.

        Please let us know if we may be of further assistance.
        Thanks for your reply.
        Since I know the tag of the draw object and I'm sure it's properties aren't modified by the user or another indicator, can't I just use one of these three methods:

        Code:
        protected override void OnBarUpdate()
        {
          if (DrawObjects["someTag"] != null && DrawObjects["someTag"] is DrawingTools.Line)
          {
            // Do something with the drawing tool line
          }        
        
          // An alternative approach to find the draw object by a tag
          if (DrawObjects["someTag"] as DrawingTools.Line != null)
          {
            // Do something drawing tool line
          }  
        
          // Yet another way to find a drawing tool by a tag
          if (DrawObjects["someTag"].GetType().Name == "Line")
          {
            // Do something drawing tool line  
          }
        }​

        Comment


          #5
          Hello td_910,

          The issue is with looping through the collection.

          As long as you are using the specific tag and not looping through the collection, this would be fine to do.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          599 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          344 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          558 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          557 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X