Getting '}' unexpected end of program and '{' unbalanced parentheses.

 
// include the file Trade.mqh
#include<Trade\Trade.mqh>

// Create an instance of Ctrade
CTrade  trade;

void OnTick()
   {
   // Get the Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   // get the account balance
   double Balance=AccountInfoDouble(Account_BALANCE); 
   
   // get the account equity
   double Equity=AccountInfoDouble(Account_EQUITY); 
   
   // if equity and balance are equal
   if (Equity==Balance)
   { // buy stop, 100 microlots, 100 points above Ask, no SL,
     //   30 points TP, no expiration, no date, no comment 
     trade.BuyStop(0.10,Ask+100*_Point,_Symbol,0,Ask+300*_Point,ORDER_TIME_GTC,0,0); 
     
  }
  
  {
   // Get the Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   // get the account balance
   double Balance=AccountInfoDouble(Account_BALANCE); 
   
   // get the account equity
   double Equity=AccountInfoDouble(Account_EQUITY); 
   
   // if equity and balance are equal
   if (Equity==Balance)
   { // sell stop, 100 microlots, 100 points below Ask, no SL,
     //   300 points TP, no expiration, no date, no comment 
     trade.SellStop(0.10,Ask+100*_Point,_Symbol,0,Ask+300*_Point,ORDER_TIME_GTC,0,0); 
     
  }
     // Call the Trailing Stop Module
     CheckTrailingStop(Ask);
  }  // End of thr OnTick function
  
  void CheckTrailingStop(double Ask)
    {
       //set the desired Stop Loss to 10 points
       double SL=NormalizeDouble(Ask-10*_Point,_Digits);
       
       //check all open positions for the current symbol
       for(int i=PositionsTotal()-1; i>0; i--) // count all currency pair positions
       {
         string symbol=PositionGetSymbol(i); //get position symbol
         
         if (_Symbol==symbol) // if chart symbol equals position symbol
         {
             //Get the ticket number
             ulong PositionTicket=PositionGetInteger (POSITION_TICKET);
             
             // get the current Stop Loss
             double CurrentStopLoss=PositionGetDouble (POSITION_SL);
             
             //If current Stop Loss is below 200 points from Ask Price
             if (currentStopLoss<SL)
             {
             // Modify the Stop Loss by 20 Points
             trade.PositionModify(PositionTicket,(CurrentStopLoss+20*_Point),0);
             }
         } /// End symbol if loop
         
       } // End for loop
   } // End Trailing Stop function

Hi all, I coded a simple buy stop and sell stop with a trailing stop but keep getting '}' unexpected end of program and '{' unbalanced parentheses. I've attached the code above. Thank you in advance.

Can anyone help?

<Title edited by moderator>

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
kevin kweya: I coded a simple buy stop and sell stop with a trailing stop but keep getting '}' unexpected end of program and '{' unbalanced parentheses.

In MetaEditor use the Styler to format your code so that you can see more easily where you are missing braces and brackets.

  • Tools→Styler (Ctrl+,)

Also, when you place the cursor just to the right of a starting brace/bracket or just to the left of an ending one, the editor highlights it and the corresponding one. That will help identify if it's the incorrect one.

You have in fact several errors in your code, not just an unbalanced brace.

EDIT: Next time, don't write such a long title. Put it in the post description text and use a short summary title instead.

 
// include the file Trade.mqh
#include<Trade\Trade.mqh>

// Create an instance of Ctrade
CTrade  trade;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
// Get the Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

// get the account balance
   double Balance=AccountInfoDouble(ACCOUNT_BALANCE);

// get the account equity
   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);

// if equity and balance are equal
   if(Equity==Balance)
     {
      // buy stop, 100 microlots, 100 points above Ask, no SL,
      //   30 points TP, no expiration, no date, no comment
      trade.BuyStop(0.10,Ask+100*_Point,_Symbol,0,Ask+300*_Point,ORDER_TIME_GTC,0,0);

     }

//{
// Get the Ask price
   Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

// get the account balance
   Balance=AccountInfoDouble(ACCOUNT_BALANCE);

// get the account equity
   Equity=AccountInfoDouble(ACCOUNT_EQUITY);

// if equity and balance are equal
   if(Equity==Balance)
     {
      // sell stop, 100 microlots, 100 points below Ask, no SL,
      //   300 points TP, no expiration, no date, no comment
      trade.SellStop(0.10,Ask+100*_Point,_Symbol,0,Ask+300*_Point,ORDER_TIME_GTC,0,0);

     }
// Call the Trailing Stop Module
   CheckTrailingStop(Ask);
 }  // End of thr OnTick function

   void CheckTrailingStop(double Ask)
     {
      //set the desired Stop Loss to 10 points
      double SL=NormalizeDouble(Ask-10*_Point,_Digits);

      //check all open positions for the current symbol
      for(int i=PositionsTotal()-1; i>0; i--) // count all currency pair positions
        {
         string symbol=PositionGetSymbol(i); //get position symbol

         if(_Symbol==symbol)  // if chart symbol equals position symbol
           {
            //Get the ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);

            // get the current Stop Loss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);

            //If current Stop Loss is below 200 points from Ask Price
            if(CurrentStopLoss<SL)
              {
               // Modify the Stop Loss by 20 Points
               trade.PositionModify(PositionTicket,(CurrentStopLoss+20*_Point),0);
              }
           } /// End symbol if loop

        } // End for loop
     } // End Trailing Stop function 
 
Just by looking
Account_BALANCE

is wrong It should be

// get the account balance
   Balance=AccountInfoDouble(ACCOUNT_BALANCE);

// get the account equity
   Equity=AccountInfoDouble(ACCOUNT_EQUITY);
 
  {
   // Get the Ask price
   
   if (Equity==Balance)
   { // sell stop, 100 microlots, 100 points below Ask, no SL,
     //   300 points TP, no expiration, no date, no comment 
     trade.SellStop(0.10,Ask+100*_Point,_Symbol,0,Ask+300*_Point,ORDER_TIME_GTC,0,0); 
     
  }
     // Call the Trailing Stop Module
     CheckTrailingStop(Ask);
  }  // End of thr OnTick function

That bracket is NOT the end of OnTick function. It is the end of the open bracket.

 
kevin kweya:

Hi all, I coded a simple buy stop and sell stop with a trailing stop but keep getting '}' unexpected end of program and '{' unbalanced parentheses. I've attached the code above. Thank you in advance.

Can anyone help?

<Title edited by moderator>

Thank you so much this works now , i appreciate your help.
 
Eric Snusber #:
Just by looking

is wrong It should be

Thank you
 
Fernando Carreiro #:

In MetaEditor use the Styler to format your code so that you can see more easily where you are missing braces and brackets.

  • Tools→Styler (Ctrl+,)

Also, when you place the cursor just to the right of a starting brace/bracket or just to the left of an ending one, the editor highlights it and the corresponding one. That will help identify if it's the incorrect one.

You have in fact several errors in your code, not just an unbalanced brace.

EDIT: Next time, don't write such a long title. Put it in the post description text and use a short summary title instead.

Thank you so much
 
Ugochukwu Mobi #:
This has no errors but has warnings and upon testing it on strategy tester it keeps adding pending trades, it's only supposed to add one buy stop. Sorry i'm new to coding so i'm kinda lost in this world
 
Fernando Carreiro #:

In MetaEditor use the Styler to format your code so that you can see more easily where you are missing braces and brackets.

  • Tools→Styler (Ctrl+,)

Also, when you place the cursor just to the right of a starting brace/bracket or just to the left of an ending one, the editor highlights it and the corresponding one. That will help identify if it's the incorrect one.

You have in fact several errors in your code, not just an unbalanced brace.

EDIT: Next time, don't write such a long title. Put it in the post description text and use a short summary title instead.

I apologise for wrongfully inputting the description in the heading bar, im new to this and new in coding. could you please elaborate for me as to where my errors in my code are?
 
kevin k # could you please elaborate for me as to where my errors in my code are?

I did. #4