Expressions are not allowed on the global scale - page 2

 
Rio12:

although there is no error in the code but the indicator is not drawing any horizontal lines



You return after the first execution of the while loop

      }
     
   return(0);
  }
  }

you can remove the return altogether

or move it

      }
     
  }
   return;
  }
 

Somewhere at the end of the while loop body you need to add an

    i--;
or else your loop runs forever.
 
Keith Watford:

You return after the first execution of the while loop


you can remove the return altogether

or move it

yup did that already still no luck, there isn't any error or warning but its still not drawing any horizontal lines

//+------------------------------------------------------------------+
//|                                                  HLine indicator.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
extern int   HOUR                = 4; //Hour to draw the lines
extern int   MINUTE              = 0; //Minute to draw the lines
extern string LineName="MyLineL";
extern string LineName1="MyLineH";
extern color LineColor=Pink; 
extern color LineColor1=DodgerBlue; 
extern int LineStyle=STYLE_SOLID;
extern int LineStyle1=STYLE_SOLID;
extern int pipDistance = 5;
extern string AlertWav="alert.wav";

extern color Line_Color          = clrBlack; //Color of the lines
input ENUM_LINE_STYLE Line_Style = STYLE_DASH; //Style of the lines

string name = "TimeLine";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {
    int Counted_bars=IndicatorCounted();
    int i=Bars-Counted_bars-1;
   
    while(i>=0)                       
     {  
      if(TimeHour(Time[i]) == HOUR && TimeMinute(Time[i]) == MINUTE)
      {
         if (ObjectFind(name+DoubleToStr(Time[i])) != 0)
        {
            ObjectCreate(name+DoubleToStr(Time[i]), OBJ_HLINE, 0, Low[i], 0);
            
            ObjectSet(LineName, OBJPROP_STYLE, LineStyle);
            ObjectSet(LineName, OBJPROP_COLOR, LineColor);
            ObjectSetInteger(0,name+DoubleToStr(Time[i]),OBJPROP_STYLE,Line_Style);
            ObjectSet(name+DoubleToStr(Time[i]), OBJPROP_COLOR, Line_Color);
            ObjectCreate(LineName1, OBJ_HLINE, 0, High[i], 0);
            ObjectSet(LineName1, OBJPROP_STYLE, LineStyle1);
            ObjectSet(LineName1, OBJPROP_COLOR, LineColor1);
        
            double val =  ObjectGet( LineName, OBJPROP_PRICE1);
            if (Bid <= val ) PlaySound(AlertWav);
            double val1 =  ObjectGet( LineName1, OBJPROP_PRICE1);
            if (Bid >= val1 ) PlaySound(AlertWav);
         }
       }
     i--;
     }
  //return(0);
  }
//+------------------------------------------------------------------+
 
lippmaje:

Somewhere at the end of the while loop body you need to add an

or else your loop runs forever.

yeah thats exactly what happened , when ever i try to attach the indicator to the chart, mt4 terminal stops responding.

after adding i--; , mt4 is not crashing anymore

as for the indicator, its still not showing any horizontal lines

//+------------------------------------------------------------------+
//|                                                  HLine indicator.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
extern int   HOUR                = 4; //Hour to draw the lines
extern int   MINUTE              = 0; //Minute to draw the lines
extern string LineName="MyLineL";
extern string LineName1="MyLineH";
extern color LineColor=Pink; 
extern color LineColor1=DodgerBlue; 
extern int LineStyle=STYLE_SOLID;
extern int LineStyle1=STYLE_SOLID;
extern int pipDistance = 5;
extern string AlertWav="alert.wav";

extern color Line_Color          = clrBlack; //Color of the lines
input ENUM_LINE_STYLE Line_Style = STYLE_DASH; //Style of the lines

string name = "TimeLine";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {
    int Counted_bars=IndicatorCounted();
    int i=Bars-Counted_bars-1;
   
    while(i>=0)                       
     {  
      if(TimeHour(Time[i]) == HOUR && TimeMinute(Time[i]) == MINUTE)
      {
         if (ObjectFind(name+DoubleToStr(Time[i])) != 0)
        {
            ObjectCreate(name+DoubleToStr(Time[i]), OBJ_HLINE, 0, Low[i], 0);
            
            ObjectSet(LineName, OBJPROP_STYLE, LineStyle);
            ObjectSet(LineName, OBJPROP_COLOR, LineColor);
            ObjectSetInteger(0,name+DoubleToStr(Time[i]),OBJPROP_STYLE,Line_Style);
            ObjectSet(name+DoubleToStr(Time[i]), OBJPROP_COLOR, Line_Color);
            ObjectCreate(LineName1, OBJ_HLINE, 0, High[i], 0);
            ObjectSet(LineName1, OBJPROP_STYLE, LineStyle1);
            ObjectSet(LineName1, OBJPROP_COLOR, LineColor1);
        
            double val =  ObjectGet( LineName, OBJPROP_PRICE1);
            if (Bid <= val ) PlaySound(AlertWav);
            double val1 =  ObjectGet( LineName1, OBJPROP_PRICE1);
            if (Bid >= val1 ) PlaySound(AlertWav);
         }
       }
     i--;
     }
  //return(0);
  }
//+------------------------------------------------------------------+
 

Replace

ObjectCreate(name+DoubleToStr(Time[i]), OBJ_HLINE, 0, Low[i], 0);

with

ObjectCreate(name+DoubleToStr(Time[i]),OBJ_HLINE,0,0,Low[i]);

You will get a lot of horizontal lines though.

You may also wish to put

ObjectsDeleteAll(0,name);

In OnDeinit so that you are not left with all the lines when you remove the indicator.

 
Keith Watford:

Replace

with

You will get a lot of horizontal lines though.

You may also wish to put

In OnDeinit so that you are not left with all the lines when you remove the indicator.

well it displayed some lines,but isn't there a way to draw lines only on recent candle whose hour is defined, there are way too many lines 

 
Rio12:

well it displayed some lines,but isn't there a way to draw lines only on recent candle whose hour is defined, there are way too many lines 

so initially i placed everything i can make sense of , but now i have removed the unwanted code and it looks a lot better now.

//+------------------------------------------------------------------+
//|                                                  HLine indicator.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
extern int   HOUR                = 4; //Hour to draw the lines
extern int   MINUTE              = 0; //Minute to draw the lines
extern string LineName="MyLineL";
extern string LineName1="MyLineH";
extern color Line_Color          = clrYellow; //Color of the lines
input ENUM_LINE_STYLE Line_Style = STYLE_SOLID; //Style of the lines

string name = "TimeLine";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll(0,name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {
    int Counted_bars=IndicatorCounted();
    int i=Bars-Counted_bars-1;
   
    while(i>=0)                       
     {  
      if(TimeHour(Time[i]) == HOUR && TimeMinute(Time[i]) == MINUTE)
      {
         if (ObjectFind(name+DoubleToStr(Time[i])) != 0)
        {
            ObjectCreate(name+DoubleToStr(Time[i]), OBJ_HLINE, 0, 0, Low[i]);

            ObjectCreate(LineName1, OBJ_HLINE, 0, 0, High[i]);
         }
       }
     i--;
     }
 
  }
//+------------------------------------------------------------------+

but i can not figure out a way to make it so that it draw the lines only at the most recent 4:00 candle and not all the 4:00 candle . the current indicator is drawing the lines on all the 4:00 candles on the chart  

 
When you draw a new line, remove the old one with ObjDelete.
 
lippmaje:
When you draw a new line, remove the old one with ObjDelete.

can you take a look at the code and tell me if thats where the objdelete is supposed to be ? i have highlighted it 
because when i run the code it only display one line (neither high nor low it is places like this  )

//+------------------------------------------------------------------+
//|                                                  HLine indicator.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
extern int   HOUR                = 4; //Hour to draw the lines
extern int   MINUTE              = 0; //Minute to draw the lines
extern string LineName="MyLineL";
extern string LineName1="MyLineH";
extern color Line_Color          = clrYellow; //Color of the lines
input ENUM_LINE_STYLE Line_Style = STYLE_SOLID; //Style of the lines

string name = "TimeLine";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll(0,name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {
    int Counted_bars=IndicatorCounted();
    int i=Bars-Counted_bars-1;
   
    while(i>=0)                       
     {  
      if(TimeHour(Time[i]) == HOUR && TimeMinute(Time[i]) == MINUTE)
      {
         if (ObjectFind(name+DoubleToStr(Time[i])) != 0)
        {
            ObjectsDeleteAll(0,name);
            
            ObjectCreate(name+DoubleToStr(Time[i]), OBJ_HLINE, 0, 0, Low[i]);

            ObjectCreate(LineName1, OBJ_HLINE, 0, 0, High[i]);
         }
       }
     i--;
     }
  
  }
//+------------------------------------------------------------------+
Files:
SSw.png  39 kb
 
looks ok
Reason: