To create a real forex account for free, click here
AI trading expert advisors are here — but you still need to test and refine them.
If you’ve already used AI tools like Claude or ChatGPT to create an expert advisor for Deriv MT5 or a cBot for Deriv cTrader, you’re off to a great start. But even when the code looks right, it can still contain mistakes that affect how the bot works.
Before you run your expert advisor (EA) in a live market, it’s important to test it and make sure it’s working the way you expect it to.
In this guide, we’ll walk through common coding mistakes in AI-generated EAs, how to fix them, and practical tips to improve your bot’s performance.
Common AI MQL5 coding errors
To create a real forex account for free, click here
These are some of the most common issues found in AI-generated EAs, along with ways to correct them:
- Undeclared identifier error in MQL5
This error appears when the bot uses a variable or name that hasn’t been defined properly, like missing a parameter or using an incorrect name in indicators like iMA().
Fix:
Check the indicator’s parameters and make sure all variable names match what’s expected in MetaEditor (MT5’s coding tool).
- Missing Buy/Sell trade execution functions
Some AI-generated bots skip essential logic for opening trades.
Fix:
Manually add basic trade execution functions. Here’s an example of a basic structure to place a buy order:
void OpenBuy() {
MqlTradeRequest request; MqlTradeResult result;
request.action = TRADE_ACTION_DEAL;
request.type = ORDER_TYPE_BUY;
request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
request.volume = 0.1; request.magic = 12345;
OrderSend(request, result);
}
Make sure also to include logic for sell orders and define the conditions under which trades should be placed.
- Improper loop conditions in Deriv cTrader
In Deriv cTrader, the bot’s logic needs to run inside specific functions like OnBar() or OnTick(). These are triggered automatically when the market moves. If your code is placed outside these sections, such as in the wrong loop or global scope, it won’t run as expected.
Fix:
Ensure that bars and ticks are handled inside OnBar() or OnTick(). This will allow your bot to respond correctly to new ticks or bars during backtesting and live trading.
To create a real forex account for free, click here
- Miscalculating lot size based on balance percentage
When the AI tries to set the lot size based on your account balance (e.g. risking 2% per trade), it may skip an important detail — the contract size. This can lead to inaccurate lot sizes.This won’t trigger a code error, but in testing, you’ll see trade sizes that don’t match your risk settings, resulting in overexposure or under-exposure in live trades.
Fix:
Use a formula with a lot size that factors in the contract size, such as:
//—————————————————————–
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double riskMoney = accountBalance * (RiskPercent / 100.0);
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double pointValue = tickValue / tickSize;
double contractSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_CONTRACT_SIZE);
if(contractSize > 0)
pointValue = pointValue / contractSize;
double lotSize = riskMoney / (StopLossPoints * pointValue);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathMax(lotSize, minLot);
lotSize = MathMin(lotSize, maxLot);
lotSize = MathFloor(lotSize / lotStep) * lotStep;
- Other errors
If you encounter one of these errors—or any other—you can always ask your AI model for help in identifying and fixing the issue.
Simply copy the error message and the relevant code, then share them with the AI model. Most of the time, it should be able to pinpoint the problem and offer a fix or suggest possible solutions.
Tip: Always run a backtest before going live. Issues like this don’t appear until you test the strategy with real market data.
Improve your AI-generated expert advisor
To create a real forex account for free, click here
Once your bot is working, you can make small changes to improve its performance and stability. Here are a few common hacks to do so:
- Add error-handling logic to manage unexpected behaviour during high volatility or connection issues.
- Use a trailing stop-loss to lock in potential profits when the market moves in your favour.
- Backtest your strategy with historical data to see how it would have performed.
- Clean up and organise your code to improve readability and efficiency. This will make it easier to update or troubleshoot later.
Tip: Use the MetaEditor Debugger (on MT5) to test how your bot runs step by step.
Why backtesting trades is critical
To create a real forex account for free, click here
AI-generated bots are a great way to automate your trading without needing to code from scratch. But even with AI, your strategy still needs human checks and adjustments to work well in live conditions.
Before going live, take time to test thoroughly and make small improvements. Even minor fixes can make a big difference in how consistently and effectively your EA performs.
To create a real forex account for free, click here