How to Build an Expert Advisor with AI for MT5 on Deriv
Expert Advisors (EAs) are trading bots that run on MetaTrader 5. With AI, you can build sophisticated EAs without knowing MQL5 — just describe the strategy and AI generates the complete code.
Deriv offers integrated MT5 — so you can build, test, and run EAs directly on the platform, with access to Forex, synthetic indices (V75), and more.
What is an Expert Advisor?
It’s a program written in MQL5 that runs inside MetaTrader 5. It can:
- Analyze the market using indicators (RSI, MACD, EMAs, etc.)
- Open and close positions automatically
- Manage stop loss, take profit, and trailing stops
- Run 24/7 without human intervention
- Backtest with historical data
Why use MT5 on Deriv?
- ✅ Access to synthetic indices (V75, Crash/Boom) — 24/7
- ✅ Forex, commodities, and crypto via CFDs
- ✅ Demo account with $10,000 in virtual funds
- ✅ Built-in Backtesting in the Strategy Tester
- ✅ Official Deriv support for MT5
- ✅ EA runs server-side — no need to keep your PC on (with a VPS)
Setting up MT5 on Deriv
Create a Deriv account
Go to deriv.com and create your account (free).
Enable Deriv MT5
In Trader’s Hub → “CFDs” section → click “Get” next to Deriv MT5 → create an MT5 account (demo or live).
Download MT5
Download MetaTrader 5 for your system (Windows, Mac, Linux via Wine). Or use the MT5 web version directly in your browser.
Log in
Use the MT5 credentials provided by Deriv (login, password, server). The Deriv server is “Deriv-Demo” or “Deriv-Server”.
Building an EA with AI
Here’s the full process:
Describe the strategy to AI
Open ChatGPT or Claude and use the prompt below.
🤖 Prompt to generate a complete EA
Copy the MQL5 code
AI generates the full EA (~200-300 lines). Copy all the code.
Open MetaEditor
In MT5 → “Tools” menu → “MetaQuotes Language Editor” (or press F4). Create a new file: File → New → Expert Advisor.
Paste and compile
Delete the default code. Paste the AI code. Click “Compile” (F7). If there are errors, paste the messages back to AI and ask for a fix.
Test in Strategy Tester
In MT5 → View → Strategy Tester. Select your EA, asset (V75), period (1 year), and run the backtest. Analyze the report.
Run on a demo account
Drag the EA onto the asset chart. Enable “AutoTrading” (top button). The EA starts trading automatically.
Sample EA — ready code
//+------------------------------------------------------------------+
//| EA EMA Cross + RSI — AI-generated |
//| IA Trader Pro — iatraderpro.com/ |
//| WARNING: Use only on a DEMO account! |
//+------------------------------------------------------------------+
#property copyright "IA Trader Pro"
#property version "1.00"
#property strict
// Input parameters
input int EMA_Fast = 9; // Fast EMA period
input int EMA_Slow = 21; // Slow EMA period
input int RSI_Period = 14; // RSI period
input int RSI_Lower = 40; // RSI minimum for buy
input int RSI_Upper = 60; // RSI maximum for sell
input double RiskPercent = 2.0; // Risk per trade (%)
input int StopLoss = 200; // Stop Loss in points
input int TakeProfit = 400; // Take Profit in points
input int TrailingStop = 100; // Trailing Stop in points
input int MagicNumber = 12345; // Magic number
// Indicator handles
int handleEmaFast, handleEmaSlow, handleRsi;
//+------------------------------------------------------------------+
//| Initialization |
//+------------------------------------------------------------------+
int OnInit()
{
handleEmaFast = iMA(_Symbol, PERIOD_CURRENT, EMA_Fast, 0, MODE_EMA, PRICE_CLOSE);
handleEmaSlow = iMA(_Symbol, PERIOD_CURRENT, EMA_Slow, 0, MODE_EMA, PRICE_CLOSE);
handleRsi = iRSI(_Symbol, PERIOD_CURRENT, RSI_Period, PRICE_CLOSE);
if(handleEmaFast == INVALID_HANDLE || handleEmaSlow == INVALID_HANDLE || handleRsi == INVALID_HANDLE)
{
Print("❌ Error creating indicators");
return INIT_FAILED;
}
Print("✅ EA IA Trader Pro started successfully");
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Deinitialization |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(handleEmaFast);
IndicatorRelease(handleEmaSlow);
IndicatorRelease(handleRsi);
Print("EA stopped. Reason: ", reason);
}
//+------------------------------------------------------------------+
//| Main tick |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if there's an open position
if(PositionsTotal() > 0)
{
// Apply trailing stop
ApplyTrailingStop();
return;
}
// Get indicator values
double emaFast[], emaSlow[], rsi[];
CopyBuffer(handleEmaFast, 0, 0, 3, emaFast);
CopyBuffer(handleEmaSlow, 0, 0, 3, emaSlow);
CopyBuffer(handleRsi, 0, 0, 2, rsi);
ArraySetAsSeries(emaFast, true);
ArraySetAsSeries(emaSlow, true);
ArraySetAsSeries(rsi, true);
// Crossover up (buy signal)
bool crossUp = emaFast[1] <= emaSlow[1] && emaFast[0] > emaSlow[0];
bool crossDown = emaFast[1] >= emaSlow[1] && emaFast[0] < emaSlow[0];
double lot = CalculateLot();
// BUY signal
if(crossUp && rsi[0] > RSI_Lower && rsi[0] < 70)
{
double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = price - StopLoss * _Point;
double tp = price + TakeProfit * _Point;
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_DEAL;
req.symbol = _Symbol;
req.volume = lot;
req.type = ORDER_TYPE_BUY;
req.price = price;
req.sl = sl;
req.tp = tp;
req.magic = MagicNumber;
req.comment = "IA Trader Pro BUY";
if(OrderSend(req, res))
Print("🟢 BUY: ", lot, " lots @ ", price);
else
Print("❌ Buy error: ", res.retcode);
}
// SELL signal
if(crossDown && rsi[0] < RSI_Upper && rsi[0] > 30)
{
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double sl = price + StopLoss * _Point;
double tp = price - TakeProfit * _Point;
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_DEAL;
req.symbol = _Symbol;
req.volume = lot;
req.type = ORDER_TYPE_SELL;
req.price = price;
req.sl = sl;
req.tp = tp;
req.magic = MagicNumber;
req.comment = "IA Trader Pro SELL";
if(OrderSend(req, res))
Print("🔴 SELL: ", lot, " lots @ ", price);
else
Print("❌ Sell error: ", res.retcode);
}
}
//+------------------------------------------------------------------+
//| Calculate lot based on risk |
//+------------------------------------------------------------------+
double CalculateLot()
{
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double riskAmount = balance * RiskPercent / 100.0;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double lot = riskAmount / (StopLoss * tickValue);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lot = MathFloor(lot / lotStep) * lotStep;
lot = MathMax(minLot, MathMin(maxLot, lot));
return lot;
}
//+------------------------------------------------------------------+
//| Trailing Stop |
//+------------------------------------------------------------------+
void ApplyTrailingStop()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(PositionGetSymbol(i) != _Symbol) continue;
if(PositionGetInteger(POSITION_MAGIC) != MagicNumber) continue;
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentSL = PositionGetDouble(POSITION_SL);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
long type = PositionGetInteger(POSITION_TYPE);
if(type == POSITION_TYPE_BUY)
{
double newSL = bid - TrailingStop * _Point;
if(newSL > currentSL && newSL > openPrice)
{
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_SLTP;
req.position = PositionGetInteger(POSITION_TICKET);
req.symbol = _Symbol;
req.sl = newSL;
req.tp = PositionGetDouble(POSITION_TP);
OrderSend(req, res);
}
}
else if(type == POSITION_TYPE_SELL)
{
double newSL = ask + TrailingStop * _Point;
if(newSL < currentSL && newSL < openPrice)
{
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_SLTP;
req.position = PositionGetInteger(POSITION_TICKET);
req.symbol = _Symbol;
req.sl = newSL;
req.tp = PositionGetDouble(POSITION_TP);
OrderSend(req, res);
}
}
}
}
✅ This EA does:
EMA 9/21 crossover + RSI filter → automatic buy/sell → 200pt stop loss → 400pt take profit (1:2) → 100pt trailing stop → lot sized at 2% risk → max 1 position. Ready to compile and run on Deriv MT5.
Powering it up with AI
After running the basic EA, ask AI to add:
- Time filter: “Trade only between 08:00 and 17:00 GMT”
- Multi-timeframe: “Confirm direction on the H1 timeframe before trading on M5”
- MACD as filter: “Only buy if MACD histogram is positive”
- Auto-breakeven: “Move SL to entry price when profit reaches 100 points”
- Chart dashboard: “Show a panel with indicator status, last trade, P&L”
🚀 To run EAs on MT5, create your free Deriv MT5 account:
Create Deriv MT5 Account →📚 Related
→ Deriv Bot: No-Code Bots
→ Python + Deriv API
→ 5 AI Prompts for Traders (includes EA prompt)
→ Deriv Review 2026