Error messages - in error processing block

 

The following EA is from the "Simple Expert Advisor" section of the book.

By downloading the tradingexpert.mq4 program from the book, and running it in my MetaEditor, I can compile it without errors .

However, when I tried typing out the tradingexpert.mq4 file, I get 2 error messages:-


..... these codes are the same as those of tradingexpert.mq4 in the MT4 Book.

..... only the last few blocks of codes are copied for easy reference here.

..... codes relevant to the error messages are highlighted.

//-----------------------------------------------------------------------------------------8-+
// Opening Orders Block
while (true) // orders opening loop
{
if (Total==0 && Opn_B==true) // if no current orders & criteria for opening Buy true
{
RefreshRates(); // refresh data
SL=Bid-New_Stop(StopLoss)*Point; // calculatng SL for opening Buy
TP=Bid+New_Stop(TakeProfit)*Point; // calculating TP for opening Buy
Alert("attempting to open Buy, waiting for response..");
Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,2,SL,TP); // opening Buy order
if (Ticket > 0)
{
Alert("Opened Buy order with ticket number ",Ticket);
return; // exit start()
}
if (Fun_Error(GetLastError())==1) // if error is not critical
continue; // retry 'while' loop
return; // exit start() as error critical
}
if (Total==0 && Opn_S==true) // if no current orders & criteria for opening Sell true
{
RefreshRates(); // refresh data
SL=Ask+New_Stop(StopLoss)*Point; // calculating SL for opening Sell
TP=Ask-New_Stop(TakeProfit)*Point; // calculating TP for opening Sell
Alert("attempting to open Buy, waiting for response..");
Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,2,SL,TP); // opening Sell order
if (Ticket > 0)
{
Alert("Opened Sell order with ticket number ",Ticket);
return; // exit start()
}
if (Fun_Error(GetLastError())==1) // if error is not critical
continue; // retry 'while' loop
return; // exit start() as error critical
}
break;
}
//-------------------------------------------------------------------------------------------9-+
return; // exit start()
}
//+------------------------------------------------------------------------------------------10-+

int Fun_Error (int Error) // Function of processing errors
{
switch(Error)
{ // Not crucial errors
case 4: Alert("Trade server is busy. Trying once again..");
Sleep(3000); // Suspend execution of EA for 3000ms
return(1); // Exit the function
case 135:Alert("Price changed. Trying once again..");
RefreshRates(); // Refresh data
return(1); // Exit the function
case 136:Alert("No prices. Waiting for a new tick..");
while(RefreshRates()==false) // cycle 'while' loop as long as data not updated
Sleep(1); // pause in program
return(1); // Exit the error function when data refreshed
case 137:Alert("Broker is busy. Trying once again..");
Sleep(3000);
return(1);
case 146:Alert("Trading subsystem is busy. Trying once again..");
Sleep(500);
return(1);
// Critical errors
case 2: Alert("Common error.");
return(0);
case 5: Alert("Old terminal version.");
Work=false; // Terminate operation in next start() cycle in preliminary processing block
return(0); // Exit the function
case 64: Alert("Account blocked.");
Work=false;
return(0);
case 133:Alert("Trading forbidden.");
return(0);
case 134:Alert("Not enough money to execute operation.");
return(0);
default: Alert("Error occurred: ",Error); // Other variants of error
return(0); // Exit the function
}
}

The following error messages are given on compiling and is color-coded in this post to indicate the lines in the program they refer to:

'(' - function definition unexpected
'Error' - variable not defined
2 error(s), 0 warning(s)

When I copied the Error Processing Block (below //-----------10-+) to another file and compile it, there are no errors:

int start()
{
Fun_Error (GetLastError());
return;
}


int Fun_Error (int Error) // Function of processing errors
{
switch(Error)
{ // Not crucial errors
case 4: Alert("Trade server is busy. Trying once again..");
Sleep(3000); // Suspend execution of EA for 3000ms
return(1); // Exit the function
case 135:Alert("Price changed. Trying once again..");
RefreshRates(); // Refresh data
return(1); // Exit the function
case 136:Alert("No prices. Waiting for a new tick..");
while(RefreshRates()==false) // cycle 'while' loop as long as data not updated
Sleep(1); // pause in program
return(1); // Exit the error function when data refreshed
case 137:Alert("Broker is busy. Trying once again..");
Sleep(3000);
return(1);
case 146:Alert("Trading subsystem is busy. Trying once again..");
Sleep(500);
return(1);
// Critical errors
case 2: Alert("Common error.");
return(0);
case 5: Alert("Old terminal version.");
return(0); // Exit the function
case 64: Alert("Account blocked.");
return(0);
case 133:Alert("Trading forbidden.");
return(0);
case 134:Alert("Not enough money to execute operation.");
return(0);
default: Alert("Error occurred: ",Error); // Other variants of error
return(0); // Exit the function
}

}

On compiling:

Compiling 'test1.mq4'...
0 error(s), 0 warning(s)


Does anyone know why MetaEditor's complier give those error messages when I typed out the EA, but does not give any error messages when I downloaded the EA from the book, or tested the error processing block seperately (when they are all the same codes)?

Many Thanks.

 

ok ... I managed to debug the program. I was able to compile the program without errors.

The errors did not occur at the locations indicated by the MetaEditor during compilation. The errors were not at the locations indicated in color in my previous post.

There were unbalanced braces {} much earlier in the program. This happened because I mis-typed when I typed out the program from a printed copy of the tradingexpert.mq4 EA.

I debugged by copying an initial small block of the typed program into another file, and tried compiling the smaller block of codes to check for errors (remember to add return; } at the end). When compilation of a small block was succesful with no errors, I added another block from the error-riden program. When compilation indicates an error, I checked the codes for the just added block.

This also shows that errors need not occur at the locations indicated by MetaEditor during compilation. Error messages may be shown during compilation, but the real errors may be different from what is described by MetaEditor and not located where MetaEditor indicates.

 
dennis:

The following EA is from the "Simple Expert Advisor" section of the book.

...

Many Thanks.