Introduction
Building a trading Expert Advisor (EA) used to require years of MQL4/MQL5 programming knowledge. Today, with AI tools like ChatGPT, Claude, and GitHub Copilot, a trader with zero coding experience can go from idea to a working EA in days. This guide walks you through the entire process — from defining your strategy to deploying it live.
Step 1: Define Your Trading Strategy First
Before you write a single line of code, you need a crystal-clear trading strategy. AI can write the code — but it cannot create the strategy for you. The strategy is your edge. You need to define:
- Entry conditions — What triggers a buy or sell? (e.g., price crosses above 200 EMA + RSI below 70)
- Exit conditions — Fixed take profit and stop loss? Trailing stop? Time-based exit?
- Position sizing — Fixed lots or percentage of balance?
- Trading sessions — Which hours should the EA trade? Which pairs or instruments?
- Filters — What conditions should prevent trading? (e.g., avoid high-impact news, Asian session, Friday close)
Write all of this in plain English before you touch any AI tool. The more specific you are, the better your EA will be.
Step 2: Choose Your Platform — MT4 or MT5?
Most retail traders use MT4, and for good reason — it has been the industry standard since 2005, has massive community support, and virtually every broker supports it. MT5 is newer and more powerful but has a smaller community and fewer EAs available.
Use MT4 if: You want maximum broker compatibility, you are trading Forex and Gold, and you prefer a proven ecosystem with thousands of existing resources.
Use MT5 if: You want to trade stocks, futures, or crypto alongside Forex, or your broker only supports MT5.
For Gold (XAUUSD) EA trading specifically, MT4 remains the dominant platform and this guide focuses on MT4.
Step 3: Use AI to Write Your MQL4 Code
This is where AI becomes your coding partner. Open ChatGPT or Claude and describe your strategy in detail. A good prompt looks like this:
"Write an MQL4 Expert Advisor for MT4 with the following rules: Buy when the 20 EMA crosses above the 50 EMA on the H1 chart and RSI(14) is below 70. Set stop loss at 50 pips and take profit at 100 pips. Use fixed lot size of 0.01. Only trade between 09:00 and 17:00 server time. Include input parameters for all values so I can adjust them in the EA settings."
The AI will generate a complete .mq4 file. Copy the code into MetaEditor (press F4 in MT4 to open it), paste the code, and compile it by pressing F7.
Step 4: Handle Common Errors
Your first compile will almost certainly have errors. Do not panic. Copy the exact error message back to the AI and ask it to fix it. Common errors include:
- 'OrderSend' error 130 — Stop loss or take profit too close to current price.
- Undeclared identifier — A variable used before it was defined.
- Wrong number of parameters — A function called with incorrect arguments.
Keep a conversation going with the AI, feeding it each error until the code compiles clean (0 errors, 0 warnings).
Step 5: Backtest Rigorously Before Going Live
Once the EA compiles, attach it to a chart in Strategy Tester (Ctrl+R in MT4). Run a backtest on at least 2 years of historical data. Look for a profit factor above 1.5, maximum drawdown below 20%, and at least 100 trades for statistical significance.
If the backtest looks poor, go back to Step 1 and refine your strategy. Do not skip this step and go live — backtesting is your safety net.
Step 6: Forward Test on a Demo Account
A good backtest does not guarantee live performance. Markets change. Run your EA on a demo account for at least 4–8 weeks before risking real money. Watch for unexpected errors in the Journal tab and verify that execution matches what the backtest showed.
Step 7: Optimise with AI Assistance
Once you have real data from forward testing, go back to the AI with specific improvement requests. For example: "My EA has a 60% win rate but loses money because losing trades are 3x larger than winning trades. Can you add a trailing stop that moves to breakeven after 30 pips of profit?" The AI can iteratively improve the EA based on real observations.
Step 8: Deploy Live with Strict Risk Rules
When you are ready to go live, start with the smallest possible lot size. Set a daily loss limit — if the EA loses more than 3% in a day, it stops trading. Most modern EAs can be coded with this feature. Ask the AI to add it before going live.
The Role of MyFXIntel in Your EA Journey
Once your EA is live, tracking performance manually is tedious and error-prone. MyFXIntel was built specifically for this — upload your MT4 statement and instantly see which days your EA performs best, which price zones to avoid, and how global events like NFP and FOMC impact your results. The intelligence layer your EA never had built in.
Conclusion
Building an EA with AI is no longer the exclusive domain of professional programmers. With a clear strategy, the right AI prompts, and disciplined testing, any trader can build and deploy a personalised Expert Advisor. The key is patience — test thoroughly before going live, and let data guide every improvement.
Analyse Your EA Performance
Upload your MT4 statement and get instant AI-powered analysis on every trade — sessions, zones, news impact, and more.
Try MyFXIntel Free →