Blog

Can ChatGPT Write an MT5 Expert Advisor? What Breaks

TradeSmithy Team5 min read

Short answer: yes, ChatGPT (and Claude, and Gemini) can write an MT5 Expert Advisor that compiles — often on the first or second try for a simple strategy. The problems are the ones that do compile: MQL4 habits inside MQL5, invented constants, indicator handles created on every tick, and trade logic that manages positions the EA never opened. You need to know what to look for, or a structure the model cannot break.

TradeSmithy generates MQL from model output, so the failure list below comes from refusals we have actually logged, not guesswork.

What does AI get right?

For a clearly specified strategy, a current model will usually produce the right skeleton: OnInit, OnTick, OnDeinit, an input block, a CTrade object and a new-bar check. For "buy when the 20 EMA crosses above the 50 EMA, stop 300 points, target 600" you will get something recognisable in seconds.

Where AI-written MQL5 breaks

1. MQL4 inside MQL5

The single most common failure. Most public MQL code online is MQL4, and it leaks in: OrderSend with the MQL4 argument list, OP_BUY, bare Bid and Ask variables, or iRSI(...) used as if it returned a number. In MQL5, iRSI returns a handle; the value comes from CopyBuffer. Some of these fail to compile. The dangerous ones compile and read the wrong value.

We covered the language differences in MQL4 vs MQL5: what actually changes.

2. Constants that do not exist

Models blend real names into plausible fake ones. A real example from our own logs: ORDER_FILLING_FOC — a cross between ORDER_FILLING_FOK and ORDER_FILLING_IOC that exists in no version of MQL5. MetaEditor catches it, but only after you have paid for the generation and copied it across.

Even the correctly spelled version is a trap: hardcoding a filling mode compiles and then fails at runtime on a broker that does not support it. CTrade picks a workable mode by itself; most EAs should never set one.

3. Indicator handles created on every tick

iMA(...) in OnTick creates a new indicator handle on every price quote. It compiles and runs, slowly, and eventually fails. Handles belong in OnInit.

4. No magic-number filter

An EA that loops over every open position and closes "all buys" will close your manual trades and every other EA's trades on the same account. Correct code filters by symbol and magic number. AI output frequently forgets one or both.

5. A file cut off mid-function

Long EAs can exceed the model's output limit. The reply ends mid-line and the code looks complete until MetaEditor reports a missing brace at the bottom.

6. The same prompt, different code

Ask twice and you get two different EAs. If one compiles and one does not, you have learned nothing about your strategy — only about sampling.

7. It does something other than what you meant

The hardest one. "Buy when RSI crosses 30" becomes "buy whenever RSI is below 30" — every tick, until the account is full of positions. It compiles and it trades. It just is not your strategy.

A workflow that catches these

  1. Write the rules as a list before prompting: entry, exit, stop, target, position sizing, maximum open trades, timeframe.
  2. Name the platform explicitly — "MQL5 for MetaTrader 5, using CTrade" — and ask for no MQL4 functions.
  3. Compile, then read the warnings, not just the errors.
  4. Backtest on a period you did not design the strategy on, with realistic spread.
  5. Forward test on demo for at least a few weeks before a live account.

Why a graph is a better target for AI than a text file

The weakness in every failure above is that the model writes free text, and free text can say anything. TradeSmithy's assistant does not write MQL; it edits your strategy graph. Every node it adds and every wire it draws is checked against the same rules the canvas enforces — an unknown connection or an illegal value is rejected before it reaches your screen. The MQL is then generated separately for MQL4 or MQL5 and checked for the other language's constructs and for known invented constants. When you backtest, an unchanged graph reuses the source it already generated instead of rolling the dice again, so two runs of the same strategy test the same code.

More on the envelope it works inside: what the AI assistant can and cannot change.

Try the AI assistant on your own strategy

Create a free TradeSmithy account and build the strategy on the canvas from a template — the Free plan includes the full builder and one MQL export a month. The AI assistant, which edits the graph for you, and server-side backtests are on Pro at $19 per 30 days.

Frequently asked questions

Can ChatGPT write a profitable trading bot?

It can write the code for whatever strategy you describe. Whether that strategy is profitable has nothing to do with the model — test it like any other.

Why does my ChatGPT EA not compile?

Most often MQL4 syntax in an MQL5 file, a function or constant that does not exist, or output that was cut off. Paste the first error back and ask for a minimal fix.

Is Claude or ChatGPT better at MQL5?

Both make the same classes of mistake. The model matters less than the process around it: precise rules, compiling, and testing.

Can AI convert an MQL4 EA to MQL5?

It can help, but order handling and indicator access differ enough that the converted EA must be retested from scratch.

Keep reading

← More from the TradeSmithy blog