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

delete IDrawObjects?

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

    delete IDrawObjects?

    I have a set of rays that I want to redraw at each bar update. I´ve red THIS on IDrawObjects and got this far:

    foreach (IDrawObject ray in DrawObjects)
    {
    if (ray.Tag.StartsWith(ID))
    {
    "here I´d like to delete"
    }
    }

    As the code implies, the rays are tagged with a common ID and an individual loop index. Since the amount of rays varies from each bar update, I can´t simply overwrite with the same tag.

    Simply put; how do I delete IDrawObjects?

    Kindly, Fredrik
    Last edited by FREEN; 09-18-2011, 02:20 PM.

    #2
    Fredrik, please see this page for how to remove draw objects - http://www.ninjatrader.com/support/h...drawobject.htm.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks Austin. This works fine but gives me an error used inside a foreach loop:

      "Error on calling 'OnBarUpdate' method for indicator 'ARBfourierSeries110917' on bar 83: Collection was modified; enumeration operation may not execute."

      Code used:

      foreach (IDrawObject ray in DrawObjects)
      {
      if (ray.Tag.StartsWith("tagRay"))
      {
      Print(ray.Tag.ToString());
      Print("----------------------");
      RemoveDrawObject("tagRay1");
      }
      }

      Where do I go wrong? Any suggestion on how I can use the "RemoveDrawObject" method in a loop?

      //Fredrik

      Comment


        #4
        Originally posted by FREEN View Post
        Thanks Austin. This works fine but gives me an error used inside a foreach loop:

        "Error on calling 'OnBarUpdate' method for indicator 'ARBfourierSeries110917' on bar 83: Collection was modified; enumeration operation may not execute."

        Code used:

        foreach (IDrawObject ray in DrawObjects)
        {
        if (ray.Tag.StartsWith("tagRay"))
        {
        Print(ray.Tag.ToString());
        Print("----------------------");
        RemoveDrawObject("tagRay1");
        }
        }

        Where do I go wrong? Any suggestion on how I can use the "RemoveDrawObject" method in a loop?

        //Fredrik
        When using foreach, you cannot modify the collection. That is standard C#.

        Instead, list the tags of the objects that you want to delete into an ArrayList, and then use a for loop to iterate through and delete the items that are listed in that ArrayList.

        Comment


          #5
          Thanks koganam. Ok, the foreach loop is read only. The tutorial examples confuses me though since they reset properties of IDrawObjects in foreach loops, as in the code bellow. Maybe there´s a difference in resetting and deleting here?! On the other hand deleting objects in an array isn´t standard C# either as far as I know. I´ll play around with you idea and some other loop constructs, thanks!

          // Loops through the DrawObjects collection
          foreach (IDrawObject draw in DrawObjects)
          {
          // Unlocks all draw objects for manual manipulation on the chart
          draw.Locked = false;
          }
          Last edited by FREEN; 09-19-2011, 06:15 AM.

          Comment


            #6
            Freen, there is a difference. In pseudo-code, you'll need to do something like this:
            Code:
            ArrayList objectsToDelete = new ArrayList();
            if (check to delete idrawobjects = true)
            {
                foreach (IDrawObject ray in DrawObjects)
                {
                    if (ray.Tag.StartsWith("tagRay"))
                    {
                        objectsToDelete.Add(ray)
                    }
                }
                
                foreach (IDrawObject obj in objectsToDelete)
                {
                    RemoveDrawObject(obj.Tag);
                }
            }
            AustinNinjaTrader Customer Service

            Comment


              #7
              Originally posted by FREEN View Post
              Thanks koganam. Ok, the foreach loop is read only. The tutorial examples confuses me though since they reset properties of IDrawObjects in foreach loops, as in the code bellow. Maybe there´s a difference in resetting and deleting here?! On the other hand deleting objects in an array isn´t standard C# either as far as I know. I´ll play around with you idea and some other loop constructs, thanks!

              // Loops through the DrawObjects collection
              foreach (IDrawObject draw in DrawObjects)
              {
              // Unlocks all draw objects for manual manipulation on the chart
              draw.Locked = false;
              }
              First of all, C# provides methods for removing items from an ArrayList or Array. However, in this case, you will not be deleting the items in the ArrayList: the ArrayList is just a table of things to delete; it is never modified.

              In effect:
              1. Create a list of objects to be deleted
              2. Read the list and delete the corresponding object in a collection space. The list remains the same: nothing is deleted; the list is just something you are reading, not modifying.
              3. Once you have gone through the list, remove the list from memory.

              Yes, modifying the properties of objects in a collection is not the same as modifying the collection itself. It is a rather subtle difference, I agree.

              Comment


                #8
                Code:
                ArrayList objectsToDelete = new ArrayList();
                    foreach (IDrawObject ray in DrawObjects)
                    {
                        if (ray.Tag.StartsWith("tagRay"))
                        {
                            objectsToDelete.Add(ray)
                        }
                    }
                 
                    foreach (IDrawObject obj in objectsToDelete)
                    {
                        RemoveDrawObject(obj.Tag);
                    }

                Worked like a charm!

                In case more newbies read this, here´s some extras I learned from this:

                Pseudocode: "... intended for human reading rather than machine reading."

                Deleting in arrays: Arrays have a code defined number of "buckets". The contents can be changed/cleared, but the "bucket" itself can not... at least not without resizing in code. ArrayLists have a dynamic amount of "buckets" and don´t need explicit resizing.

                Thanks Austin and koganam!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by wzgy0920, 04-20-2024, 06:09 PM
                2 responses
                26 views
                0 likes
                Last Post wzgy0920  
                Started by wzgy0920, 02-22-2024, 01:11 AM
                5 responses
                32 views
                0 likes
                Last Post wzgy0920  
                Started by wzgy0920, Yesterday, 09:53 PM
                2 responses
                49 views
                0 likes
                Last Post wzgy0920  
                Started by Kensonprib, 04-28-2021, 10:11 AM
                5 responses
                192 views
                0 likes
                Last Post Hasadafa  
                Started by GussJ, 03-04-2020, 03:11 PM
                11 responses
                3,235 views
                0 likes
                Last Post xiinteractive  
                Working...
                X