Function definition unexpected. help please

 
/*
Hello to all
Hope you had a good weekend. I study on my weekend, then tried a different approach. But I stalled out 
with error described as:  '(' function definition unexpected. 
Same error for both user-defined function "Close_Open_Sell" and "Close_Open_Buy".
I'll show the code below with "<<<<<<<" as flags for location of errors.
Thanks to anyone who can help and with explanation. As I say, I was studying and redoing all day yesterday. My head is dizzy.
Cheers
*/

double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

//------------- Existing Orders -----------
int start() {
for(int index = OrdersTotal() - 1; index >= 0; index--) if (
    OrderSelect(index, SELECT_BY_POS)                              // Only my orders w/
&&  OrderMagicNumber()  == MagicNumber                             // my magic number
&&  OrderSymbol()       == Symbol()) {                             // and period and symbol
    if(OrderType()==OP_SELL) {
    ATR = iATR(Symbol(),0,20,1);
    StopLoss_Sell = (ATR*2);
    Close_Open_Sell();  
  } else  {
    if (OrderType() == OP_BUY)  {
    ATR = iATR(Symbol(),0,20,1);
    StopLoss_Buy = (ATR*2);
    Close_Open_Buy();
    }
   }
  }

//-------------- User-Defined ------------

void Close_Open_Sell()  //<<<<<<<<<<<<<<<<<<<<<<<<< '('-funtion definition unexpected
  if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2));  {
   OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
  return();
}
void Close_Open_Buy()  //<<<<<<<<<<<<<<<<<<<<<<<<<< '('-function definition unexpected
  if(Bid <= OrderSelect(OrderOpenPrice) - (ATR*2));  {
   OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
  return();
}  
//------------------Open Buy----------------

       if (Ask > High[iHighest(NULL,0,MODE_HIGH,10, 1)])  {
          if(OrdersTotal() < 0)  {                              
       int BUY = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-50*Point,Ask+50*Point," ",0,0,Blue);    
     }  
  } else  {
    
//--------------- Open Sell ----------------
                                       
   if (Bid < Low[iLowest(NULL,0,MODE_LOW,10, 1)])  {                                                                                  
     int SELL = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+50*Point,Bid-50*Point," ",0,0,Red);  
       }
      return(0); 
    } 
  } 
Hello
 

Looks like you haven't understood the concept of code-blocks and the use of braces.

For example, you have not enclosed the entire Close_Open_Sell() code-block in braces.

I seem to remember Gordon asking you to read the documentation on this very topic. That was good advice.


CB

 
cloudbreaker wrote >>

Looks like you haven't understood the concept of code-blocks and the use of braces.

For example, you have not enclosed the entire Close_Open_Sell() code-block in braces.

I seem to remember Gordon asking you to read the documentation on this very topic. That was good advice.

CB

Hello CB

That is correct that he suggested the reading. The reading was done twice and refered to yesterday. The reading compiled into many different formats. I am sorry that I still do not understand. Braces at the end of lines. Some to be at the beginning. Some programmers that use was the Mocrosoft 3.0, etc. Not that I under estimate, it is that I overlooked from the overwhelming amount of new info. My fault.

I'll recheck.

Thanks for your input.

Cheers

 

No probs, if you have any specific questions about how to use braces, let me know. There's really not that much to it.


CB

 

Thank You CB

Since your first post to this thread, I have used the today for more studying and rewritting the code. Check with the next post please.

 
Ok here we go. Hope I did not buther it up beyond reason.
/*
Hello to all especially to CB,
Error described as:  '(' function definition unexpected -- shows only with "Close_Open_Sell".
The line "Close_Open_Buy" does not have that error any more.???????

One solution cancels one problem but loops back to other previous problems. Dizzy. 
This is not the only problem I have contended with. Just one I have stalled out with. Maybe tomorrow will be better. 
There has been many other problems I have dealt with succesfully, thanks to the book and to the gurus of this site.
I appreciate the help. The hints, and clues you throw help just as well, such as, readings, pointing out examples.  
Lines are flagged where there are difficulties.
Each line has a brief summary, in hopes that someone can see where it is that I have weakness.
Thanks to anyone who can help.
Cheers
*/

double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

//------------- Existing Orders -----------
int start()                                                         // special function 
{                                                                   // open brace. Begin block of code                                                               // open brace
for(int index = OrdersTotal() - 1; index >= 0; index--)             // operator 'for', expression, condition, expression 
{                                                                  // open brace, operators follow the open brace
  if (OrderSelect(index, SELECT_BY_POS)                             // compound operator 'if' condition 1
&&  OrderMagicNumber()  == MagicNumber                              // logical operation &&
&&  OrderSymbol()       == Symbol())                                // logical operation &&
                                                                    // End of description condition 1, 
                                                                    // no result for condition 1, such as Alert, Print,
                                                                    // Therefore,  no closing '}'???
                                                                  
  if(OrderType()==OP_SELL)        // compound operator 'if' condition 2
&&  ATR = iATR(Symbol(),0,20,1);  // <<<<<<<<<< logical operation &&
&&  StopLoss_Sell = (ATR*2);      // <<<<<<<<<< logical operation &&
    Close_Open_Sell();            // <<<<<<<<<< inserted function call, here a result is requested
  }                               // <<<<<<<<<< end block of code with closing '}', for condition 2


    
for (index = OrdersTotal() -1; index >= 0; index--)                // operator 'for' expression, condition, expression 
  {                                                                 // open brace. begin block of code
   if(OrderSelect(index, SELECT_BY_POS)                             // compound operator 'if' condition 1
&&  OrderMagicNumber()  ==MagicNumber                               // logical operation &&
&&  OrderSymbol()       ==Symbol())                                 // logical operation &&
                                                                    // end of description condition 1
                                                                    // no result for condition 
                                                                    // Therefore, no closeing '}'????
                                                                    
   if (OrderType() == OP_BUY)      // compound operator 'if' condition 2                                 
&&  ATR = iATR(Symbol(),0,20,1);   // logical operation &&                                
&&  StopLoss_Buy = (ATR*2);        // logical operation &&
    Close_Open_Buy();              // function call     
    }                              // end of block of code with closeing '}'
   return(0);                      // exit function
// }                                 // exit start ()


   
//-------------- User-Defined ------------

void Close_Open_Sell() {           //<<<<<<<<<<<<<<<<<<<<<<<<< '('-funtion definition unexpected, UNresolved
  if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2));  
   OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
  return();
}
void Close_Open_Buy()  {         
  if(Bid <= OrderSelect(OrderOpenPrice) - (ATR*2));  
   OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
   }
  return();
}                                //<<<<<<<<<<<<<<<<<<<<<<<<<<< '}' unbalanced parentheses




/*
//------------------Open Buy----------------

       if (Ask > High[iHighest(NULL,0,MODE_HIGH,10, 1)])  {  //<<<<<<<< '{'-Expression on global scope not allowed
          if(OrdersTotal() < 0);                              
       int BUY = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-50*Point,Ask+50*Point," ",0,0,Blue);    
     }  
   else  {     //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< '{'-expression on global scope not allowed                                             
    
//--------------- Open Sell ----------------
                                       
   if (Bid < Low[iLowest(NULL,0,MODE_LOW,10, 1)])  {                                                                                  
     int SELL = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+50*Point,Bid-50*Point," ",0,0,Red);  
       }
      return(0); 
    } 
  
*/   


 
Huckleberry:
Ok here we go. Hope I did not buther it up beyond reason.

You still have too many problems... There is no point even trying to understand the logic in your code, since most of it doesn't make any sense. Here are some of the problems:

  • Braces (and indentation) - your code is still a mess in that regard. The most basic rule is this: for every opening brace '{' there must be a matching closing brace '}'. This is called brace balancing. Code will never compile if braces are not balanced.
  • Your usage of the '&&' operator makes no sense in the following code:
if(OrderType()==OP_SELL)        // compound operator 'if' condition 2
&&  ATR = iATR(Symbol(),0,20,1);  // <<<<<<<<<< logical operation &&
&&  StopLoss_Sell = (ATR*2);      // <<<<<<<<<< logical operation &&
    Close_Open_Sell();            // <<<<<<<<<< inserted function call, here a result is requested
  }                               // <<<<<<<<<< end block of code with closing '}', for condition 2

I assume this is what u meant? If so, you should read AGAIN about conditional operator 'if - else' and also make sure u understand boolean operators.

if(OrderType()==OP_SELL)         // compound operator 'if' condition 2
  {
   ATR = iATR(Symbol(),0,20,1);  // <<<<<<<<<< logical operation &&
   StopLoss_Sell = (ATR*2);      // <<<<<<<<<< logical operation &&
   Close_Open_Sell();            // <<<<<<<<<< inserted function call, here a result is requested
  }                              // <<<<<<<<<< end block of code with closing '}', for condition 2

  • OrderSelect() and OrderOpenPrice() usage in the following makes no sense:
void Close_Open_Sell() {           //<<<<<<<<<<<<<<<<<<<<<<<<< '('-funtion definition unexpected, UNresolved
  if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2));  
   OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
  return();
}
Please read about OrderSelect() and OrderOpenPrice(). Also read about return operator - either u return something or simply use the version with no brackets (but your way is invalid).
 

Hello Gordon

As I understand the required reading for OrderOpenPrice and Orderselect---

if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2)) //if condition is greater than or equal to,
//The ATR[1] that is multiplied by two, then is added to
// the open price of the order that was selected. If so,

/*
Hello to all,
The compilier did not like the code I wrote. Nothing new.  
The code has taken on a different appearance though, after reading the last post.
The errors now look familiar to the first time I tried many weeks ago
before I started posting this code.
All problems seem to exist within the Function Call parameters.
Comma or semi expected. 
ATR expression on global scope not allowed
unbalanced parentheses

Thanks to anyone who can help.
Cheers
*/

double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

//-------------- Existing Sell Orders -----------------
int start()                                                         // special function start()
{                                                                   // >> open brace. begin program                          
for(int index = OrdersTotal() - 1; index >= 0; index--)  
{                                                                   // >> open brace begin block
  if (OrderSelect(index, SELECT_BY_POS)                             // existing orders
&&  OrderMagicNumber()  == MagicNumber                              // with my MN    
&&  OrderSymbol()       == Symbol())                                // with my symbol
}                                                                   // << close brace end of block
                                                                                          
if(OrderType()==OP_SELL)  
  {                                                                 // open brace                         
    Close_Open_Sell(ATR);                                           // function call
   }                                                                // << close brace

//-------------- Existing Buy Orders -----------------     
for(index = OrdersTotal() -1; index >= 0; index--)                   
  {                                                                 // >> open brace              
   if(OrderSelect(index, SELECT_BY_POS)                                                                                                                                         
&&  OrderMagicNumber()  ==MagicNumber                            
&&  OrderSymbol()       ==Symbol()) 
}                                                                    // << close brace
                                                                                                                                                                             
if (OrderType() == OP_BUY) 
  {                                                                  // >> open brace                                                    
    Close_Open_Buy(ATR);                                             // function call     
  return(0);                                                         
}                                                                    // << close brace
    
//-------------- User-Defined ------------

void Close_Open_Sell
  { 
   ATR = iATR(Symbol(),0,20,1);  
//   StopLoss_Sell = (ATR*2);                                          // >> open brace
    if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2)) //if condition is greater than or equal to,
                                                     //The ATR[1] that is multiplied by two, then is added to
                                                     // the open price of the order that was selected. If so,
                                                     // control goes to OrderClose
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
   return(0);
  }                                                                  // << close brace
                                                                     
void Close_Open_Buy 
  {                                                                  // >> open brace
   ATR = iATR(Symbol(),0,20,1);  
//   StopLoss_Buy = (ATR*2);                         
    if(Bid <= OrderSelect(OrderOpenPrice) - (ATR*2))  
    OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
   return(0);
  }                                                                  //  << close brace
}                                                                   //  << close brace, end start () 


// control goes to OrderClose

 
You took the rule about brace balancing and completely broke up all the nested blocks in a way that makes no sense. For example:
for(int index = OrdersTotal() - 1; index >= 0; index--)  
{                                                                   // >> open brace begin block
  if (OrderSelect(index, SELECT_BY_POS)                             // existing orders
&&  OrderMagicNumber()  == MagicNumber                              // with my MN    
&&  OrderSymbol()       == Symbol())                                // with my symbol
}                                                                   // << close brace end of block
                                                                                          
if(OrderType()==OP_SELL)  
  {                                                                 // open brace                         
    Close_Open_Sell(ATR);                                           // function call
   }                                                                // << close 

The braces after the 'for' statement define a block of code which is 'looped on'. So u r basically looping on nothing now, the Close_Open_Sell() function is not being looped on, because it's not in that block!


It should be like so:

   for(int index=OrdersTotal()-1; index>=0; index--)  
      {                                                                   
      if (OrderSelect(index, SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
         {                                                                                    
         if(OrderType()==OP_SELL)  
            Close_Open_Sell(ATR);
         }
      }

Notice the braces are still balanced - each opening brace has a closing brace. But all code and blocks of code that are supposed to be looped on are inside the 'for' loop block.


In the following code:

if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2))
Your usage of OrderSelect() is still wrong. The function prototype for OrderSelect() is:
bool OrderSelect(int index, int select, int pool=MODE_TRADES)

What does that have to do with the way u r using it???


OrderOpenPrice us a function, not a variable, it should be called like so: OrderOpenPrice(), but first u must properly select an order.

 
gordon wrote >>
You took the rule about brace balancing and completely broke up all the nested blocks in a way that makes no sense. For example:

The braces after the 'for' statement define a block of code which is 'looped on'. So u r basically looping on nothing now, the Close_Open_Sell() function is not being looped on, because it's not in that block!

It should be like so:

Notice the braces are still balanced - each opening brace has a closing brace. But all code and blocks of code that are supposed to be looped on are inside the 'for' loop block.

In the following code:

Your usage of OrderSelect() is still wrong. The function prototype for OrderSelect() is:

What does that have to do with the way u r using it???

OrderOpenPrice us a function, not a variable, it should be called like so: OrderOpenPrice(), but first u must properly select an order.

THIS IS AN UPDATE

Hello Gordon,

THIS IS AN UPDATE....The code is not perfected. But it has compiled. Please read the rest of the post. I have NOT yet posted the

compiled code yet. The code that is below explains a part of my confusion. Only a part.

The area where I have been confused has been found. I have been under the impression, the order had been properly ordered within MODULE ONE. Since the order fits all the above conditions, the control is now sent by the FUNCTION CALL.... " Close_Open_Sell"... to MODULE THREE. The order info from MODULE ONE is now in MODULE THREEE. Since the EA already knows the order info in MODULE THREE why ask for those conditions to be repeated.

Thanks Gordon, and everyone else.

double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

//-------------- Existing Sell Orders -----------------

//-------------------MODULE ONE << << << << << 
int start()                                                         // special function start()
{                                                                   // >> open brace. begin program                          
for(int index = OrdersTotal() - 1; index >= 0; index--)  
{                                                                   // >> open brace begin block
  if (OrderSelect(index, SELECT_BY_POS)                             // existing orders
&&  OrderMagicNumber()  == MagicNumber                              // with my MN    
&&  OrderSymbol()       == Symbol())                                // with my symbol
{                                                                  // << close brace end of block
                                                                                          
if(OrderType()==OP_SELL)  
  Close_Open_Sell(ATR);                                           // function call
}                                                                // << close brace
}
//-------------- Existing Buy Orders -----------------

//------------------- MODULE TWO  << << << << <<     
for(index = OrdersTotal() -1; index >= 0; index--)                   
  {                                                                 // >> open brace              
   if(OrderSelect(index, SELECT_BY_POS)                                                                                                                                         
&&  OrderMagicNumber()  ==MagicNumber                            
&&  OrderSymbol()       ==Symbol()) 
{                                                                    // << close brace
                                                                                                                                                                             
if (OrderType() == OP_BUY) 
  Close_Open_Buy(ATR);                                             // function call     
  return(0);                                                         
}                                                                    // << close brace
}    
//-------------- User-Defined ------------

//-------------- MODUKLE THREE  << << << << <<
void Close_Open_Sell
  { 
    bool OrderSelect(index, SELECT_BY_POS, MODE_TRADES)// Is this not the same conditions mentioned in MODULE ONE?
    {
      ATR = iATR(Symbol(),0,20,1);  
  //   StopLoss_Sell = (ATR*2);                                          // >> open brace
      if(Ask >= OrderSelect(OrderOpenPrice) + (ATR*2)) //if condition is greater than or equal to,
                                                     //The ATR[1] that is multiplied by two, then is added to
                                                     // the open price of the order that was selected. If so,
                                                     // control goes to OrderClose
 

Hello to all

This is the compiled program. There were bugs to start with, but there are now buys, sells and exits. Thank you for your help.

But not everthing works like the book has said. Yes the braces balance, but not exactly like they should. There are two open braces in a row. When I had tried to place them like they should, errors appear. But this code is working at the moment???

Why so many open braces in a row, and of course, the closeing braces line up at the end of bolcks??

Thanks again

Cheers

//+------------------------------------------------------------------+
//|                                           Compiled v5.mq4        
double ATR;
double Slippage = 3;
double StopLoss_Buy;
double StopLoss_Sell;
double Lots=0.4;
double MagicNumber;

//-------------- Existing Sell Orders -----------------

//-------------------MODULE ONE << << << << << 

int start()                                                       
{                                                      // 1st open brace
  ATR = iATR(Symbol(),0,20,1);                                                                                      
  for(int index = OrdersTotal()-1; index >= 0; index--)
   {                                                    //Second open brace               
    if (OrderSelect(index -1, SELECT_BY_POS)==true)
     {                                                 //Third open brace
    if (OrderType()==OP_SELL && Ask >= OrderOpenPrice() + (ATR*2)                             
&&  OrderMagicNumber()  == MagicNumber                             
&&  OrderSymbol()       == Symbol())                           
                                                               
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);                                                                                         
  }
}                                           
                                                           
//-------------- Existing Buy Orders -----------------

//------------------- MODULE TWO  << << << << <<     
for(index = 1; index<=OrdersTotal();  index--)                    
  {                                                                            
    if(OrderSelect(index -1, SELECT_BY_POS)==true)
    {                         
    if(OrderType()==OP_BUY && Bid <= OrderOpenPrice() - (ATR*2)                                                                                                   
&&  OrderMagicNumber()  ==MagicNumber   
&&  OrderSymbol()       ==Symbol())  
                                                                
    OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);  
    }                                                                                                          
  return(0);                                                         
}                                                                  


//-------------------- Open Buy -------------------- 
     if (Ask > High[iHighest(NULL,0,MODE_HIGH,10, 1)]) 
       {  
          if(OrdersTotal() == 0)  
          int BUY = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-10*Point,Ask+10*Point," ",0,0,Blue);    
       }
   else                                                  
    
//-------------------- Open Sell --------------------
  {                                     
   if (Bid < Low[iLowest(NULL,0,MODE_LOW,10, 1)])
    {         
     if(OrdersTotal() == 0)                                                                              
     int SELL = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+10*Point,Bid-10*Point," ",0,0,Red);  
       }
      return(0); 
    } 
  }  
Reason: