How can I solve ')' - open parenthesis expected in MQL5?

 

Hi, I encounter this error when I execute my code: ')' - open parenthesis expected , I am unable to solve this error, I don't know what the problem is, would please help me to solve this error if possible? Here is my code:

//+------------------------------------------------------------------+

//|                                                         test.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Global variables                                                |
//+------------------------------------------------------------------+
input int MA_Period = 50; // Period for the moving average
input double Risk_Percentage = 2.0; // Risk percentage per trade
input double Stop_Loss = 100.0; // Initial stop loss in points
input double Take_Profit = 200.0; // Take profit level in points
double LotSize; // Position size based on risk
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Calculate position size based on risk percentage and stop loss
   LotSize = AccountInfoDouble(ACCOUNT_FREEMARGIN) * Risk_Percentage / Stop_Loss;
   
   Print("Expert Advisor initialized successfully.");
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Print("Expert Advisor deinitialized successfully.");
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   double closePrice[]; // Declare 'closePrice' as an array

   if (!CopyClose(Symbol(), 0, 1, 1, closePrice))
   {
      Print("Error copying Close price: ", GetLastError());
      return;
   }

   double ma = iMA(Symbol(), 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE);
   
   if (closePrice[1] < ma && closePrice[0] > ma && closePrice[1] < closePrice[0] - 10) // Add condition to check price trend
   {
      MqlTradeRequest request;
      request.action = TRADE_ACTION_DEAL;
      request.magic = 123456;
      request.symbol = Symbol();
      request.volume = LotSize;
      request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
      request.type = ORDER_TYPE_BUY;
      request.type_filling = ORDER_FILLING_FOK;
      
      double Point = SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Define Point
      
      request.tp = NormalizeDouble(closePrice[0] + Take_Profit * Point, Digits); // Set take profit level

      MqlTradeResult result;

      if (!OrderSend(request, result))
      {
         Print("Buy OrderSend error: ", GetLastError());
         return;
      }
   }
   else if (closePrice[1] > ma && closePrice[0] < ma && closePrice[1] > closePrice[0] + 10) // Add condition to check price trend
   {
      MqlTradeRequest request;
      request.action = TRADE_ACTION_DEAL;
      request.magic = 123456;
      request.symbol = Symbol();
      request.volume = LotSize;
      request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
      request.type = ORDER_TYPE_SELL;
      request.type_filling = ORDER_FILLING_FOK;

      double Point = SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Define Point
      
      request.tp = NormalizeDouble(closePrice[0] - Take_Profit * Point, Digits); // Set take profit level

      MqlTradeResult result;

      if (!OrderSend(request, result))
      {
         Print("Sell OrderSend error: ", GetLastError());
         return;
      }
   }
}

I get this error in these two lines:

request.tp = NormalizeDouble(closePrice[0] + Take_Profit * Point, Digits); // Set take profit level

request.tp = NormalizeDouble(closePrice[0] - Take_Profit * Point, Digits); // Set take profit level


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.03.22
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

The issue might be due to the NormalizeDouble() function.

OPTION-1:

Ensure that the Digits variable is correctly defined, as it's required by the NormalizeDouble() function. Make sure you have int Digits = SymbolInfoInteger(Symbol(), SYMBOL_DIGITS); declared somewhere in your code before you use it in NormalizeDouble() .

int Digits;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Calculate position size based on risk percentage and stop loss
   LotSize = AccountInfoDouble(ACCOUNT_FREEMARGIN) * Risk_Percentage / Stop_Loss;
   
   // Get number of decimal places for the symbol
   Digits = SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);
   
   Print("Expert Advisor initialized successfully.");
   
   return(INIT_SUCCEEDED);
}

This will ensure that the Digits variable is properly defined before being used in the NormalizeDouble() function.


OPTION-2:

Make this change to  declare Digits

int Digits = (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);

// ...

request.tp = NormalizeDouble(closePrice[0] + Take_Profit * Point, Digits);
request.tp = NormalizeDouble(closePrice[0] - Take_Profit * Point, Digits);


or this

request.tp = NormalizeDouble(closePrice[0] + Take_Profit * Point, 4);
request.tp = NormalizeDouble(closePrice[0] - Take_Profit * Point, 4);


Should be fine!

 
محمد دهقانی: I execute my code: ')' - open parenthesis expected , I am unable to solve this error, I don't know what the problem is,
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2.     double ma = iMA(Symbol(), 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE);
    

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

  3. request.tp = NormalizeDouble(closePrice[0] + Take_Profit * Point, Digits); // Set take profit level
    
    request.tp = NormalizeDouble(closePrice[0] - Take_Profit * Point, Digits); // Set take profit level

    Perhaps you should read the manual. MT5 does not have a Predefined Variable Point. Use the actual variable or the function Point.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

 

#define Digits       Digits()
#define Point        Point()
add this after #property at the top
 
You read, and understand the error, "Open parentheses", is expected. Check your reference guide, if still not clear from all the solutions above.
Reason: