Need help debugging!

 

This indicator is to draw a horizontal line(x) at 50% of every closed D1 candle from High to Low IF the value of x is more than the OPEN price and less than the CLOSE price of a LONG CANDLE, and vice versa.

and during the close of the candle, there will be a function to delete any existing lines(from previous candles) if the lines are NOT EQUALS to value of x

 

I have tried it and it is working about 90%, but there are always 2-3 missing lines that is supposed to be drawn but wasn't drawn out.

 

Anyone can help? below is the code 

 

 

//+------------------------------------------------------------------+

//|                                                      ProjectName |

//|                                      Copyright 2012, CompanyName |

//|                                       http://www.companyname.net |

//+------------------------------------------------------------------+

#property indicator_chart_window       // Èíäèê. ðèñóåòñÿ â îñíîâíîì îêíå

#property strict


input bool     UsePeriodD1=false;         // Draw line on Period D1

input int      MaxCandleD1=0;             // Candle for Draw (if set 0 Draw for all candle)

input color    colorD1=clrRed;            // Color line on Period D1

input int      widthD1=1;                 // Wigth line on Period D1

input bool     backD1=true;               // BackGround line on Period D1


int   Counted_bars_D1=0;

//--------------------------------------------------------------------

int init() // Ñïåöèàëüíàÿ ôóíêöèÿ init()

  {

//--------------------------------------------------------------------

//--------------------------------------------------------------------

   return(0);                             // Âûõîä èç ñïåö. ô-èè init()

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int deinit()

  {

   ObjectsDeleteAll(ChartID(),"leaff");

   return(0);                             // Âûõîä èç ñïåö. ô-èè init()

  }

//--------------------------------------------------------------------

int start() // Ñïåöèàëüíàÿ ôóíêöèÿ start()

  {

   int i,j;

   string object;

   

   if(UsePeriodD1)

     {

      if(iBars(Symbol(),PERIOD_D1)-Counted_bars_D1>0)

        {

         i=iBars(Symbol(),PERIOD_D1)-Counted_bars_D1;


         if(MaxCandleD1>0)

           {

            if(i>MaxCandleD1)i=MaxCandleD1;

           }

         if(i>iBars(Symbol(),PERIOD_D1))i=iBars(Symbol(),PERIOD_D1);


         while(i>0)

           {

            if(iHigh(Symbol(),PERIOD_D1,i)-iLow(Symbol(),PERIOD_D1,i)<=MathAbs(iOpen(Symbol(),PERIOD_D1,i)-iClose(Symbol(),PERIOD_D1,i))*2)

              {

               for(j=ObjectsTotal();j>=0;j--)

                 {

                  object=ObjectName(j);

                  if(StringFind(object,"leaff_D1_",0)>-1)

                    {

                     if(iOpen(Symbol(),PERIOD_D1,i)>iClose(Symbol(),PERIOD_D1,i))

                       {

                        if(

                           ObjectGetDouble(0,object,OBJPROP_PRICE)>iClose(Symbol(),PERIOD_D1,i) && 

                           ObjectGetDouble(0,object,OBJPROP_PRICE)<iOpen(Symbol(),PERIOD_D1,i)

                           )

                          {

                           ObjectDelete(object);

                          }

                       }

                     if(iOpen(Symbol(),PERIOD_D1,i)<iClose(Symbol(),PERIOD_D1,i))

                       {

                        if(

                           ObjectGetDouble(0,object,OBJPROP_PRICE)<iClose(Symbol(),PERIOD_D1,i) && 

                           ObjectGetDouble(0,object,OBJPROP_PRICE)>iOpen(Symbol(),PERIOD_D1,i)

                           )

                          {

                           ObjectDelete(object);

                          }

                       }

                    }

                 }

               ObjectCreate(0,"leaff_D1_"+IntegerToString(iTime(Symbol(),PERIOD_D1,i)),OBJ_HLINE,0,iTime(Symbol(),PERIOD_D1,i),((iHigh(Symbol(),PERIOD_D1,i)+iLow(Symbol(),PERIOD_D1,i))/2));

               ObjectSetInteger(0,"leaff_D1_"+IntegerToString(iTime(Symbol(),PERIOD_D1,i)),OBJPROP_COLOR,colorD1);

               ObjectSetInteger(0,"leaff_D1_"+IntegerToString(iTime(Symbol(),PERIOD_D1,i)),OBJPROP_WIDTH,widthD1);

               ObjectSetInteger(0,"leaff_D1_"+IntegerToString(iTime(Symbol(),PERIOD_D1,i)),OBJPROP_BACK,backD1);

              }

            i--;

           }


         Counted_bars_D1=iBars(Symbol(),PERIOD_D1);

        }

     }


 


//--------------------------------------------------------------------

   return(0);                             // Âûõîä èç ñïåö. ô-èè init()

  }

//--------------------------------------------------------------------