Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Removing drawing objects

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

    Removing drawing objects

    Hi

    Sorry if this is a stupid question (!)

    I'm building an indicator which draws a line following a move in price.

    Within OnBarUpdate I check if the price move meets my conditions and then draw the line from my calculated start point.
    Before I draw the line I check if there is any existing line from the same start point using the following code

    Code:
    for (int a3 = CurrentBar; a3 > (CurrentBar - SwingLowCandle_a); a3--) //remove previous lines drawn for the same impulse
                            {
                                if (DrawObjects[a3 + "line1"] != null)
                                {
                                    RemoveDrawObject(a3 + "line1");
                                    Print("Drawing Object " + a3 + "line1 removed.");
                                }
                            }
    However, with this I was occassionally getting an error similar to:

    Error on calling 'OnBarUpdate' method on bar 337: Collection was modified; enumeration operation may not execute.
    So I started looking for a solution on here, but couldn't quite find what I was looking for except that it's advised to use FOREACH rather than FOR statement.

    So I then tried just wrapping my original code in a foreach as below:

    Code:
    foreach (DrawingTool draw in DrawObjects.ToList())
                        {
                            for (int a3 = CurrentBar; a3 > (CurrentBar - SwingLowCandle_a); a3--) //remove previous lines drawn for the same impulse
                            {
                                if (DrawObjects[a3 + "line1"] != null)
                                {
                                    RemoveDrawObject(a3 + "line1");
                                    Print("Drawing Object " + a3 + "line1 removed.");
                                }
                            }
                        }
    Since making the change, it seems to work (chart looks good) and I've not had any errors.

    I just wanted to double check that my method is correct?

    Many thanks
    Tim

    #2
    Hello Tim,

    Thanks for your post.

    Yes, your process looks correct.You can review the options in the help guide here: https://ninjatrader.com/support/help...rawobjects.htm

    I'm not sure if this will help but so that you are aware, when you are drawing a line that has the same tag name as an already existing line (drawn by your indicator) you do not need to go through the removal process. The draw method will first automatically check for a draw object with the same tag name and if it matches, it will first remove the draw object before drawing the latest occurrence of the line. This is the default behavior of all draw methods. From the help guide on Draw.Line():

    Tag - A user defined unique id used to reference the draw object. For example, if you pass in a value of "myTag", each time this tag is used, the same draw object is modified. If unique tags are used each time, a new draw object will be created each time.tag Reference: https://ninjatrader.com/support/help...?draw_line.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    574 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    333 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    553 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    551 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X