DeleteStragglerArrows

 

The code below is my attempt to delete all arrow objects on the chart which do not touch a price bar. When I comment out ..., the script accurately Prints all objects which do and do not match the criteria as expected. However, when I uncomment ObjectDelete, the arrow objects which should get deleted (e.g. the stragglers which do not touch a price bar) do not all get deleted. Rather, I need to click the script several times.

The screen shot below illustrates what I'm trying to accomplish. The first screen shot is with ObjectDelete(ObjectName(i)); commented out as seen in the code excerpt below. Notice how all stragglers get printed to screen correctly. The second screen is what I get after running the script with ObjectDelete... uncommented. Not all corresponding objects get deleted.

Please help me quickly delete all straggler arrows!

   double   ArrowPrice, ArrowBarHigh, ArrowBarLow;
   bool     StragglerArrow;
   int      i;
 
   for(i=0;i<ObjectsTotal();i++)
   {
   ArrowPrice = ObjectGet(ObjectName(i),OBJPROP_PRICE1);
   ArrowBarHigh = High[iBarShift(NULL,0,ObjectGet(ObjectName(i),OBJPROP_TIME1))];
   ArrowBarLow = Low[iBarShift(NULL,0,ObjectGet(ObjectName(i),OBJPROP_TIME1))];
   StragglerArrow = ArrowPrice > ArrowBarHigh || ArrowPrice < ArrowBarLow;
      if(StragglerArrow && ObjectType(ObjectName(i)) == OBJ_ARROW)
      {
      Print("ArrowPrice: ",ArrowPrice," deleted");
      //ObjectDelete(ObjectName(i));
      }
      else
      Print("ObjectPrice: ",ArrowPrice," not deleted");    
   }



Second chart result with ObjectDelete code uncommented. Note that 2 out of the 3 objects actually get deleted.

 
1. Try to analyze error code (GetLastError function)

2. Set ObjectsRedraw() as final line
 
stringo wrote:
1. Try to analyze error code (GetLastError function)

2. Set ObjectsRedraw() as final line

Tried both. No errors reported. ObjectsRedraw() doesn't help.

Any other ideas? Still struggling to make sense of handling objects here.