Quick Answer
The MetaEditor errors '}' - unexpected end of program and '{' - unbalanced parentheses nearly always appear together, and they mean one thing: a { has no matching } (or the other way round), so the compiler reaches the end of the file while a block is still open. In forum threads, the cause was a missing closing brace of a function or of an if, a stray character such as a lone /, braces of a for loop in the wrong order, or a missing semicolon before the braces.
The method has four steps. Auto-format the file (MetaEditor: Tools, then Styler), so that the indentation shows where the block structure breaks. Read the first error and the line it names, not the last. Fix one thing, compile again, because many later messages are results of the first one. And check the platform: pasted MQL4 code gives more errors in an MQL5 file. This post shows a broken EA, the fixed one, the follow-up errors that disappear, and a small script that counts the braces in a piece of code.
From the author: I fix and convert EAs for a living. Most "unexpected end of program" cases that I see are not hard: one brace is missing, and 20 other messages hide it. The fastest way is to stop reading the list and format the file.
Key Facts
| What you see | Usual cause | What to do |
|---|---|---|
| '}' - unexpected end of program | An opening { was never closed | Find the block that has no } |
| '{' - unbalanced parentheses | The compiler counts blocks that do not close | Same problem, from the other side |
| 'OnTick' - function already defined and has body | A brace was missing before it, so it looks nested or duplicated | Fix the brace above first |
| 'OnTick' - function can be declared only in the global scope | A function starts inside an open block | A } is missing before this function |
| Errors that change after each fix | Later messages depend on earlier ones | Compile after every single fix |
| A / or other stray character after return | A syntax error that confuses the block structure | Remove it |
The messages and causes in this table come from compile questions on the MQL5 forum, including one where the author's EA showed '}' - unexpected end of program at line 164 and '{' - unbalanced parentheses at line 41.
A Broken EA
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
Print("Stopped, reason ", reason);
// the closing brace of OnDeinit is missing
void OnTick()
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if(ask > 0)
{
Print("Ask: ", ask);
// the closing brace of the if is missing too
}
The compiler sees void OnTick() inside OnDeinit, which is not allowed. Typical output: 'OnTick' - function can be declared only in the global scope, then '}' - unexpected end of program, then '{' - unbalanced parentheses. Three messages, two missing braces.
The Fixed EA
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
Print("Stopped, reason ", reason);
}
void OnTick()
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if(ask > 0)
{
Print("Ask: ", ask);
}
}
Every { has a } at the same indentation. That is also the rule that makes the problem visible, as the next section shows.
Step 1: Auto-Format the File
In one forum thread, the first answer to a poster with these two errors was to indent the code properly with the editor's auto-indent, Tools, then Styler. Another thread gave the same advice: with the styler on, the error is easier to find. After formatting, a block that never closes pushes every following line to the right, and a block that closes too early leaves lines at the left edge. You can see the problem in seconds.
Step 2: Read the First Error, Not the Last
A thread says it in one line: after the missing brace is fixed, you now have to fix all the other errors. This is normal. A missing brace makes the compiler read later functions as part of an earlier block, so it reports problems in code that is fine. Start from the first message in the list and the first line that it names.
Step 3: Look for the Small Syntax Mistakes Too
The answers in the threads found more than missing braces:
| Found in a thread | Why it breaks the structure | Fix |
|---|---|---|
| A stray / after return 1; | Not valid syntax | Delete it |
| A for loop with no body, braces in the wrong order | The braces close and open in the wrong places | Put { after the for(...) and } at the end of the body |
| A line missing its semicolon | The next line is read as part of it | Add the ; |
| double x == value; | == is a comparison, not an assignment | Use = |
| Names that differ in case or spelling | Different names, an undeclared identifier | See undeclared identifier |
| A comparison stored in a double | The result is a bool | Use a bool variable |
A moderator in one of the threads also found that the original start() function was never closed, that a stray { opened a block that was never used, and that an if block lacked its closing bracket.
Step 4: Check the Platform
Two threads show the same trap: the code was written for MQL4 and compiled as MQL5, or it was posted in the wrong section. MQL4 trade functions such as OrderSend with a different signature do not exist in MQL5, so fixing the braces uncovers new errors. If the file is from MQL4, see wrong parameters count and PositionSelectByIndex for the next messages that you may meet.
A Script That Counts Braces
This script does not trade. It counts the { and } characters in a text and prints the difference. It does not skip braces inside strings or comments, so it is only a first check:
#property strict
void CountBraces(const string label, const string src)
{
int open = 0, close = 0;
for(int i = 0; i < StringLen(src); i++)
{
ushort c = StringGetCharacter(src, i);
if(c == '{') open++;
if(c == '}') close++;
}
PrintFormat("%s: { = %d, } = %d, difference = %d", label, open, close, open - close);
}
void OnStart()
{
CountBraces("broken", "void A() { if(x) { y(); } void B() { z(); }");
CountBraces("fixed", "void A() { if(x) { y(); } } void B() { z(); }");
}
The log should show broken: { = 3, } = 2, difference = 1 and fixed: { = 3, } = 3, difference = 0. A difference that is not zero tells you that a brace is missing (positive) or extra (negative). In your own file, paste a function at a time and find the one that does not balance.
The Indicator Handle Tip From the Same Thread
In the thread with the trailing stop EA, an answer pointed to a second problem that has nothing to do with braces: the indicator handle was created in OnTick, on every tick. An indicator handle is created once, in OnInit(), stored in a global variable, and used in OnTick() with CopyBuffer. See MQL5 CopyBuffer guide and MQL5 trailing stop EA for the full pattern.
Similar Compile Messages
| Message | What it means | Where to look | |---|---|---| | Unexpected end of program, unbalanced parentheses | A block is not closed | This post | | Variable already defined | The same name is declared twice in a scope | Variable already defined | | Undeclared identifier | A name the compiler does not know | Undeclared identifier | | Wrong parameters count | A call has too few or too many arguments | Wrong parameters count | | Implicit conversion warnings | A type is converted without a cast | Possible loss of data |
Checklist: What to Check When You See These Errors
- Run Tools, Styler on the file.
- Read the first error and its line.
- Look for a block that opens and does not close, or closes in the wrong place.
- Check for stray characters and missing semicolons near that line.
- Fix one thing and compile again.
- Check the platform: is this MQL4 code?
- Count the braces of each function if the cause is still not clear.
- Test the EA on a demo account when it compiles.
Common Mistakes
- Reading the error list from the bottom.
- Fixing five things and compiling once.
- Pasting unformatted code into a forum or a tool.
- Deleting a brace to make an error disappear.
- Compiling MQL4 code as MQL5.
- Creating indicator handles in
OnTick.
FAQ
Related Guides
- MQL5 variable already defined: the same name declared twice.
- MQL5 undeclared identifier: a name the compiler does not know.
- MQL5 wrong parameters count: a call with the wrong number of arguments.
- MQL5 possible loss of data due to type conversion: a type warning that often comes next.
- MQL5 PositionSelectByIndex undeclared identifier: a function that does not exist.
- MQL5 OnCalculate not found: an indicator that needs its entry point.
- MQL5 CopyBuffer guide: create the handle once, read the buffer later.
- MQL5 trailing stop EA: a full EA with a clean structure.
- MQL5 return value of OrderSend should be checked: store the result and read
result.retcode.
Fix Compile Errors Without Guessing
If an Expert Advisor does not compile, paste the code and the error messages into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. The Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.