I am trying to remove rectangles that I drew and for some reason I cannot remove them successfully. The following is a code snippet similar to my actual code and the output it generates. What am I doing wrong? Thank you.
public class RemoveDrawObjectTest : Indicator { public class RectRegion { public double high; public double low; public string tag; } public List<RectRegion> rect_l; SessionIterator sessionIterator; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "RemoveDrawObjectTest"; Calculate = Calculate.OnBarClose; IsOverlay = false; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; } else if (State == State.Configure) { rect_l = new List<RectRegion>(); } else if (State == State.Historical) { sessionIterator = new SessionIterator(Bars); } } protected override void OnBarUpdate() { if (Bars.IsFirstBarOfSession) sessionIterator.GetNextSession(Time[0], true); if (CurrentBar > 5) return; // define some region RectRegion reg = new RectRegion(); reg.high = GetCurrentAsk(); reg.low = reg.high - 3 * TickSize; reg.tag = CurrentBar.ToString(); // put it in the list rect_l.Insert(0, reg); // Draw the region Draw.Rectangle(this, reg.tag, Time[0], reg.low, sessionIterator.ActualSessionEnd, reg.high, true, "Default"); // remove old elements int n = rect_l.Count; if (n > 3) { RemoveDrawObject(rect_l[n - 1].tag); Print("rect_l.Count: " + n + " DrawDrawObjects.Count: " + DrawObjects.Count + " tag: " + rect_l[n - 1].tag); foreach (var item in DrawObjects) Print(DrawObjects[item.Tag].Tag); rect_l.RemoveAt(n - 1); } } }
rect_l.Count: 4 DrawDrawObjects.Count: 4 tag: 0
@0
@1
@2
@3
rect_l.Count: 4 DrawDrawObjects.Count: 5 tag: 1
@0
@1
@2
@3
@4
rect_l.Count: 4 DrawDrawObjects.Count: 6 tag: 2
@0
@1
@2
@3
@4
@5
Comment