Quick Answer
"Variable already defined" is a MetaEditor compile error. It means that, in one place in your code, you declared a name that already exists in the same scope. The compiler points at the second declaration and refuses to build.
The cause is almost always one of three things: you declared the same variable twice in one function (often with a different type, such as int total first and double total later), you pasted two blocks of code that each declare lots, ticket or i, or you declared a variable in the middle of a function and forgot that you already did it at the top. The fix is to declare each name once per scope, and after that only assign to it. You cannot "change the type" of a variable by declaring it again.
From the author: I build and convert Expert Advisors. This error shows up most when a strategy is assembled from pieces: an entry block from one source, a risk block from another, and both start with
double lots = ...;. The compiler is right to stop, and the fix takes a minute once you know what to look for.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| "variable already defined" on a line double total... | total was declared earlier in the same function | Remove the type, assign only: total = ...; |
| The earlier declaration has another type (int) | You tried to change the type by declaring again | Choose one type, or use a new name |
| Error after you pasted a block | Both blocks declare the same name | Rename one, or wrap each block in { } |
| Error on i in two for loops | Usually not an error, the counters are in their own scope | Check the loops are not nested or sharing one scope |
| Error on a function parameter | A local variable has the same name as a parameter | Rename the local variable |
| Error only after you added an input | An input and a local have the same name | Rename one of them |
What the Error Looks Like
A forum thread on this error shows the pattern clearly. The code declared total as an int at the top of OnTick(), and later a double declaration list also contained total. The compiler reported "variable already defined". As one of the answers puts it, you can't define it a second time or change its type to double. The fix is to declare each variable once and assign total later.
In MetaEditor you will see something like this in the Errors tab, with the file and the line (the exact wording and the line numbers depend on your build):
'total' - variable already defined
The Broken Code
This function declares total twice in the same block:
void OnStart()
{
int total = 5;
double price = 1.1050,
lots = 0.10,
total = price * lots; // 'total' - variable already defined
Print(total);
}
The first int total and the second double total are in the same scope: the body of OnStart. The compiler does not treat the second as a new variable, and it does not let you replace the first.
Cause 1: A Name Declared Twice in One Function
The most common case. Look above the line that the compiler marks, in the same function, for the same name. The fix is to declare the variable only once:
void OnStart()
{
double price = 1.1050;
double lots = 0.10;
double total = price * lots;
Print(total);
}
If the first total is still needed (a different meaning), give the second a name that says what it is: positionValue, riskAmount.
Cause 2: Declare Once, Then Assign
When a value changes during the function, you declare the variable at the first use and assign to it later, without the type:
double lots = 0.10;
// ...
lots = NormalizeDouble(lots, 2); // assign, do not write "double lots" again
A typical mistake in a long OnTick: the top declares double lots; and the middle of the function repeats double lots = CalculateLots();.
Cause 3: Two Pasted Blocks
Two blocks that were fine on their own:
// entry block
double lots = 0.10;
trade.Buy(lots, _Symbol);
// risk block, pasted below
double lots = RiskLots(0.01, 50); // variable already defined
There are three fixes. Rename one name, reuse the first variable (lots = RiskLots(...);), or put each block in its own { } so that each has its own scope:
{
double lots = 0.10;
trade.Buy(lots, _Symbol);
}
{
double lots = RiskLots(0.01, 50);
trade.Buy(lots, _Symbol);
}
Use the third option with care. Two variables with the same name in separate blocks are legal, but a reader of the code may not see which lots is meant. A new name is easier to read.
Cause 4: Scope and for Loops
A for loop that declares its counter, for(int i = 0; ...), gives the counter a scope that lasts only for the loop. Two such loops one after the other can both use i:
for(int i = 0; i < 3; i++) total += i;
for(int i = 0; i < 4; i++) total += i; // fine: a new i
If the compiler marks a loop counter, look for a declaration of the same name above it in the same block, for example int i; at the top of the function, or two counters declared in one plain block. When i is declared once outside, the loops should use for(i = 0; ...) without int. Check the exact behavior in your build: the message and the line tell you which declaration the compiler sees first.
Note that a local variable with the same name as a global variable or an input is a different case. In MQL5 this usually gives a warning that the declaration hides another one (check the wording in your build), not the "already defined" error. Still, rename it, because a hidden global is a source of wrong values.
A Full EA That Shows the Fixed Scope
This EA does not trade. It prints the result of two loops that both use i and of one variable that is declared once and then assigned:
#property strict
int OnInit()
{
int total = 0;
for(int i = 0; i < 3; i++)
total += i; // 0 + 1 + 2 = 3
for(int i = 0; i < 4; i++)
total += i; // 0 + 1 + 2 + 3 = 6, so total is 9
double average = total / 2.0; // a new name, declared once
average = NormalizeDouble(average, 1); // assigned, not declared again
PrintFormat("total=%d average=%.1f", total, average);
return INIT_SUCCEEDED;
}
The output should be total=9 average=4.5. To see the error, add double total = 1.5; below the first line of OnInit and compile: MetaEditor reports that the variable is already defined.
How to Find the First Declaration Fast
- Read the name in the message and the line number.
- Search the function for that name (Ctrl+F in MetaEditor), starting from the top of the function.
- Check the scope: is the first declaration in the same block, or in an outer block that is still open?
- Check the parameters of the function: a parameter is a declaration too.
- Decide: one variable (assign later), two variables (rename), or two blocks (add braces or rename).
Similar Compile Messages
| Message | What it means | Where to look | |---|---|---| | Variable already defined | A name is declared twice in one scope | This post | | Undeclared identifier | A name that the compiler does not know | MQL5 undeclared identifier | | Wrong parameters count | A call with too few or too many arguments | MQL5 wrong parameters count | | Possible loss of data | A type conversion warning | MQL5 possible loss of data | | CS0136 in cTrader | The same idea in C#: a name used twice | cTrader CS0136 |
Checklist: What to Check When You See "Variable Already Defined"
- Find the first declaration of the name in the same function.
- Declare once, then assign without the type.
- Do not change a type by declaring again: use a new name.
- Rename one of two pasted blocks' variables, or give each block braces.
- Check the loop counters, and a counter that is declared outside.
- Check parameters and inputs for the same name.
- Compile again and read the next message, because one error can hide another.
- Run the EA on a demo account before it trades with real money.
Common Mistakes
- Declaring a variable again to "reset" it.
- Pasting two blocks without checking their names.
- Reusing a name with a different type.
- Naming a local variable like a parameter or an input.
- Fixing the line and not reading the rest of the error list.
- Using short names such as
i,nandpfor different things in a long function.
FAQ
Related Guides
- 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: a type conversion warning.
- MQL5 array out of range: an index outside the array.
- MQL5 lot size calculation: one place for the lot size, so that it is declared once.
- cTrader CS0136: the same kind of name conflict in C#.
- Pine Script to MQL5 guide: converting a script step by step.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
- MQL5 PositionSelectByIndex undeclared identifier: a position function that does not exist, and the loop to use instead.
- MQL5 unexpected end of program, unbalanced parentheses: find the missing brace.
Fix Compile Errors Without Guessing
If an Expert Advisor does not compile, paste the code and the error message 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. 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.