How to Build an MT5 Expert Advisor Without Coding Using AI Tools
The old way to build an MT5 EA: hire a coder, spend weeks back-and-forth, pay hundreds of dollars, and end up with code you can't maintain. The new way: describe your strategy to an AI tool, get working MQL5 code in minutes, and iterate yourself. Here's the full process.
What You Need Before You Start
- A MetaTrader 5 (MT5) account with a broker that offers XAUUSD trading
- MetaEditor installed (comes with MT5 — press F4 to open)
- Access to an AI tool (ChatGPT, Claude, or similar)
- A trading strategy idea in plain English — entry rule, exit rule, risk per trade
You do not need to know MQL5, C++, or any programming language. The AI tool handles the code. Your job is to describe the strategy clearly and review the backtest results critically.
Step 1: Define Your Strategy in Plain English
Before prompting any AI tool, write out your strategy in plain English. The more specific you are, the better the generated code will be.
Strategy definition template:
- Instrument + timeframe: e.g. XAUUSD, H1 chart
- Entry rule: e.g. Buy when the 20 EMA crosses above the 50 EMA and RSI is above 50
- Exit rule: e.g. Take profit at 2× ATR, stop loss at 1× ATR from entry
- Risk per trade: e.g. 1% of account balance
Step 2: Prompt an AI Tool to Write the MQL5 Code
Open your AI tool of choice (ChatGPT, Claude, etc.) and use this prompt structure:
Write a complete, compilable MQL5 Expert Advisor for MetaTrader 5 with the following rules:
- Instrument: XAUUSD, H1 timeframe
- Entry: Buy when 20 EMA crosses above 50 EMA and RSI(14) > 50. Sell when 20 EMA crosses below 50 EMA and RSI(14) < 50.
- Exit: Take profit = 2 × ATR(14) from entry. Stop loss = 1 × ATR(14) from entry.
- Risk: 1% of account balance per trade.
- Only one trade at a time (no pyramiding).
Include all necessary includes, handle trade execution with CTrade, and add input parameters for the indicator periods and risk percentage.
The AI tool will return complete MQL5 code. Copy all of it.
Step 3: Paste the Code into MetaEditor and Compile
- In MT5, press F4 to open MetaEditor
- Click File → New → Expert Advisor (template), give it a name
- Delete the default template code and paste in the AI-generated code
- Press F7 (or click Compile)
- If there are errors in the Errors tab, copy the exact error text and paste it back into the AI tool with: "This code gave the following compile error — please fix it: [error text]"
- Repeat until the Errors tab shows 0 errors
Step 4: Backtest in MT5 Strategy Tester
- In MT5, press Ctrl+R to open Strategy Tester
- Select your EA from the Expert dropdown
- Set Symbol to XAUUSD, timeframe to H1
- Set a date range (at least 2 years of data for meaningful results)
- Set Model to Every tick based on real ticks for most accurate results
- Click Start
Review these key metrics in the results: Profit Factor (aim for above 1.5), Maximum Drawdown (aim for below 20%), and Total Net Profit. If the results look wrong, describe the issue to the AI tool and ask it to fix the logic.
Step 5: Debug Errors with the AI Tool
Most first-generation EAs have bugs — the EA opens too many trades, the position sizing is wrong, or the entry logic fires at the wrong time. Fix these by describing the problem precisely:
"The EA is opening multiple trades in the same direction at once. I want only one position at a time. Here's the current code: [paste code]. Please fix this."
Be specific — "it's not working" produces poor fixes. "It opens 3 buy trades simultaneously instead of 1" produces accurate fixes.
Step 6: Deploy on Your XAUUSD Chart
- In MT5, open a XAUUSD H1 chart
- In the Navigator panel (Ctrl+N), find your EA under Expert Advisors
- Drag the EA onto the chart
- In the EA properties dialog, set your risk parameters (e.g. RiskPercent = 1.0)
- Ensure Allow Automated Trading is checked
- Click OK — the EA icon should appear in the top right corner of the chart (smiley face = running)
- Enable the AutoTrading button in the MT5 toolbar
Common Mistakes and How to Avoid Them
- Vague prompts: "Build me a profitable EA" gives unusable output. Always specify exact entry/exit rules.
- Skipping the backtest: Never run an EA live without at least 1-2 years of backtest data. A strategy that looks right in code may not perform as expected.
- Over-optimising: Don't fit the EA too tightly to historical data — it will fail on future price action. Test on an out-of-sample period.
- No risk limit: Always define a maximum risk per trade (1% of account balance is a common starting point). Never risk more than you can afford to lose.
Frequently Asked Questions
Do I need to know MQL5 to build an MT5 EA?
No. AI tools like ChatGPT and Claude can generate complete, working MQL5 code from a plain English description of your strategy. You describe what you want — entry rules, exit rules, risk settings — and the AI writes the code. You then paste it into MetaEditor, compile it, and run it in MT5.
Which AI tool is best for building MT5 EAs?
ChatGPT (GPT-4o or above) and Claude (Sonnet or above) both produce reliable MQL5 code when given a well-structured prompt. The quality depends more on your prompt than on the specific AI tool. Follow the 4-part framework: instrument + timeframe + entry/exit rules + risk rule.
What if the AI-generated code has errors?
Copy the exact error message from MetaEditor and paste it back into the AI tool. Ask it to fix the error. This debugging loop — generate, compile, fix errors — typically takes 2-5 iterations for a simple EA. More complex EAs may require more rounds.
Can I build a profitable EA this way?
The AI tool writes code that matches your strategy description — it does not create a profitable strategy for you. Profitability depends on the logic of your strategy, how well it's backtested, and whether it performs in live market conditions. Always backtest thoroughly and use strict risk management.
Do I need a VPS to run an MT5 EA?
For consistent 24/5 operation, a VPS (Virtual Private Server) is recommended so your EA runs even when your PC is off. Many MT5 brokers offer free VPS hosting for accounts above a minimum balance. Alternatively, you can run MT5 on your PC during market hours.
Trading involves significant risk of loss. AI QUANT is an educational platform and does not provide licensed investment advice. Past performance does not guarantee future results. Only trade with funds you can afford to lose. For regulatory guidance, refer to the Securities Commission Malaysia and Bank Negara Malaysia.
Want to build your first EA with live guidance?
AI QUANT VIP Members get monthly live sessions where we build EAs and indicators using AI tools — step by step, with live Q&A.