Simple indicator locking MT4

 

This indicator is supposed to simply display two horizontal lines at high/low of the bar designated by StartHH and StartMM each day.  As soon as I load it on a chart, it locks MT4.  I took an existing indi I wrote that displays horizontal lines at support/resistance to make this indi, the original one works no problem.  Very little was changed.  Can anyone see what's wrong here?

 

#property indicator_chart_window

//---- User Inputs
extern int    StartHH     = 19;
extern int    StartMM     = 0;
extern int    EndHH       = 23;
extern int    EndMM       = 45;
extern color  HighColor   = White;
extern color  LowColor    = Aqua;
extern int    LineWidth   = 2;
extern int    LineStyle   = 0;      // 0 = Solid, 1 = Dashed, 2 = Dotted, 3 = alternating dashes and dots, 4 = alternating dashes and double dots
extern int    LinesToDraw = 10;     // Total line objects to place on chart

// Constants
#define SecondsInDay 86400

//---- Global Variables
datetime SaveBarTime;
string   LineName;

//---- MT4 Indicator Initialization Function
void init() {
   LineName = "OR_" + StartHH + StartMM + "_";
   SaveBarTime = 0;
   return;
}

//---- MT4 Indicator Removal Function
void deinit() {
   DeleteLines();
   return;
}

//---- MT4 Tick Iteration Function
void start() {
   Print("Here1");
   if (Time[0] != SaveBarTime) {
      SaveBarTime = Time[0];
      if (((TimeHour(Time[1]) == StartHH) && (TimeMinute(Time[1]) == StartMM)) || (ObjectFind(LineName+"1High") < 0)) {
         DeleteLines();
         int      Bar,
                  LineNumber = 1;
         datetime EndTime = SetTime(EndHH,EndMM),
                  StartTime = SetTime(StartHH,StartMM);
         if (EndTime < StartTime)
            EndTime += SecondsInDay;
         while (LineNumber <= LinesToDraw) {
            Bar = iBarShift(NULL,0,StartTime,true);
            if ((TimeCurrent() >= StartTime) && (Bar > 0)) {
               DrawLine(LineName+LineNumber+"High",Bar,iBarShift(NULL,0,EndTime,false),High[Bar],HighColor);
               DrawLine(LineName+LineNumber+"Low",Bar,iBarShift(NULL,0,EndTime,false),Low[Bar],LowColor);
               LineNumber++;
               StartTime -= SecondsInDay;
               EndTime -= SecondsInDay;
            }
            if (Time[Bars -1] > StartTime)
               break;
         }
         WindowRedraw();
      }
   }
   return;
}

void DeleteLines() {
   for (int i = 1; i <= LinesToDraw; i++) {
      if (ObjectFind(LineName+i+"High") == 0)
         ObjectDelete(LineName+i+"High");
      if (ObjectFind(LineName+i+"Low") == 0)
         ObjectDelete(LineName+i+"Low");
   }
   return;
}

void DrawLine(string Name,int i,int j,double Price,color Color) {
   ObjectCreate(Name,OBJ_TREND,0,Time[i],Price,Time[j],Price);
   ObjectSet(Name,OBJPROP_BACK,true);
   ObjectSet(Name,OBJPROP_COLOR,Color);
   ObjectSet(Name,OBJPROP_RAY,false);
   ObjectSet(Name,OBJPROP_STYLE,LineStyle);
   ObjectSet(Name,OBJPROP_WIDTH,LineWidth);
   return;
}

datetime SetTime(int HH,int MM) {
   datetime time = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE));
   if (TimeHour(time) != 0)            //
      time -= (TimeHour(time) * 3600); //
   if (TimeMinute(time) != 0)          //---- This code here because TimeToStr function sometimes
      time -= (TimeMinute(time) * 60); //---- doesn't return midnight properly
   if (TimeSeconds(time) != 0)         //
      time -= TimeSeconds(time);       //
   time += (HH * 3600) + (MM * 60);
   return(time);
}
 
dlewisfl:

This indicator is supposed to simply display two horizontal lines at high/low of specified time range each day.  As soon as I load it on a chart, it locks MT4.  I took an existing indi I wrote that displays horizontal lines at support/resistance to make this indi, the original one works no problem.  Very little was changed.  Can anyone see what's wrong here?

This line . . .

if ((TimeCurrent() >= StartTime) && (Bar > 0)) 

  . . . prevents the LineNumber variable incrementing past 6 so that it is always less than LinesToDraw (10) . . .   the result is an infinite loop.

 
RaptorUK:

This line . . .

  . . . prevents the LineNumber variable incrementing past 6 so that it is always less than LinesToDraw (10) . . .   the result is an infinite loop.

Thanks ... I just spotted the logic flaw. The two lines that decrement StartTime and EndTime by one day should not be within that "if" statement block.  If the start HH and MM specified was later than the current date's time (meaning don't draw lines for today yet), it never got into that block to decrement the start/end time so looped infinitely.   Must've been daydreaming to miss that!  Here's what it should look like:

         while (LineNumber <= LinesToDraw) {
            Bar = iBarShift(NULL,0,StartTime,true);
            if ((TimeCurrent() >= StartTime) && (Bar > 0)) {
               DrawLine(LineName+LineNumber+"High",Bar,iBarShift(NULL,0,EndTime,false),High[Bar],HighColor);
               DrawLine(LineName+LineNumber+"Low",Bar,iBarShift(NULL,0,EndTime,false),Low[Bar],LowColor);
               LineNumber++;
            }
            StartTime -= SecondsInDay;
            EndTime -= SecondsInDay;
            if (Time[Bars -1] > StartTime)
               break;
         }
Reason: