Fixing AI-Generated MQL5: The Compile Errors Models Make
Short answer: most compile errors in AI-written MQL5 come from three causes — MQL4 functions and constants in an MQL5 file, names the model invented, and output that was cut off before the end. Fix the first error in MetaEditor's list and recompile: one wrong declaration often produces a dozen follow-on errors that disappear with it.
This is the reference for the messages themselves. For why models make these mistakes, see can ChatGPT write an MT5 Expert Advisor?
MQL4 in an MQL5 file
| MetaEditor says | What the AI wrote | Fix |
|---|---|---|
'OP_BUY' - undeclared identifier | MQL4 order type | Use CTrade: trade.Buy(lots, _Symbol, 0, sl, tp) |
'OrderSend' - wrong parameters count | MQL4's 11-argument OrderSend | MQL5's takes a request and result struct — or use CTrade |
'Bid' - undeclared identifier / 'Ask' - ... | MQL4 predefined prices | SymbolInfoDouble(_Symbol, SYMBOL_BID) |
'iRSI' - wrong parameters count | MQL4's iRSI(..., shift) returning a value | Create the handle in OnInit, read with CopyBuffer |
'AccountBalance' - undeclared identifier | MQL4 account function | AccountInfoDouble(ACCOUNT_BALANCE) |
'MarketInfo' - undeclared identifier | MQL4 symbol function | SymbolInfoDouble / SymbolInfoInteger |
'OrdersTotal' loop that never finds a trade | MQL4 position loop | Positions are PositionsTotal() / PositionGetTicket() in MQL5 |
The last row is the dangerous one: OrdersTotal() exists in MQL5 — it counts
pending orders — so the code compiles and the EA never sees its open trades.
Names that do not exist
Models blend real identifiers into plausible fakes. A real one from our logs is
ORDER_FILLING_FOC, a cross between ORDER_FILLING_FOK and ORDER_FILLING_IOC.
The error is undeclared identifier. The best fix is often to delete the whole
line: trade.SetTypeFilling(...) is rarely needed, because CTrade picks a
filling mode the broker accepts, and a hardcoded mode that the broker does not
support fails at runtime with retcode 10030.
Search the MQL5 documentation for any identifier you do not recognise. If it has no page, the model made it up.
Arithmetic and type errors
| MetaEditor says | Cause | Fix |
|---|---|---|
'%' - illegal operation use | % on a double | MathMod(a, b) |
possible loss of data due to type conversion (warning) | A double stored in an int | Declare it double, or cast deliberately |
implicit conversion from 'number' to 'string' (warning) | Numbers concatenated into text | DoubleToString / IntegerToString |
'^' - illegal operation use | ^ used as "power" on a double | MathPow(a, b) — on integers ^ compiles as XOR and is silently wrong |
The file was cut off
| MetaEditor says | Cause | Fix |
|---|---|---|
unexpected end of program | The reply stopped mid-function | Ask for the file in parts, or regenerate |
unbalanced parentheses | Same | Same |
Long EAs can exceed the model's output limit. Check that the last line of the file is a closing brace, not half a statement.
Warnings worth reading
#property strictin an MQL5 file produces a warning, not an error. It is an MQL4 directive and does nothing in MQL5 — delete it.- "return value of 'OrderSend' should be checked" — the EA ignores whether its
order went through. Check the result, and with
CTradeprinttrade.ResultRetcode()when a call returns false.
The reverse problem: MQL5 in an MQL4 file
Asking for MQL4 produces the mirror image: 'CTrade' - undeclared identifier,
'PositionSelect' - function not defined, 'CopyBuffer' - function not defined. MQL4 has none of them. MQL4 trades with OrderSend returning a ticket
and loops with OrderSelect. The full list of differences is in
MQL4 vs MQL5.
A prompt that avoids most of this
Write an MQL5 Expert Advisor for MetaTrader 5. Use
#include <Trade\Trade.mqh>andCTradefor all trading. Create indicator handles in OnInit and read them with CopyBuffer. Use no MQL4 functions or constants. Do not set a filling mode. Filter positions by symbol and magic number. Return the complete file.
Skip the debugging loop
TradeSmithy generates MQL4 and MQL5 from separate, platform-specific instructions,
removes #property strict from MQL5, and refuses output containing the other
language's constructs or known invented constants before you download it. The
strategy lives in a graph the assistant edits, so the code is regenerated from a
structure that was already checked. Start free — one export a month —
or see Pro for the AI assistant. Compare tools in our
EA builder comparison.
Frequently asked questions
Why does my ChatGPT MQL5 code have so many errors?
Usually one root cause — often MQL4 syntax — producing many follow-on errors. Fix the first error and recompile.
What does "undeclared identifier" mean in MQL5?
The name is not defined: an MQL4-only function or constant, a typo, or a name the model invented.
Should I ignore compiler warnings?
Not in trading code. Type-conversion warnings in lot or price calculations can silently round values.
Can I ask the AI to fix its own errors?
Yes — paste the first error with its line and ask for a minimal fix. Recompile after each fix rather than accepting a full rewrite.
Keep reading
Can ChatGPT Write an MT5 Expert Advisor? What Breaks
It can draft MQL5 that compiles — sometimes. The ways AI-written EAs fail, why each happens, and a workflow that catches them before real money does.
5 min readWhy Is My EA Not Trading? A 12-Point MT5 and MT4 Checklist
The EA is attached, the market is moving, and nothing happens. Twelve causes in the order worth checking, with the MT5 and MT4 error codes that name each one.
4 min readMQL5 Wizard vs a Visual EA Builder: Which Should You Use?
The MQL5 Wizard builds a free MT5 EA from Standard Library modules. How its weighted signals work, where it runs out, and when a visual builder fits better.
4 min read