Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot delete historically drawn objects by Tag.Contains

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

    Cannot delete historically drawn objects by Tag.Contains

    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 objects if their tag CONTAINS a string.

    To illustrate the problem simply, please do the following:

    1. Create a new strategy and immediately press 'Generate.'
    2. Copy the below code into it and save and compile.
    3. Open Strategy Analyzer based on that new strategy with the following parameter:
    • Instrument: MES 06-23 (Though I don't think this matters)
    • Price based on: Last
    • Type: Volume
    • Value 200
    • Start and End date: 05/19/2023
    • Everything else is default value
    4. Open a NinjaScript Output window
    5. Press 'Run'
    6. Change the Strategy Analyzer display from SUMMARY to CHART and scroll the chart to the far left to see the drawn objects.

    Does it look similar to my screenshot? No functioning drawobject.count and all the diamonds remain except for the one successfully removed "DIA_Diamond 10"?

    I hope you have the same results as I do. If so, what's going on? If not...what am I doing wrong?

    How can I delete historically drawn objects when their tag contains a certain string?

    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)
                {
                    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:	Strategy3.jpg Views:	0 Size:	235.8 KB ID:	1252371
    Last edited by hillborne; 05-22-2023, 05:35 AM.

    #2
    See also: https://forum.ninjatrader.com/forum/...ject-using-tag

    This post is the same issue, but specific to strategy analyzer.

    I suspect this is because strategy analyzer's charts do not support all these methods the same way regular charts do. To use the full suite of drawing object properties and methods you may need to run it on a chart. I think the purpose of Strategy Analyzer's charts is somewhat restricted to showing the trade executions unlike regular charts, but NinjaTrader support can confirm the details of this.

    This is both a simplifying assumption and a performance optimization - typically strategy analyzer strategies run "headless" so as to save resources - you wouldn't want to run 10,000 iterations of some optimization and to have it remembering the location of every trend line drawn... all that is typically wanted are the trades and the trade statistics. It sounds like you in particular want it to do all the drawing object functions though that is uncommon. NinjaTrader Support can confirm if this is possible by setting a property to turn on this functionality within Strategy Analyzer.

    All of that having been said, it seems inconsistent that it lets you draw them but you cannot iterate them. NinjaTrader Support or engineering will need to advise exactly where the line has been drawn here.
    Last edited by QuantKey_Bruce; 05-22-2023, 05:05 AM.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Bruce, you are right that it's only on Strategy Analyzer. I set it up on a chart with historical data and turned on live data and it worked like when it was an Indicator.

      The reason I want to use Strategy Analyzer with drawn objects is because a lot of my trades are based on trendlines, trend channels, regressions, etc. So, in order to confirm that my logic is working, I need to see the drawn object and where a trade should take place. But also be able to delete some otherwise I end up with a laggy screen full of hundreds of lines. I have parameters in my strategy to turn on or off drawing so backtesting is still efficient when I don't need to see what's drawn.

      Thank you sooo much for confirming that you cannot iterate the drawobjects as well! My coding skills have atrophied so much I was beginning to lose hope.

      Yes, I hope NT Support can point the way to where the drawobjects actually live in a Strategy Analyzer window.
      Last edited by hillborne; 05-22-2023, 05:38 AM.

      Comment


        #4
        Hello hillborne,

        Thank you for your post.

        The DrawObjects collection is only available from a chart as the owner of the NinjaScript. If you are running a chart in the Strategy Analyzer, then the Strategy Analyzer owns the script and would not be able to take advantage of the DrawObjects collection. It does not have access to the chart display while the script is being run. For more details on the DrawObjects collection:


        What I suggest is to create your own list of tags for each object you draw. Then, you could loop through that list to get a specific tag (you can still use .Contains in your loop) rather than looping through the DrawObjects that won't be available to the Strategy Analyzer. Each time an object is removed via RemoveDrawObject() or RemoveDrawObjects(), you simply need to also remove it from the list (or clear the list if all objects are removed).

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

        Comment


          #5
          Hi Emily,

          Thank you for the confirmation and suggestion on a workaround.

          It'd be great if the documentation you linked could contain a note that: DrawObjects is not available for iteration/access within Strategy Analyzer.

          Thanks!

          Comment


            #6
            I actually didn't realize they even allowed you to draw them on the Strategy Analyzer chart, so it does more than I thought it did. It seems they allow you to draw them - they just don't keep track of them after the fact. I agree it would be good for this to be documented, probably in the list of notes at the top of this page: https://ninjatrader.com/support/help...rawobjects.htm
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              Thank you both for your feedback. I will submit your suggestions for additional documentation as help guide edits for future reference.

              Please don't hesitate to reach out with any future NinjaTrader items we may assist you with.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              600 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              347 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
              558 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X