The incorrect output position of the text object created

 
Dear all,

  I used the following code in my EA to create a text object at a position higher than the current bar's High.

#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

bool IsNewBarbyTime(){
  static datetime lastbar;
  datetime curbar = Time[0];
  if(lastbar!=curbar){
    lastbar=curbar;
    return (true);
  }
  else return(false);
 return(true);
}


string CreateTxtObj(int shift, string txt,int fsize, int fcolor){
  double price=0.0;
  datetime time;
  string stime;
  static int objIDX=10;
  string objname="TXT";
  objname="TXT"+objIDX;

  price=High[shift]+(High[shift]-Low[shift])/2;//price >High[shift],  to make the text object created above the bar.
  time=Time[shift];
  if(!ObjectCreate(objname, OBJ_TEXT, 0, time,price))
    Alert("Error:ObjectCreate failed: ",GetLastError());
  if(!ObjectSetText(objname, txt, fsize, "Arial", fcolor)) Alert("Error:ObjectSetText failed: ",GetLastError());
  
  objIDX++;
  return(objname);
}


int start(){
  if(!IsNewBarbyTime()) return;
  if(Bars<100||IsTradeAllowed()==false) return;

  CreateTxtObj(0,DoubleToStr(MathMod(Bars,5),0),7,Red);

  return(0);
}
//+------------------------------------------------------------------+

But the output of the text object is always lower than the position specified. ( No objectCreate failure err or ObjectSetText error was reported. )  As shown in the picture below. You can copy the code and compile it as an EA you will see the output during backtest....

the object created is always lower than the position specified...

May I know what is wrong with my code?Thanks a lot for your time and consideration.

 
tzm:

Dear all,

  I used the following code in my EA to create a text object at a position higher than the current bar's High.

But the output of the text object is always lower than the position specified. ( No objectCreate failure err or ObjectSetText error was reported. )  As shown in the picture below. You can copy the code and compile it as an EA you will see the output during backtest....

May I know what is wrong with my code?Thanks a lot for your time and consideration.


You are drawing the Object on Bar 0 when the High and Low are not yet known,  draw it on Bar 1 instead.
 
RaptorUK :
You are drawing the Object on Bar 0 when the High and Low are not yet known, draw it on Bar 1 instead.


Dear RaptorUK,


You definitely pinpoint the error I made! thank you so much!

 
Your object naming
  static int objIDX=10;
  :
  objname="TXT"+objIDX;
  if(!ObjectCreate(objname, OBJ_TEXT, ..
  :
  objIDX++;
Simplified, static removed
  objname="TXT"+Time[shift];
  if(!ObjectCreate(objname, OBJ_TEXT, ..
Reason: