Why AI-Generated MQL5 Won't Compile (and How to Fix It)

Published 2026-09-13 · Ranar Algo

Large language models are great at sketching a trading idea, but paste their MQL5 into MetaEditor and it often won't compile. Here are the five failures we see most, and how to fix each.

1. MQL4 syntax in an MQL5 file

The classic tell is a single-line indicator call with a shift argument, e.g. iMA(_Symbol, 0, 14, 0, MODE_EMA, PRICE_CLOSE, 0) used directly in a comparison. That is MQL4. In MQL5 you create a handle once, then copy values.

2. Missing indicator handles

MQL5 indicators are handle-based. Create the handle in OnInit, read it with CopyBuffer in OnTick, and release it in OnDeinit.

int emaHandle;
int OnInit(){ emaHandle = iMA(_Symbol, _Period, 200, 0, MODE_EMA, PRICE_CLOSE); return INIT_SUCCEEDED; }
void OnTick(){
  double ema[]; ArraySetAsSeries(ema, true);
  if(CopyBuffer(emaHandle, 0, 0, 2, ema) < 2) return;
  // use ema[0], ema[1]
}
void OnDeinit(const int r){ IndicatorRelease(emaHandle); }

3. No new-bar detection

Without it, the EA acts on every tick and fires dozens of orders per bar. Guard on the bar time:

datetime lastBar = 0;
bool NewBar(){ datetime t = iTime(_Symbol, _Period, 0); if(t == lastBar) return false; lastBar = t; return true; }

4. Wrong order-filling mode

OrderSend fails silently on some brokers if the filling mode is unsupported. Use CTrade from <Trade/Trade.mqh> and let it manage filling, or set type_filling to a mode the symbol allows.

5. Array indexing without ArraySetAsSeries

MQL5 arrays are not series-indexed by default, so array[0] may be the oldest bar, not the newest. Call ArraySetAsSeries(arr, true) before you read [0] as "current".

The shortcut

Fixing these by hand is the slow part. Ranar Algo generates the MQL5 from a plain-English strategy, compiles it against real MetaEditor in a loop until it builds clean, then runs a real MetaTrader 5 Strategy Tester backtest — so you skip the compile-error churn. You keep your IP and download the source. It's still improving; feedback welcome.

Build & backtest your own EA free →

Ranar Algo turns a plain-English strategy into a compiled MQL5 Expert Advisor with a real MetaTrader 5 backtest. You keep your IP. Past performance does not guarantee future results.