Indicator re-draw problem

 

I have an indicator that draws horizontal lines on a chart based on swings.  Now, the math works every time (Comment() shows the values).  But the indicator isn't re-drawing when it should.

Can anyone tell me why?

Note: the functions HLineCreate, HLineMove, HLineDelete are from the help text.

//+------------------------------------------------------------------+
//|                                                       HLines.mq4 |
//|                                    Copyright 2014, Nondisclosure |
//|                                               http://no.link.yet |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Nondisclosure"
#property link      "http://no.link.yet"
#property version   "1.00"
#property strict
#property indicator_chart_window

extern  string        blah="-Ceiling line variables-";
//input string          InpCeilingName="Ceiling";     // Line name
//input int             InpCeilingNamePrice=25;         // Line price, %
input color           InpCeilingColor=clrBlue;     // Line color
input ENUM_LINE_STYLE InpCeilingStyle=STYLE_DASH; // Line style
input int             InpCeilingWidth=1;          // Line width
input bool            InpCeilingBack=false;       // Background line
input bool            InpCeilingSelection=true;   // Highlight to move
input bool            InpCeilingHidden=false;      // Hidden in the object list
input long            InpCeilingZOrder=0;         // Priority for mouse click
extern string         blah2="--------------------------";
extern string         blah3="-Floor line variables-";
//input string          InpFloorNameName="Floor";     // Line name
//input int             InpFloorNamePrice=25;         // Line price, %
input color           InpFloorColor=clrRed;     // Line color
input ENUM_LINE_STYLE InpFloorStyle=STYLE_DASH; // Line style
input int             InpFloorWidth=1;          // Line width
input bool            InpFloorBack=false;       // Background line
input bool            InpFloorSelection=true;   // Highlight to move
input bool            InpFloorHidden=false;      // Hidden in the object list
input long            InpFloorZOrder=0;         // Priority for mouse click
extern string         blah4="---------------------------";


double Ceiling=0, Floor=0;
datetime LastTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   LastTime=Time[0];
   GetCeilingFloor();
//---
   return(INIT_SUCCEEDED);
  }

void GetCeilingFloor()
{
   double UpperLevel=0, LowerLevel=100000, FastMACurrent=0, SlowMACurrent=0, FastMALast=0, SlowMALast=0, DiffCurrent=0, DiffLast=0;
   int CurrentBar=2, Cross=0;
   bool AllFound=false;
   while(Cross<3)
   {
      SlowMALast=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar-1);
      FastMALast=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar-1);
      SlowMACurrent=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar);
      FastMACurrent=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar);
      DiffLast=FastMALast-SlowMALast;
      DiffCurrent=FastMACurrent-SlowMACurrent;

      if((DiffCurrent>0 && DiffLast<0) || (DiffCurrent<0 && DiffLast>0))  //There's a cross
      {
         Cross++;  
         //if(Cross==1) {ObjectCreate(0,"Cross1",OBJ_ARROW_UP,0,Time[CurrentBar],Low[CurrentBar]);}
         //if(Cross==2) {ObjectCreate(0,"Cross2",OBJ_ARROW_UP,0,Time[CurrentBar],Low[CurrentBar]);}
         //if(Cross==3) {ObjectCreate(0,"Cross3",OBJ_ARROW_UP,0,Time[CurrentBar],Low[CurrentBar]);}
         
      }
      if(DiffCurrent>0 && (Cross==1 || Cross==2)) //Going Long
      {
         UpperLevel=MathMax(High[CurrentBar],UpperLevel);
      }
      if(DiffCurrent<0 && (Cross==1 || Cross==2)) //Going Short
      {
         LowerLevel=MathMin(Low[CurrentBar],LowerLevel);
      }
      CurrentBar++;
   }
   Ceiling=UpperLevel;
   Floor=LowerLevel;
   Comment("Ceiling:",Ceiling,"\n","Floor:",Floor);
   if(ObjectFind(0,"Ceiling")>1)
   {
      HLineMove(0,"Ceiling",Ceiling);   
   }
   if(ObjectFind(0,"Floor")>1)
   {
      HLineMove(0,"Floor",Floor);   
   }
   if(ObjectFind(0,"Ceiling")<0)
   {
      HLineCreate(0,"Ceiling",0,Ceiling,InpCeilingColor,InpCeilingStyle,InpCeilingWidth,InpCeilingBack,InpCeilingSelection,InpCeilingHidden,InpCeilingZOrder);
   }
   if(ObjectFind(0,"Floor")<0)
   {
      HLineCreate(0,"Floor",0,Floor,InpFloorColor,InpFloorStyle,InpFloorWidth,InpFloorBack,InpFloorSelection,InpFloorHidden,InpFloorZOrder);
   }
   ChartRedraw(0);
}


//+------------------------------------------------------------------+
//| Create the horizontal line                                       |
//+------------------------------------------------------------------+
bool HLineCreate(const long            chart_ID=0,        // chart's ID
                 const string          name="HLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 double                price=0,           // line price
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=true,    // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)         // priority for mouse click
  {
//--- if the price is not set, set it at the current Bid price level
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
   ResetLastError();
//--- create a horizontal line
   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))
     {
      Print(__FUNCTION__,
            ": failed to create a horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Move horizontal line                                             |
//+------------------------------------------------------------------+
bool HLineMove(const long   chart_ID=0,   // chart's ID
               const string name="HLine", // line name
               double       price=0)      // line price
  {
//--- if the line price is not set, move it to the current Bid price level
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
   ResetLastError();
//--- move a horizontal line
   if(!ObjectMove(chart_ID,name,0,0,price))
     {
      Print(__FUNCTION__,
            ": failed to move the horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Delete a horizontal line                                         |
//+------------------------------------------------------------------+
bool HLineDelete(const long   chart_ID=0,   // chart's ID
                 const string name="HLine") // line name
  {
//--- reset the error value
   ResetLastError();
//--- delete a horizontal line
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete a horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
  
void OnDeinit(const int Reason)
{
   HLineDelete(0,"Ceiling");
   HLineDelete(0,"Floor");
   ObjectDelete(0,"Cross1");
   ObjectDelete(0,"Cross2");
   ObjectDelete(0,"Cross3");
   Comment(" ");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   bool NewBar = LastTime != Time[0];
   LastTime = Time[0];
   if(NewBar)
   {
      GetCeilingFloor();
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

ObjectFind() returns number of subwindow, starting with 0.

You create objects in subwindow 0, but your test looks like this:

 

   if(ObjectFind(0,"Ceiling")>1)
   {
      HLineMove(0,"Ceiling",Ceiling);   
   }
   if(ObjectFind(0,"Floor")>1)
   {
      HLineMove(0,"Floor",Floor);   
   }

 

 This should be:

 

   if(ObjectFind(0,"Ceiling")>=0)
   {
      HLineMove(0,"Ceiling",Ceiling);   
   }
   if(ObjectFind(0,"Floor")>=0)
   {
      HLineMove(0,"Floor",Floor);   
   }
 

Even better:

 

   if(ObjectFind("Ceiling")>=0)
   {
      HLineMove(0,"Ceiling",Ceiling);   
   }
   else
   {
      HLineCreate(0,"Ceiling",0,Ceiling,InpCeilingColor,InpCeilingStyle,InpCeilingWidth,InpCeilingBack,InpCeilingSelection,InpCeilingHidden,InpCeilingZOrder);
   }

   if(ObjectFind("Floor")>=0)
   {
      HLineMove(0,"Floor",Floor);   
   }
   else
   {
      HLineCreate(0,"Floor",0,Floor,InpFloorColor,InpFloorStyle,InpFloorWidth,InpFloorBack,InpFloorSelection,InpFloorHidden,InpFloorZOrder);
   }
Reason: