"Unexpected end of program" and "Unbalanced parentheses" Expert Advisor Problem! - page 3

 
Hello MQL4 Community. Can you please assist me with the  expert advisor code above. I am fairly new to coding so your support will be highly appreciated.
 
OTMT Howard #:
Run your code with the styler. Then you would see that the last posted close bracket belongs to if statement after the OrderSelect. Missing one for the for, if OrdersTotal, and TrailOrder.
 
Thank you very much William Roeder.

If you don't mind me asking for more support. I would like to ask you and anyone in the Mql4 Community for assistance with my day of week conditions in MQL4. I want to add this condition check as follows.

I did my code research and managed to find the weekday limits from:

"https://www.earnforex.com/guides/optimizing-metatrader-expert-advisor-trade-certain-days-week/"

//+--------------------------------------------------------------------------------------------
 if(Hour() == StartHour) // Check  if((DayOfWeek()==0 && Sunday==true and 1 through 7.
     {
      comment = GetDateAndTime();

Code conditions:

I set as extern bool (Global Var)

input bool Sunday    = true;
input bool Monday    = true;
input bool Tuesday   = true;
input bool Wednesday = true;
input bool Thursday  = true;
input bool Friday    = true;
input bool Saturday  = true;

Optimize based on day of week

if (((TimeDayOfWeek(TimeCurrent()) == 0) && (Sunday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 1) && (Monday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 2) && (Tuesday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 3) && (Wednesday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 4) && (Thursday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 5) && (Friday)) ||
    ((TimeDayOfWeek(TimeCurrent()) == 6) && (Saturday)))
    {
        Entry(); 
    }

Mql4 Date condition:

MqlDateTime dt;
TimeToStruct(TimeCurrent(), dt);

Time position and condition:

if (((dt.day_of_week == 0) && (Sunday)) ||
    ((dt.day_of_week == 1) && (Monday)) ||
    ((dt.day_of_week == 2) && (Tuesday)) ||
    ((dt.day_of_week == 3) && (Wednesday)) ||
    ((dt.day_of_week == 4) && (Thursday)) ||
    ((dt.day_of_week == 5) && (Friday)) ||
    ((dt.day_of_week == 6) && (Saturday)))
    {
        Entry();
    }
//+--------------------------------------------------------------------------------------------


 
nuubsterkill #:
Thank you :) Had do adjust two other things aswell and got it to run without any errors :)

HELLO im having the same issue , unexpected end of programm and this same parenthesis problem

Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
  • 2022.05.07
  • www.mql5.com
MQL5: linguagem de estratégias de negociação inseridas no Terminal do Cliente MetaTrader 5. A linguagem permite escrever seus próprios sistemas automáticos de negócios, indicadores técnicos, scripts e bibliotecas de funções
Files:
 
Kpeji Elisha #: unexpected end of programm and this same parenthesis problem

if openParenthese conditions closeParenthese

Don't post pictures of code, they are too hard to read.

Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
          Messages Editor

 

'unbalanced parenthese' and 'unexpected end of program' on mql5 

this is the code:

// Include necessary libraries

#include <Trade\Trade.mqh>

#include <Indicators\Trend.mqh>



// Indicator parameters

input int Period = 100;



// Trading variables

double Trend;

int ticket;

bool tradeOpened = false;

double StopLoss = 30.0;

double TakeProfit = 50.0;

int MagicNumber = 12345;



// Calculate the moving average on each tick

void OnTick()

{

// Get the current time

datetime time = TimeCurrent();



Copy code

// Calculate the moving average

Trend = iTrend(NULL, 0, Period, MA_METHOD_EMA, PRICE_CLOSE, 0);



// Check if it's between 7:00 and 9:00 New York time

if (time >= TimeCurrent() - (GetTimeZone() * 60) + TimeHour(7) &&

    time <= TimeCurrent() - (GetTimeZone() * 60) + TimeHour(9))

{

    // Check if a trade is already open

    if (!tradeOpened)

    {

        // Check if the price is above the moving average

        if (Ask > Trend)

        {

            // Open a buy trade

            ticket = OrderSend(NULL, ORDER_TYPE_BUY, 1.0, Ask, 10, Ask - StopLoss * Point, Ask + TakeProfit * Point, "My trade", MagicNumber, 0, Green);



            // Check if the trade was successful

            if (ticket > 0)

            {

                tradeOpened = true;

            }

        }

        // Check if the price is below the moving average

        else if (Ask < Trend)

        {

            // Open a sell trade

            ticket = OrderSend(NULL, ORDER_TYPE_SELL, 1.0, Ask, 10, Ask + StopLoss * Point, Ask - TakeProfit * Point, "My trade", MagicNumber, 0, Red);



            // Check if the trade was successful

            if (ticket > 0)

            {

                tradeOpened = true;

            }

        }

    }

}

// Check if it's after 9:00 New York time

else if (time > TimeCurrent() - (GetTimeZone() * 60) + TimeHour(9))

{

    // Check if a trade is open

    if (tradeOpened)

    {

        // Close the trade

        if (OrderClose(ticket, 1.0, Ask, 10))

        {

            tradeOpened = false;

        }

    }

       }

 
Amine Asaad #: 'unbalanced parenthese' and 'unexpected end of program' on mql5 this is the code:

Please note that the line ... " #include <Trade\Trade.mqh> " is for MQL5 not MQL4.

Do you know how to code in MQL4?

Also, please edit your post (don't create a new one) and post your code properly ...

 
Amine Asaad #: this is the code:

  1. if (time >= TimeCurrent() - (GetTimeZone() * 60) + TimeHour(7) &&
    

    TimeHour takes a datetime and returns the hour. Seven (7) is not a dattime.

  2.            ticket = OrderSend(NULL, ORDER_TYPE_SELL, 1.0, Ask, 10, Ask + StopLoss * Point, Ask - TakeProfit * Point, "My trade", MagicNumber, 0, Red);
    

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

  3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
Amine Asaad #: 'unbalanced parenthese' and 'unexpected end of program' on mql5 this is the code:

You posted in the MQL4 section but mention MQL5 — which is it — MQL4 or MQL5?

Except for the include, your code is mostly MQL4 (trade functions), not MQL5.

It is important that you learn to style your code properly and not have so much empty space. Use the built-in code styler in MetaEditor to help you with this.

Pride in making your code look "beautiful" helps to be able to read it more easily and find errors in your code and logic ...

// Include necessary libraries
// #include <Trade\Trade.mqh> // <- This is only available in MQL5
#include <Indicators\Trend.mqh>

// Indicator parameters
input int Period = 100;

// Trading variables
double Trend;
int ticket;
bool tradeOpened = false;
double StopLoss = 30.0;
double TakeProfit = 50.0;
int MagicNumber = 12345;

// Calculate the moving average on each tick
void OnTick()
{
// Get the current time
   datetime time = TimeCurrent();

// Copy code // <- This should be a comment and not code

// Calculate the moving average
   Trend = iTrend(NULL, 0, Period, MA_METHOD_EMA, PRICE_CLOSE, 0);

// Check if it's between 7:00 and 9:00 New York time
   if (time >= TimeCurrent() - (GetTimeZone() * 60) + TimeHour(7) &&
             time <= TimeCurrent() - (GetTimeZone() * 60) + TimeHour(9))
   {
      // Check if a trade is already open
      if (!tradeOpened)
      {
         // Check if the price is above the moving average
         if (Ask > Trend)
         {
            // Open a buy trade
            ticket = OrderSend(NULL, ORDER_TYPE_BUY, 1.0, Ask, 10, Ask - StopLoss * Point, Ask + TakeProfit * Point, "My trade", MagicNumber, 0, Green);

            // Check if the trade was successful
            if (ticket > 0)
            {
               tradeOpened = true;
            }
         }
         else // Check if the price is below the moving average
            if (Ask < Trend)
            {
               // Open a sell trade
               ticket = OrderSend(NULL, ORDER_TYPE_SELL, 1.0, Ask, 10, Ask + StopLoss * Point, Ask - TakeProfit * Point, "My trade", MagicNumber, 0, Red);

               // Check if the trade was successful
               if (ticket > 0)
               {
                  tradeOpened = true;
               }
            }
      }
   }
   else // Check if it's after 9:00 New York time
      if (time > TimeCurrent() - (GetTimeZone() * 60) + TimeHour(9))
      {
         // Check if a trade is open
         if (tradeOpened)
         {
            // Close the trade
            if (OrderClose(ticket, 1.0, Ask, 10))
            {
              tradeOpened = false;
            }
         }
      }
} // <- Missing closing brace for OnTick function

However, now that the missing brace has been fixed, you now have to fix all the other errors ...


 

Code Error generated by the coded copied into MQL4 editor are as follows..;' - unexpected end of program & unbalanced parentheses  - Kindly see assist in fixing this errors - See detail source code below:

<Incorrectly posted code deleted>

Reason: