'if' - expressions are not allowed on a global scope - page 2

 
What part of Count your brackets "(" - ")" and "{" - "}" was unclear to you?
 
hello.Excuseme what this is problem at MQL5 :'CheckReturnCode' - function declarations are allowed on global, namespace or class scope only  
h
 
MANIGHORBANPOUR #: what this is problem at MQL5 :'CheckReturnCode' - function declarations are allowed on global, namespace or class scope only  

What part of Count your brackets "(" - ")" and "{" - "}" was unclear to you?

The question has been answered five (5) times.

We can't see your broken code.

Fix your broken code.

 
Excuse me, what is the correct form?   #include   <Mql5Book\Trade.mqh>
 

Just make sure you don't have anything like this in your code

if(TimeCurrent()>iTime(_Symbol,_Period,1)return;

void OnInit(){
//..
 }

You can see there's a conditional statement outside of any function.

And yes, make sure you have matching brackets. One misplaced bracket can give lots of error messages which often don't help tracking down the error.

 

 please help me to correct this error  'for' - expressions are not allowed on a global scope

also find the code where this error is occurs

// Define variables
datetime startBar;
double highPrice, lowPrice, boxTop, boxBottom;
int boxColor = clrCornflowerBlue;

for(int i = 1; i <= Bars - 1; i++)
  {

// Determine if current bar is a high or low point
   if((High[i] > High[i-1]) && (High[i] > High[i+1]))
     {
      startBar = i;
      highPrice = High[i];
      lowPrice = Low[i];
      break;
     }
   else
      if((Low[i] < Low[i-1]) && (Low[i] < Low[i+1]))
        {
         startBar = i;
         highPrice = High[i];
         lowPrice = Low[i];
         break;
        }
  }

// Loop through remaining bars
for(int i = startBar + 1; i <= Bars - 1; i++)
  {

// Check if current bar is higher or lower than previous high/low
   if(High[i] > highPrice)
     {
      highPrice = High[i];
     }
   else
      if(Low[i] < lowPrice)
        {
         lowPrice = Low[i];
        }

// Check if current bar is last bar in box
   if(i == Bars - 1)
     {
      boxTop = highPrice;
      boxBottom = lowPrice;

      // Draw box
      ObjectCreate("OrderBlock_" + IntegerToString(startBar), OBJ_RECTANGLE, 0, Time[startBar], boxTop, Time[Bars - 1], boxBottom);
      ObjectSet("OrderBlock_" + IntegerToString(startBar), OBJPROP_COLOR, boxColor);
     }
   else
      if((High[i+1] < highPrice) && (Low[i+1] > lowPrice))
        {
         boxTop = highPrice;
         boxBottom = lowPrice;

         // Draw box
         ObjectCreate("OrderBlock_" + IntegerToString(startBar), OBJ_RECTANGLE, 0, Time[startBar], boxTop, Time[i], boxBottom);
         ObjectSet("OrderBlock_" + IntegerToString(startBar), OBJPROP_COLOR, boxColor);

         // Reset variables for next block
         startBar = i;
         highPrice = High[i];
         lowPrice = Low[i];
        }
  }
 
//+------------------------------------------------------------------+
 
@ahmar113 #:   please help me to correct this error  'for' - expressions are not allowed on a global scope. also find the code where this error is occurs

Please don't post code with so much white-space. It makes it hard to read.

Every second line was empty, so I've edited your post and removed the extra white-space in your code.

 
ahmar113 #:   please help me to correct this error  'for' - expressions are not allowed on a global scope. also find the code where this error is occurs

In MQL, all logical code must be inside a function block. Only variable declarations are allowed in the global scope.

Was your code generated by ChatGPT?

Do you understand the basics of coding in C/C++ like languages?

Leaning the basics will help you immensely.

 
MANIGHORBANPOUR #:

i don`t reallly anderstand and ican`t find topic this  about i need more support7


 

You can't just put it in the global scope meaning it must be in the OnTick{ if something } function or somewhere. 
Or within another function you created where it returns a value or bool or something such as MYfunction() { {if(0) Print("OK);} etc or some such thing. 
You can't just throw it in there at the beginning or end of your code or even after the last } bracket. This would take you back to global scope again and MQL4 does not want to see this. 

Remember these closing brackets { } start and stop a control of things. { bracket gets read, } then returns control to wherever it came from. Could be the previous { bracket or back to global scope.

Remember the code reads top to bottom and the bracket } ends your function control to giving control back to global scope or a previous controlled function or could just end. Then it reads the code continuing down giving or taking control to and from global scope, to functions to other functions and back again.

If your brackets release control back to global scope you have to do something else or write a function. You can't just write if / comparison codes in there. Global doesn't want to see this.

It keeps going until it reaches another Function and then exits/returns control back to global again or back to a previous control bracket etc. 

Read the MQL4 book to see some illustrations at the very beginning of the book you will see it there too as well as the information that others posted. 

Hope this helps. 

Reason: