Blog

Fixing AI-Generated MQL5: The Compile Errors Models Make

TradeSmithy Team5 min read

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 saysWhat the AI wroteFix
'OP_BUY' - undeclared identifierMQL4 order typeUse CTrade: trade.Buy(lots, _Symbol, 0, sl, tp)
'OrderSend' - wrong parameters countMQL4's 11-argument OrderSendMQL5's takes a request and result struct — or use CTrade
'Bid' - undeclared identifier / 'Ask' - ...MQL4 predefined pricesSymbolInfoDouble(_Symbol, SYMBOL_BID)
'iRSI' - wrong parameters countMQL4's iRSI(..., shift) returning a valueCreate the handle in OnInit, read with CopyBuffer
'AccountBalance' - undeclared identifierMQL4 account functionAccountInfoDouble(ACCOUNT_BALANCE)
'MarketInfo' - undeclared identifierMQL4 symbol functionSymbolInfoDouble / SymbolInfoInteger
'OrdersTotal' loop that never finds a tradeMQL4 position loopPositions 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 saysCauseFix
'%' - illegal operation use% on a doubleMathMod(a, b)
possible loss of data due to type conversion (warning)A double stored in an intDeclare it double, or cast deliberately
implicit conversion from 'number' to 'string' (warning)Numbers concatenated into textDoubleToString / IntegerToString
'^' - illegal operation use^ used as "power" on a doubleMathPow(a, b) — on integers ^ compiles as XOR and is silently wrong

The file was cut off

MetaEditor saysCauseFix
unexpected end of programThe reply stopped mid-functionAsk for the file in parts, or regenerate
unbalanced parenthesesSameSame

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 strict in 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 CTrade print trade.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> and CTrade for 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

← More from the TradeSmithy blog