Edgoro
Compile error 161 · MQL5

MQL5 error: unexpected end of program (unbalanced braces)

MetaEditor's "unexpected end of program" and "unbalanced parentheses" errors in MQL5: a missing closing brace, and how to find which one.

MetaEditor · Errors tab · error 161 · line 1
unexpected end of program
unbalanced parentheses

What it means

The compiler reached the end of the file while a block was still open: a { without its }, or a ( without its ). MetaEditor reports error 161, "unexpected end of program", usually with error 133, "unbalanced parentheses". Neither points at the line where the brace is missing.

Usual causes

  • A missing closing brace, often after editing an if or a loop inside a function.
  • A brace or parenthesis lost in a copy-paste, or commented out with the line it was on.
  • An unclosed string or comment that swallows the rest of the file.

How to detect it

  • MetaEditor's Errors tab shows unexpected end of program and unbalanced parentheses. In build 6275 they point at line 1 and at the opening brace of the block that's still open, not at the place where the closing brace should be.
  • Check the blocks of the function the second message points at, one by one: with consistent indentation, the missing } is where the indentation stops making sense.

Broken code

MetaEditor reports unexpected end of program (line 1) and unbalanced parentheses (line 2): the if block's brace is never closed.

MQL5
void OnStart()
  {
   for(int i = 0; i < 3; i++)
     {
      if(i > 0)
        {
         Print(i);
     }
  }

Fixed code

The same program, corrected. It compiles in MetaEditor with 0 errors and 0 warnings.

MQL5
void OnStart()
  {
   for(int i = 0; i < 3; i++)
     {
      if(i > 0)
        {
         Print(i);
        }
     }
  }

Why the fix works

The if block gets its closing brace, at the indentation of its opening brace, and every block is closed again before the end of the file. With one brace per line, aligned with the brace it closes (MetaEditor's own style), a missing one is easy to spot.

Questions

Why does MetaEditor point to line 1 for a missing brace?

Because the compiler only notices the problem when it reaches the end of the file with a block still open, and it can't know where you meant to close it. The "unbalanced parentheses" message points at the opening brace that was never closed, which is the better clue.

How do I find a missing brace in a long MQL5 file?

Start from the block the "unbalanced parentheses" message points at, and check each nested block's closing brace. Commenting out functions one at a time and recompiling narrows it down fast.

The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Rather not write the error handling yourself?

Edgoro's MT5 EA generator writes complete MQL5 Expert Advisors from plain English and compiles each one in MetaEditor before you get it. A clean build proves the code compiles, not that a strategy works: test it on a demo account.