Code checking - EA that opens position when candle closes above/below MA (MT5)

 

Hi everyone! When I compile the code below there are 2 errors:

'}' - unexpected end of program Hota001.mq5 89 6

'{' - unbalanced parentheses Hota001.mq5 17 3

But when I add an } in the end, the errors jump to 17. Any help?


// Define the input parameters
input int ema_period = 9;    // Period for the Exponential Moving Average
input double lot_size = 0.1; // Lot size for each trade
input int stop_loss = 50;   // Stop loss in pips
input int take_profit = 100; // Take profit in pips

// Define the global variables
double ema = 0;             // Variable to store the Exponential Moving Average
double close = 0;           // Variable to store the current close price
double prev_close = 0;      // Variable to store the previous close price
bool is_trade_open = false; // Flag to track if a trade is open
int trade_type = 0;         // Variable to store the type of trade (buy or sell)
double trade_open_price = 0;// Variable to store the open price of the trade

// Define the OnInit() function
void OnInit()
  {
// Calculate the Exponential Moving Average
   ema = iMA(_Symbol, PERIOD_CURRENT, ema_period, 0, MODE_EMA, PRICE_CLOSE, 0);


// Define the OnTick() function
   void OnTick()
     {
      // Get the current close price
      close = SymbolInfoDouble(_Symbol, SYMBOL_BID);

      // Get the previous close price
      prev_close = iClose(_Symbol, PERIOD_CURRENT, 1);

      // Check if the previous candle closed above the Exponential Moving Average
      if(prev_close > ema)
        {
         // Open a buy trade if no trade is currently open
         if(!is_trade_open)
           {
            trade_type = OP_BUY;
            trade_open_price = close;
            int stoploss = trade_open_price - stop_loss * _Point;
            int takeprofit = trade_open_price + take_profit * _Point;
            int ticket = OrderSend(_Symbol, trade_type, lot_size, close, 3, stoploss, takeprofit, "Buy Order", 0, 0, Green);
            if(ticket > 0)
              {
               is_trade_open = true;
              }
           }
        }

      // Check if the previous candle closed below the Exponential Moving Average
      else
         if(prev_close < ema)
           {
            // Open a sell trade if no trade is currently open
            if(!is_trade_open)
              {
               trade_type = OP_SELL;
               trade_open_price = close;
               int stoploss = trade_open_price + stop_loss * _Point;
               int takeprofit = trade_open_price - take_profit * _Point;
               int ticket = OrderSend(_Symbol, trade_type, lot_size, close, 3, stoploss, takeprofit, "Sell Order", 0, 0, Red);
               if(ticket > 0)
                 {
                  is_trade_open = true;
                 }
              }
           }

      // Check if a trade is open
      if(is_trade_open)
        {
         // Get the current profit/loss of the trade
         double profit_loss = trade_type == OP_BUY ? close - trade_open_price : trade_open_price - close;

         // Check if the stop loss or take profit level is reached
         if(profit_loss >= take_profit * _Point || profit_loss <= -stop_loss * _Point)
           {
            // Close the trade
            int ticket = OrderClose(OrderTicket(), lot_size, Bid, 3, Red);
            if(ticket > 0)
              {
               is_trade_open = false;
               trade_type = 0;
               trade_open_price = 0;
              }
           }
        }
      // Save the current close price for the next tick
      prev_close = close;
     }
//+------------------------------------------------------------------+  
 
Augusto Ramos: '}' - unexpected end of program Hota001.mq5 89 6
  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)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. int ticket = OrderSend(_Symbol, trade_type, lot_size, close, 3, stoploss, takeprofit, "Sell Order", 0, 0, Red); 

    Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. void OnInit()
      {          
    // Calculate the Exponential Moving Average
       ema = iMA(_Symbol, PERIOD_CURRENT, ema_period, 0, MODE_EMA, PRICE_CLOSE, 0);
    
    // Define the OnTick() function
       void OnTick()
         {          

    You can't define a function (OnTick) inside another (OnInit).

  4. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

Reason: