The 12 errors an AI makes writing MQL5 and Pine Script
If you asked a language model for an Expert Advisor and MetaEditor gave you a wall of red, it is not bad luck: it is almost always the same twelve failures. This list was not written from memory or gathered from forums — every entry came from generating the code and actually compiling it, the MQL5 ones with MetaEditor and the Pine ones in TradingView's editor.
How this list was produced
Dozens of indicators and EAs were generated with a model, compiled one by one, and the log was read. The lists of valid constants behind them were derived by testing each candidate against the compiler; the CTrade methods came from MQL5/Include/Trade/Trade.mqh, the authoritative source. That is why each error carries its literal message: it is what you will paste into a search box.
The ones that give no error are the expensive ones
Of the twelve, these fail silently: they compile, they run, and they do something other than what you think. There is no red line to follow, so they are found late or not at all.
1 of 12.
- 1 · MQL5
PLOT_COLOR_INDEX: a constant that does not exist, by one letter
What comes out
PlotIndexSetInteger(0, PLOT_COLOR_INDEX, clrRed);What it should be
PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrRed);What the compiler says: 'PLOT_COLOR_INDEX' - undeclared identifier. Three occurrences produced 9 compile errors.
Why an AI writes it: PLOT_COLOR_INDEXES does exist, in the plural, but it means something else: how many colours the plot has, not which one. For the line colour the property is PLOT_LINE_COLOR. The singular sounds so reasonable that the model writes it without hesitating.
- 2 · MQL5
trade.SetMagicNumber(): the method that sounds right and does not exist
What comes out
CTrade trade; trade.SetMagicNumber(20260806);What it should be
CTrade trade; trade.SetExpertMagicNumber(20260806);What the compiler says: 'SetMagicNumber' - no member of 'CTrade'.
Why an AI writes it: It is the perfect example of why generating once does not catch this: in the same batch, 10 of 11 files used the correct name. It is an INTERMITTENT failure of around 15%, and the rest of the EA is usually flawless.
- 3 · MQL5
Reading a symbol property with the wrong function family
What comes out
double stops = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);What it should be
long stops = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL); // Si lo necesitas como double: (double)SymbolInfoInteger(...)What the compiler says: cannot convert enum, together with wrong parameters count. Two occurrences produced 4 errors.
Why an AI writes it: Each symbol property belongs to ONE family (double, integer or string) and must be read with its function. The minimum stop distance is an integer, even though one thinks of it as a price. This came up during an ITERATION, where the model edits existing code and pays less attention to types.
- 4 · MQL5
Using iMA() or iRSI() as in MQL4, without CopyBuffer
What comes out
double ma = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE, 1);What it should be
// En OnInit(), UNA vez: int maHandle = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE); // Al leer: double buf[]; ArraySetAsSeries(buf, true); CopyBuffer(maHandle, 0, 1, 1, buf); double ma = buf[0];What the compiler says: wrong parameters count, or a double that actually holds a handle number and produces absurd figures.
Why an AI writes it: In MQL4 these functions returned the VALUE and took a shift parameter. In MQL5 they return a HANDLE that must be created once and read with CopyBuffer. It is the most frequent error of all, because most trading material online is MQL4.
- 5 · MQL5
Ask and Bid as global variables
What comes out
double price = Ask;What it should be
double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // o, mejor, un tick completo: MqlTick tick; SymbolInfoTick(_Symbol, tick);What the compiler says: 'Ask' - undeclared identifier.
Why an AI writes it: They were predefined in MQL4 and are gone in MQL5. Same cause as the previous one: the training corpus is full of MQL4.
- 6 · MQL5
The whole MQL4 order model: OP_BUY, OrderLots(), OrderTicket()
What comes out
OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0); double lots = OrderLots();What it should be
CTrade trade; // #include <Trade/Trade.mqh> trade.Buy(0.1, _Symbol); if (PositionSelect(_Symbol)) double lots = PositionGetDouble(POSITION_VOLUME);What the compiler says: 'OP_BUY' - undeclared identifier, 'OrderLots' - undeclared identifier, and a cascade behind them.
Why an AI writes it: MQL5 changed the whole model: instead of an order list traversed with OrderSelect there are positions, orders and history, separately. When a model starts here, the whole file comes out in MQL4.
- 7 · MQL5
CTrade without its #include
What comes out
CTrade trade; // y ninguna directiva arribaWhat it should be
#include <Trade/Trade.mqh> CTrade trade;What the compiler says: 'CTrade' - undeclared identifier.
Why an AI writes it: The model writes the class because it has seen it a thousand times, but the include lives on the first line and it is generating from the middle. The easiest to fix on this list, and one of the most common.
- 8 · Pine ScriptFails with no error
Pine v6: the backtest comes out with ZERO trades and not one error
What comes out
strategy("Mi estrategia", overlay = true)What it should be
strategy("Mi estrategia", overlay = true, margin_long = 0, margin_short = 0)What the compiler says: None. It compiles, applies to the chart, and the trade list is empty.
Why an AI writes it: In Pine v5 the default margin was 0 and leaving it alone was enough. In v6 the default became 100, which demands 100% margin and REJECTS any leveraged position. The costliest error here precisely because it is silent: it lets you conclude your strategy has no signals when in fact no order is accepted.
- 9 · Pine Script
The alertcondition() message must be a CONSTANT string
What comes out
alertcondition(cruce, "RSI", "RSI cruzó " + str.tostring(nivel))What it should be
alertcondition(cruce, "RSI", "RSI cruzó el nivel en {{ticker}} {{interval}}") // Para texto realmente calculado: alert(), dentro de un if.What the compiler says: An argument of "simple string" type was used but a "const string" is expected (CE10123).
Why an AI writes it: The irony of the real case: the model ALREADY used TradingView's placeholders and also concatenated, which is exactly what breaks it. The alert is registered at compile time, so its text must be known then; alert() runs at execution and does accept computed strings.
- 10 · Pine Script
Indicators without the ta. namespace
What comes out
media = sma(close, 20) señal = crossover(close, media)What it should be
media = ta.sma(close, 20) señal = ta.crossover(close, media)What the compiler says: Could not find function or function reference 'sma'.
Why an AI writes it: Indicators moved to the ta. namespace in v5. As with MQL4, most examples out there are v4, so the model reproduces them.
- 11 · Pine Script
Parameters removed in v6: when= and transp=
What comes out
strategy.entry("L", strategy.long, when = cruce) plot(media, color = color.blue, transp = 40)What it should be
if cruce strategy.entry("L", strategy.long) plot(media, color = color.new(color.blue, 40))What the compiler says: Unknown argument 'when' / Unknown argument 'transp'.
Why an AI writes it: A model trained on v5 keeps writing them. Also watch the other v6 changes that do NOT error: and/or became lazy, so a ta.* inside a condition can stop running on some bars and skew the whole series. Assign ta.* to variables BEFORE the condition.
- 12 · Pine Script
A named argument missing its =
What comes out
entrada = input.source(close, title "Fuente de precio")What it should be
entrada = input.source(close, title = "Fuente de precio")What the compiler says: Syntax error at input '...'.
Why an AI writes it: It almost always shows up in calls split over several lines, where the eye misses it. A single lost equals sign in a 300-line script, and the error points at the line without saying what is missing.
What this guide does not do
It does not tell you whether your strategy is any good, or whether it will make money. It is only about the code doing what it says it does: these are programming errors, not trading ones. And it is not a closed list — it is the one we can demonstrate by compiling. If you hit one that is missing, write to us and we will check it.
Where this comes from
These twelve failures are the rules of a validator that runs over all the code SuaVar generates, before showing it to you. The list is yours and needs no sign-up. If you want to try the tool, the free plan includes one indicator and one EA generation a month, no card.