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
ifor 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 programandunbalanced 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.
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.
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.