Can You Trust Trading Code Written by AI?
Asking ChatGPT or another assistant to write an Expert Advisor or a TradingView indicator has become routine. The code usually arrives quickly, looks tidy and often compiles. That is exactly what makes it tempting to run it without a second look.
The question worth asking is not whether AI can write trading code, because it plainly can, but whether the code does what its author believes it does. This article looks at what research says about AI-generated code in general, then at the faults we see most often in AI-written MQL5 and Pine Script, and at how a structured review catches them.
What the Research Says About AI-Written Code
The most cited study is from researchers at New York University, published at the IEEE Symposium on Security and Privacy in 2022. They prompted GitHub Copilot with 89 scenarios tied to high-risk weakness types and found that around 40% of the 1,689 programs it produced were vulnerable (Pearce et al., Asleep at the Keyboard?).
A Stanford user study points at the human side of the problem. Participants with access to an AI assistant wrote significantly less secure code than those without one, and were more likely to believe their code was secure. Those who trusted the assistant less and engaged more carefully with their prompts produced fewer vulnerabilities (Perry et al., Do Users Write More Insecure Code with AI Assistants?).
A third study looked at a quieter fault: invented dependencies. Across 16 code-generating models and 576,000 Python and JavaScript samples, a meaningful share of suggested packages simply did not exist (Spracklen et al., We Have a Package for You!). For trading stacks with a Python side, such as data pipelines or the MetaTrader 5 Python bridge, every import an assistant suggests is worth checking against the real package index before anything is installed.
None of these studies looked at MQL or Pine Script, so their percentages should not be read as error rates for trading code. The lesson that transfers is simpler: AI output needs reviewing, and confidence in it is not evidence that it is correct.
Fault One: MQL4 and MQL5 Mixed Together
MetaTrader 4 and MetaTrader 5 use different languages with different trading functions, and AI assistants often blend the two.
In MQL4, OrderSend() takes the symbol, order type, volume, price, slippage, stop loss, take profit and other values as separate arguments, and returns the ticket number assigned by the trade server, or -1 if it fails (MQL4 Reference, OrderSend). In MQL5, OrderSend() takes an MqlTradeRequest structure and an MqlTradeResult structure and returns true or false (MQL5 Reference, OrderSend).
Code that “looks like MetaTrader” can therefore be a mixture that fails to compile, or that compiles against a custom MQL4-style compatibility library while quietly assuming MQL4 behaviour. The first step in any review is confirming which platform and which API the code actually targets.
Fault Two: Treating “Sent” as “Done”
This is the fault we would most like every EA owner to know about. In MQL5, a true return from OrderSend() does not mean a trade was executed. MetaQuotes states that it only means the basic check of the structures passed, and that it “is not a sign of successful execution of a trade operation”. The result structure has to be read to find out what the trade server actually did (MQL5 Reference, OrderSend).
Generated EAs frequently wrap the call in a simple if statement, treat true as success and never inspect the result’s retcode, for example TRADE_RETCODE_DONE or TRADE_RETCODE_PLACED. The EA then carries on as if a position exists when it may not. MQL5 also provides OrderCheck(), which checks whether there are enough funds for a requested operation before it is sent and reports the outcome in an MqlTradeCheckResult structure (MQL5 Reference, OrderCheck). Missing result handling and missing pre-checks are both concrete, checkable review items.
Fault Three: Pine Script Written for the Wrong Version
Pine Script versions are not interchangeable. TradingView’s own migration guide says some v6 changes are not compatible with v5 scripts, and that scripts from v4 or earlier should be updated one version at a time. In v6, int and float values are no longer implicitly cast to bool, bool values can no longer be na, and the and/or operators evaluate lazily (TradingView, Pine Script v6 migration guide).
An assistant that learned from older examples can write v4 or v5 habits under a //@version=6 header. Sometimes the editor rejects it; sometimes it compiles and behaves differently from what was intended, which is harder to spot.
Fault Four: Indicators That Repaint
Pine scripts run differently on history and in real time. On historical bars a script executes once per bar on confirmed values; on the open realtime bar it executes on every update, and the high, low, close and volume are not final until the bar closes (TradingView, Execution model).
When historical and realtime results differ, TradingView calls it repainting, and says it is widespread. Its documentation describes established fixes: adding a barstate.isconfirmed condition to signals, and requesting higher-timeframe data with a [1] offset together with lookahead = barmerge.lookahead_on, noting that the offset and the lookahead setting depend on each other and neither can be removed on its own (TradingView, Repainting).
Generated code often calls request.security() with neither safeguard, so plots and alerts look one way on the chart’s history and another way live. We touched on what AI can and cannot do inside TradingView in an earlier article; this is the practical side of the same question.
What a Structured Review Looks Like
A useful review of AI-written trading code follows the same order each time. First, confirm the platform, language and version the code targets. Second, compile it cleanly with no warnings left unexplained. Third, read every trade call and confirm that results and return codes are handled, not assumed. Fourth, for Pine Script, check how the script behaves on confirmed versus realtime bars and how any higher-timeframe data is requested. Fifth, check every external dependency is real and necessary. Finally, test the code’s behaviour in the platform’s own tools before it goes anywhere near a live account.
This is the core of our AI Code Rescue work, and it applies equally to code built for our MetaTrader and TradingView clients.
Limits of a Code Review
A review is not a guarantee. It can show that code is correct for the platform and does what its specification says; it cannot say anything about whether the underlying trading idea is a good one, and we never make claims about how a strategy will perform.
Compiling cleanly is also not the same as behaving correctly, which is why review and testing go together. The research cited above dates from 2021 to 2024 and looked at general-purpose code, so newer models may behave differently. It should be read as evidence that review is needed, not as a measure of today’s error rates.
None of this is an argument against AI assistants. They are genuinely useful for scaffolding, for explaining unfamiliar functions and for speeding up routine work. The problem is only ever unreviewed output.
Conclusion
Can you trust trading code written by AI? Not on its own. The faults that matter most, such as mixed MQL4 and MQL5 calls, unchecked trade results, version mismatches and repainting logic, are rarely visible at a glance, and the research suggests that people using assistants are more confident than they should be. A methodical review by someone who knows the platforms turns a fast first draft into code you can rely on.
Drafted with AI assistance; reviewed, edited and approved by Ionut Claudiu Fratila.