Download MetaTrader 5

Getting errors but don't know where to correct, please help!

To add comments, please log in or register
Opengates
165
Opengates 2015.01.20 15:22 

Hello Everybody in the house,

I have a little problem with my code and I want anyone with good knowledge in EA programming assist me out. Any help that brings result shall be greatly appreciated.

I was working on this automated system, it should work like this:

It opens two opposite positions simultaneously, set TP at 110p, SL at 45p while the trailing loss would be 35. It uses no indicator and it should open these positions immediately there is price change either up or down. The problem I am having now is that it is giving errors in line 24 as " '}' - unexpected end of program" and the second error is: " '{' - unbalanced parentheses". I have done everything I know to do but couldn't figure out where the problem is. 

Here is the code below:

int init()
  {
//----
 
 int    SL = 450; // Preset SL (pt)  
 int    TP = 1100;  // Preset TP (pt)  
 double Lot = 0.10;  //Lot for the trade
//----
   return(0);
  }
//---------------------------------------------------

int start()  // Special function start 
{  
 //-------------------------------------------------------------------------- 2 --
 
while(true)                                  // Cycle that opens an order    
{     
int ticket1=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, SL, TP);
int ticket2=OrderSend(Symbol(), OP_SELL, Lot, Bid, 2, SL, TP);    
Alert (GetLastError()); 
Alert(Symbol(),"  Sell = ",AccountFreeMargin()// At selling       
  -AccountFreeMarginCheck(Symbol(),OP_SELL,1)); 
   Alert(Symbol(),"  Buy = ",AccountFreeMargin() // At buying       
  -AccountFreeMarginCheck(Symbol(),OP_BUY,1));  
 return(true);
                             // Exit start()                   
//-------------------------------------------------------------------- 7 --     
int ticket1=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, SL, TP);
int ticket2=OrderSend(Symbol(), OP_SELL, Lot, Bid, 2, SL, TP);    
if (ticket1>0 && ticket2>0)                            // Got it!:)       
{        
Alert ("Opened both the orders of Buy and Sell successfully",ticket1,ticket2);
return(true);                  // Exit cycle       
}
//-------------------------------------------------
//Order Modification ==> Trailing Loss(New Stop Loss)
extern int Tral_Stop=35;                      // Trailing distance
//--------------------------------------------------------------- 1 --
   string Symb=Symbol();                       // Symbol
//--------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)         // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) //If the next is available
        {                                       // Analysis of orders:
         int Tip=OrderType();                  // Order type        
         if(OrderSymbol()!=Symb||Tip>1)continue;// The order is not "ours"
         double SL=OrderStopLoss();            // SL of the selected order 
         //------------------------------------------------------ 3 --
         while(true)                            // Modification cycle
           {
            double TS=Tral_Stop;                // Initial value
            int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);//Min. distance
            if (TS<Min_Dist)                    // If less than allowed.
               TS=Min_Dist;                     // New value of TS
            //--------------------------------------------------- 4 --
            bool Modify=false;                  // Not to be modified
            switch(Tip)                         //By order type
              {
               case 0 :                         // Orderð Buy
                  if (NormalizeDouble(SL,Digits)< // If it is lower than we want .
                     NormalizeDouble(Bid-TS*Point,Digits))
                    {
                     SL=Bid-TS*Point;           // then modify it
                     string Text="Buy ";        //  Text for Buy
                     Modify=true;               // To be modified
                    }
                  break;                        // // Exit 'switch'              
               case 1 :                         // Order Sell
                  if (NormalizeDouble(SL,Digits)> // If it is higher than we want.
                     NormalizeDouble(Ask+TS*Point,Digits)
                     || NormalizeDouble(SL,Digits)==0)//or equal to zero
                    {
                     SL=Ask+TS*Point;           // then modify it
                     Text="Sell ";              // Text for Sell
                     Modify=true;               // To be modified
                    }
              }                                 // End of 'switch'
            if (Modify==false)                  // If it is not modified
               break;                           //Exit 'while'
            //--------------------------------------------------- 5 --
            double TP    =OrderTakeProfit();    // TP of the selected order.
            double Price =OrderOpenPrice();     // Price of the selected order
            int    Ticket1=OrderTicket();
            int    Ticket2=OrderTicket();        // Ticket of the selected order
           
            Alert ("Modification ",Text,Ticket1,Ticket2,". Awaiting response..");           
bool Ans=OrderModify(Ticket1,Ticket2,Price,SL,TP,0);//Modify it!           

//------------------------------------------------------------------- 6 --
          
if (Ans==true)                      // Got it! :)             
{              
Alert ("Order ",Text,Ticket1,Ticket2," are modified:)");              
break;                           // From modification cycle.            
 }
       
            //--------------------------------------------------- 7 --
           int Error=GetLastError();           // Failed :(           
switch(Error)                       // Overcomable errors             
{              
case 130:Alert("Wrong stops. Retrying.");                 
RefreshRates();               // Update data                 
continue;                     // At the next iteration              
case 136:Alert("No prices. Waiting for a new tick..");                 
while(RefreshRates()==false)  // To the new tick                    
Sleep(1);                  // Cycle delay                 
continue;                     // At the next iteration              
case 146:Alert("Trading subsystem is busy. Retrying ");                 
Sleep(500);                   // Simple solution                 
RefreshRates();               // Update data                 
continue;                     // At the next iteration                 
// Critical errors              
case 2 : Alert("Common error.");                 
break;                       
// Exit 'switch'              
case 5 : Alert("Old version of the client terminal.");                
 break;                       
// Exit 'switch'              
case 64: Alert("Account is blocked.");                 
break;                       
// Exit 'switch'              
case 133:Alert("Trading is prohibited");                 
break;                       
// Exit 'switch'              
default: Alert("Occurred error ",Error); //Other errors             
}           
break;                              // From modification cycle          
}                                    // End of modification cycle       
//---------------------------------------------------------------------- 8 --      
 }                                       // End of order analysis    
 }                                       // End of order search
//------------------------------------------------------------------------------- 9 --  
return(true);// Exit start() 
}

Please I would be glad if you "masters" in the house can help me out.

Thanks in advance, you are blessed.

Opengates

Opengates
165
Opengates 2015.01.20 15:26  

In addition, line 24 is exactly where a parentheses is located under "init start() while the second error points to line 145 which is the last parentheses "}" on the last line of this code.

Thanks.

Opengates

whroeder1
14753
whroeder1 2015.01.20 16:53  
Opengates:I am having now is that it is giving errors in line 24 as " '}' - unexpected end of program" and the second error is: " '{' - unbalanced parentheses".
Code simplified
int start()  // Special function start 
{  
 //-------------------------------------------------------------------------- 2 -- 
while(true)                                  // Cycle that opens an order    
{     
int ticket1=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, SL, TP);
:
 return(true);
                             // Exit start()                   
//-------------------------------------------------------------------- 7 --     
int ticket1=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, SL, TP); 
:
//------------------------------------------------------------------------------- 9 --  
return(true);// Exit start() 
}
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. After the first return, why is there more unreachable code?
  3. After the last return is the closing brace for the first while.
  4. No returning brace for start
Opengates
165
Opengates 2015.01.21 18:25  
int init()
  {
//----
 
 int    SL = 450; // Preset SL (pt)  
 int    TP = 1100;  // Preset TP (pt)  
 double Lot = 0.10;  //Lot for the trade
//----
   return(0);
  }
//---------------------------------------------------

int start()  // Special function start 
{  
 //-------------------------------------------------------------------------- 2 --
 
while(true)                                  // Cycle that opens an order    
{     
int ticket1=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, SL, TP);
int ticket2=OrderSend(Symbol(), OP_SELL, Lot, Bid, 2, SL, TP);    
Alert (GetLastError()); 
Alert(Symbol(),"  Sell = ",AccountFreeMargin()// At selling       
  -AccountFreeMarginCheck(Symbol(),OP_SELL,1)); 
   Alert(Symbol(),"  Buy = ",AccountFreeMargin() // At buying       
  -AccountFreeMarginCheck(Symbol(),OP_BUY,1));  
 return(true);
                                     
//-------------------------------------------------------------------- 7 --     
int ticket1=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, SL, TP);
int ticket2=OrderSend(Symbol(), OP_SELL, Lot, Bid, 2, SL, TP);    
if (ticket1>0 && ticket2>0)                                   
{        
Alert ("Opened both the orders of Buy and Sell successfully",ticket1,ticket2);
return(true);                     
}
//-------------------------------------------------
//Order Modification ==> Trailing Loss(New Stop Loss)
extern int Tral_Stop=35;                      // Trailing distance
//--------------------------------------------------------------- 1 --
   string Symb=Symbol();                       // Symbol
//--------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)         // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) //If the next is available
        {                                       // Analysis of orders:
         int Tip=OrderType();                  // Order type        
         if(OrderSymbol()!=Symb||Tip>1)continue;// The order is not "ours"
         double SL=OrderStopLoss();            // SL of the selected order 
         //------------------------------------------------------ 3 --
         while(true)                            // Modification cycle
           {
            double TS=Tral_Stop;                // Initial value
            int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);//Min. distance
            if (TS<Min_Dist)                    // If less than allowed.
               TS=Min_Dist;                     // New value of TS
            //--------------------------------------------------- 4 --
            bool Modify=false;                  // Not to be modified
            switch(Tip)                         //By order type
              {
               case 0 :                         // Orderð Buy
                  if (NormalizeDouble(SL,Digits)< // If it is lower than we want .
                     NormalizeDouble(Bid-TS*Point,Digits))
                    {
                     SL=Bid-TS*Point;           // then modify it
                     string Text="Buy ";        //  Text for Buy
                     Modify=true;               // To be modified
                    }
                  break;                        // // Exit 'switch'              
               case 1 :                         // Order Sell
                  if (NormalizeDouble(SL,Digits)> // If it is higher than we want.
                     NormalizeDouble(Ask+TS*Point,Digits)
                     || NormalizeDouble(SL,Digits)==0)//or equal to zero
                    {
                     SL=Ask+TS*Point;           // then modify it
                     Text="Sell ";              // Text for Sell
                     Modify=true;               // To be modified
                    }
              }                                 // End of 'switch'
            if (Modify==false)                  // If it is not modified
               break;                           //Exit 'while'
            //--------------------------------------------------- 5 --
            double TP    =OrderTakeProfit();    // TP of the selected order.
            double Price =OrderOpenPrice();     // Price of the selected order
            int    Ticket1=OrderTicket();
            int    Ticket2=OrderTicket();        // Ticket of the selected order
           
            Alert ("Modification ",Text,Ticket1,Ticket2,". Awaiting response..");           
bool Ans=OrderModify(Ticket1,Ticket2,Price,SL,TP,0);//Modify it!           

//------------------------------------------------------------------- 6 --
          
if (Ans==true)                      // Got it! :)             
{              
Alert ("Order ",Text,Ticket1,Ticket2," are modified:)");              
break;                           // From modification cycle.            
 }
       
            //--------------------------------------------------- 7 --
           int Error=GetLastError();           // Failed :(           
switch(Error)                       // Overcomable errors             
{              
case 130:Alert("Wrong stops. Retrying.");                 
RefreshRates();               // Update data                 
continue;                     // At the next iteration              
case 136:Alert("No prices. Waiting for a new tick..");                 
while(RefreshRates()==false)  // To the new tick                    
Sleep(1);                  // Cycle delay                 
continue;                     // At the next iteration              
case 146:Alert("Trading subsystem is busy. Retrying ");                 
Sleep(500);                   // Simple solution                 
RefreshRates();               // Update data                 
continue;                     // At the next iteration                 
// Critical errors              
case 2 : Alert("Common error.");                 
break;                       
// Exit 'switch'              
case 5 : Alert("Old version of the client terminal.");                
 break;                       
// Exit 'switch'              
case 64: Alert("Account is blocked.");                 
break;                       
// Exit 'switch'              
case 133:Alert("Trading is prohibited");                 
break;                       
// Exit 'switch'              
default: Alert("Occurred error ",Error); //Other errors             
}           
break;                              // From modification cycle          
}                                    // End of modification cycle       
//---------------------------------------------------------------------- 8 --      
 }                                       // End of order analysis    
 }                                       // End of order search
//------------------------------------------------------------------------------- 9 --  
return(true);// Exit start() 
}
Opengates
165
Opengates 2015.01.21 18:26  

This is the full code.

What else do I do to get the solution?

Thanks once again

Carl Schreiber
6670
Carl Schreiber 2015.01.21 18:52  

Sometimes an additional closing ')' is interpreted as '}'.

Search for a ')' without an opening '('.

whroeder1
14753
whroeder1 2015.01.21 18:57  
Opengates:

This is the full code.

What else do I do to get the solution?

  1. What part of "Edit your post" was unclear?
  2. I posted what was wrong (#2.) You must THINK, what do you want it to do.?
Opengates
165
Opengates 2015.01.21 19:28  
WHRoeder:
Opengates:

This is the full code.

What else do I do to get the solution?

  1. What part of "Edit your post" was unclear?
  2. I posted what was wrong (#2.) You must THINK, what do you want it to do.?

Opengates
165
Opengates 2015.01.21 19:37  

Thanks, I now understand. Please I will try to interpret what you said if I am correct:

#2. This implies that I should remove either the 'return' or 'the brace' or remove both?

#3. I should remove the brace for the 'while'?

#4. I should put brace for the start return on the last line of the code?

Thanks so far, pls put me through this once and I believe I should get it right.

You are blessed.

Opengates
165
Opengates 2015.01.21 19:50  
gooly:

Sometimes an additional closing ')' is interpreted as '}'.

Search for a ')' without an opening '('.

Opengates
165
Opengates 2015.01.21 19:51  

Thanks,  will check that too.

You are blessed.

12
To add comments, please log in or register