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

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
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by PaulMohn, Today, 09:12 AM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by ETFVoyageur, Today, 05:50 AM
    5 responses
    32 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by thumper57, 05-11-2024, 04:30 PM
    13 responses
    38 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by llanqui, Yesterday, 10:29 AM
    2 responses
    15 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Started by llanqui, Yesterday, 11:10 AM
    2 responses
    22 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Working...
    X