ObjectCreate issue...

 
Hi. Can anyone tell me why the below code does not place trade arrows correctly. The 'price' placement is fine, but it fails to always place it at the right bar, often placing arrows many bars in the past, not Bar 0, the current bar. btw ArrowNum is of Global Integer type.

      ArrowNum++;
      if (!IsTesting())
      {
       if (OrderType()==OP_BUY) {
        ObjectCreate("TradeArrow"+ArrowNum,OBJ_ARROW,0,Time[0],Bid);
        ObjectSet("TradeArrow"+ArrowNum,OBJPROP_ARROWCODE,1);
        ObjectSet("TradeArrow"+ArrowNum,OBJPROP_COLOR,Blue); }
       else if (OrderType()==OP_SELL) {
        ObjectCreate("TradeArrow"+ArrowNum,OBJ_ARROW,0,Time[0],Ask);
        ObjectSet("TradeArrow"+ArrowNum,OBJPROP_ARROWCODE,1);
        ObjectSet("TradeArrow"+ArrowNum,OBJPROP_COLOR,Red); }
      }




 
"but it fails to always place it at the right bar"

Does it do this on a new chart, first time you run it?

If no...

Does it do it on an existing chart, particularly after changing timeframe or changing some indicator setting?

If yes...

my guess is you are reusing ArrowNum but not deleting old arrows named with those numbers
 
"but it fails to always place it at the right bar"

Does it do this on a new chart, first time you run it?

If no...

Does it do it on an existing chart, particularly after changing timeframe or changing some indicator setting?

If yes...

my guess is you are reusing ArrowNum but not deleting old arrows named with those numbers






It always works fine initially on a new chart but then stops after awhile. I think you may be right - I included the following line and it seems to be behaving properly now. Many thanks!

while (ObjectFind("TradeArrow"+ArrowNum) > 0)
       ArrowNum++;