Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't remove script drawn object using tag.

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

    Can't remove script drawn object using tag.

    I have an indicator that draws trendlines and labels them with prefixes (PTL_ / VTL_).

    When the trendline was broken, I had a function that deleted drawn objects if they contained PTL_or VTL_.

    The last time I used it successfully was 2 years ago and now when I use it, the Rays aren't being deleted. I took a look in visual studio and saw that even though there are 17 drawn objects by bar#77, DrawObjects.Count = 0.

    Below is the code that used to work as well as 2 screenshots for reference. Is there a different way to delete DrawObjects?
    Thanks!

    Code:
    public void DeleteAllFromChartByTagContains(string text)
    {
    if (CurrentBar == 77)
    {
    Print("Stopping at: " + CurrentBar);
    }
    
    List<DrawingTool> itemsToRemove = new List<DrawingTool>();
    
    foreach (DrawingTool dt in DrawObjects.ToList())
    {
    if (dt.Tag.Contains(text))
    {
    itemsToRemove.Add(dt);
    }
    }
    foreach (DrawingTool dt in itemsToRemove)
    {
    string toRemove = "";
    if (dt.Tag.Contains(text))
    {
    toRemove = dt.Tag;
    RemoveDrawObject(toRemove);
    }
    }
    }
    Click image for larger version  Name:	RemoveDrawObject.jpg Views:	0 Size:	108.2 KB ID:	1251880Click image for larger version  Name:	RemoveDrawObjectMissing.jpg Views:	0 Size:	150.0 KB ID:	1251881
    Attached Files

    #2
    Hello, thanks for writing in. There is no other way to remove draw objects from code. My test script is working if you would like to test this out:
    Code:
            int drawnbar = -1;
            bool drawn = false;
            protected override void OnBarUpdate()
            {
                if(State == State.Historical)
                    return;
    
                if(!drawn)
                {
                    Draw.HorizontalLine(this, "Remove This Line", High[0], Brushes.Red);
                    drawn = true;
                    drawnbar = CurrentBar;
                }
    
                if(drawn && CurrentBar == drawnbar+1)
                {
                    RemoveDrawObject("Remove This Line");
                }
    
    
            }​
    Your script is likely just not providing the correct tag string in the RemoveDrawObject method. Use Print() to confirm you are supplying the correct method.

    Comment


      #3
      Hi Chris,

      Thanks for the quick reply.

      I can't even check if my removal via tag works because even though there are 17 drawobjects by bar 77, the drawobject.count is 0!

      I amended my code to print the count of draw objects right after they are called and it always says 0. Doesn't matter if they were Rays, Diamonds, or Triangles. They don't show up in DrawObjects.

      I double-clicked on one of the Rays and it shows that it's attached to my strategy.

      AAAhhhhhh!

      I noticed that your example would return if the state was historical. I just tried running my indicator on a blank chart set to Simulated Data Feed and it worked!

      The trendlines were created and deleted as they were broken.

      In the screenshots, I've been trying to verify that objects were removed by looking at historical data that met my conditions.

      So, it seems that in the last few years, DrawObjects was changed to not hold anything drawn on historical data?

      This is a problem because trendlines can persist for a long time and I'd like to see them even though NT wasn't running. I'd also not like to have 5 days of historically drawn trendlines persist into the current session.

      My final question is this: How can I access historically drawn objects selectively? Since they're on the chart, they must exist somewhere. Which class besides DrawObjects has them?

      Click image for larger version

Name:	NoDrawObjects.jpg
Views:	237
Size:	241.8 KB
ID:	1252024Click image for larger version

Name:	RayCode.jpg
Views:	185
Size:	43.7 KB
ID:	1252025

      Comment


        #4
        Hello, thanks for the follow up. The DrawObjects does hold historically drawn objects. This can be proven with a test:

        Code:
                int drawnbar = -1;
                bool drawn = false;
                protected override void OnBarUpdate()
                {
                    if(!drawn) //Draw a horizontal line on the first bar on the chart.
                    {
                        Draw.HorizontalLine(this, "Remove This Line", High[0], Brushes.Red);
                        drawn = true;
                        drawnbar = CurrentBar;
                    }
        
                    if(drawn && CurrentBar == BarsArray[0].Count-5)
                    {
                        Print("The line from historical " + DrawObjects["Remove This Line"] + " " + Time[0]);
                    }
        
                    if(drawn && State == State.Realtime)
                    {
                        Print(DrawObjects["Remove This Line"]);
                    }
        
        
                }​

        Comment


          #5
          Hi Chris,

          I'm sorry I'm not being very clear. I've come up with the simplest example to demonstrate the problem.

          This strategy draws a diamond and an arrow on each of the first 20 bars of a chart. After each drawing, I Print DrawObjects.Count which is somehow always 0.

          After drawing the 20 diamonds and 20 arrows, I remove the 10th diamond using it's literal tag: RemoveDrawObject("DIA_Diamond 10"); This works as you've shown.

          I then try to remove any draw object that contains "DIA_" and I cannot because drawobjects is 0!

          So, my final 2 questions are:
          1. Why is drawobject.count always 0 for historically drawn objects.

          2. How can I programatically iterate through historical drawobjects and remove any that contain a certain string? I've attempted to do that in DeleteAllFromChartByTagContains but DrawObjects.ToList is always empty.

          Thanks again!

          Code:
          public void DeleteAllFromChartByTagContains(string text)
          {
          List<DrawingTool> itemsToRemove = new List<DrawingTool>();
          
          foreach (DrawingTool dt in DrawObjects.ToList())
          {
          if (dt.Tag.Contains(text))
          {
          itemsToRemove.Add(dt);
          }
          }
          foreach (DrawingTool dt in itemsToRemove)
          {
          if (dt.Tag.Contains(text))
          {
          RemoveDrawObject(dt.Tag);
          }
          }
          }
          
          string diamondPrefix = "DIA_";
          
          protected override void OnBarUpdate()
          {
          if(CurrentBar < 20)
          {
          Draw.Diamond(this, diamondPrefix + "Diamond " + CurrentBar, true, 0, High[0] + .5, Brushes.Red);
          Draw.ArrowUp(this, "Arrow " + CurrentBar, true, Time[0], High[0] + 1, Brushes.White);
          
          Print("Number of DrawObjects: " + DrawObjects.Count);
          }
          if (CurrentBar == 21)
          {
          RemoveDrawObject("DIA_Diamond 10");
          
          DeleteAllFromChartByTagContains(diamondPrefix);
          
          Print("Number of DrawObjects: " + DrawObjects.Count);
          
          }
          }


          Click image for larger version

Name:	Problem.jpg
Views:	238
Size:	261.8 KB
ID:	1252157

          Comment


            #6
            You do not appear to have a clean test. Pasting in your exact code and running it on a clean chart with nothing else interfering, one sees this, just as expected:

            Click image for larger version

Name:	image.png
Views:	180
Size:	11.5 KB
ID:	1252168

            You need to close all your workspaces, make a new, clean workspace, and a new, clean indicator, and a new, clean chart, and into the new, clean indicator paste ONLY the code you have posted to the forum. Once you understand your starting point, you can start reintroducing some of your more complex scenario slowly and piece by piece until you understand what you have done wrong.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              Also, and this is not why it fails, but... in your loop removing the drawing objects, you don't have to check the tag again. You've already done that.
              Code:
              foreach (DrawingTool dt in itemsToRemove)
                          {
                              RemoveDrawObject(dt.Tag);
                          }​
              Bruce DeVault
              QuantKey Trading Vendor Services
              NinjaTrader Ecosystem Vendor - QuantKey

              Comment


                #8
                Hi Bruce,

                Thank you for your help in testing this. I really appreciate it!

                It's been so long since I've touched this code that I realized it was originally a STRATEGY, not an Indicator. My bad! I'm super rusty and confused the terms when creating the original post.

                But a problem remains: You're right that it works as an Indicator...I just tried it. But it does NOT work when tested in the Strategy Analyzer.

                This thread can be deleted due to my stupidity and I'll start a new one in the correct forum section for strategy development here:
                I'm having a problem trying to delete drawobjects using a partial tag when they are drawn historically within Strategy Analyzer. I cannot iterate through DrawObjects because it always seems to equal zero or is empty. I CAN delete a specific historically drawn object if I know its exact tag but I'm trying to mass delete

                Comment

                Latest Posts

                Collapse

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