Prompt Engineering for Traders: Build MT5 EAs with AI Tools

The quality of your EA depends on the quality of your prompt. Garbage in, garbage out — a vague prompt produces vague code that doesn't match your strategy. Here's the exact framework to use, with two complete prompt examples you can adapt immediately.

Why Prompt Quality Determines EA Quality

AI tools generate code based on what you describe. If your description is ambiguous, the AI makes assumptions — and those assumptions may not match your intent. A prompt like "build me a profitable XAUUSD EA" tells the AI almost nothing. It will invent a strategy, and that invented strategy has no connection to any validated edge.

A good prompt specifies every parameter the AI needs to write correct code: the instrument, the timeframe, the entry conditions (exactly), the exit conditions (exactly), and the risk rule. Nothing ambiguous, nothing left to the AI's "judgement."

The 4-Part Strategy Prompt Framework

1. Instrument + Timeframe

"XAUUSD, H1 chart"

Determines symbol name, indicator periods, and trading session context

2. Entry Rule (exact condition)

"Buy when 20 EMA crosses above 50 EMA. Sell when 20 EMA crosses below 50 EMA."

No vague terms — the AI needs a specific condition it can translate to code

3. Exit Rule (specific values)

"Take profit = 2 × ATR(14) from entry. Stop loss = 1 × ATR(14) from entry."

ATR-based exits adapt to gold's changing volatility automatically

4. Risk Rule

"Risk 1% of account balance per trade."

This tells the AI how to calculate lot size based on entry-to-stop distance

Example Prompt 1 — Moving Average Crossover EA

A simple trend-following EA that buys when the fast MA crosses above the slow MA and exits with ATR-based levels:

Prompt:

Write a complete, compilable MQL5 Expert Advisor for MetaTrader 5 with these exact rules:

Instrument: XAUUSD, H1 timeframe

Entry: Buy when the 20-period EMA crosses above the 50-period EMA on the close of a bar. Sell when the 20-period EMA crosses below the 50-period EMA on the close of a bar.

Exit: Take profit = 2 × ATR(14) from entry price. Stop loss = 1 × ATR(14) from entry price.

Risk: Calculate lot size so that if the stop loss is hit, the loss equals 1% of account balance.

Trade management: Only one position at a time (no pyramiding). Close any open position when the opposite signal triggers.

Use the CTrade class for trade execution. Include input parameters for EMA periods (default 20 and 50), ATR period (default 14), and risk percentage (default 1.0). Add all necessary includes and handle potential errors in trade execution.

Example Prompt 2 — ATR-Based XAUUSD Momentum EA

A momentum EA that enters on intraday breakouts and adapts stop losses to current volatility:

Prompt:

Write a complete, compilable MQL5 Expert Advisor for MetaTrader 5 with these exact rules:

Instrument: XAUUSD, H1 timeframe

Entry: Buy when the current bar's close is above the highest high of the last 20 bars (20-bar channel breakout), and RSI(14) is above 55. Sell when the current bar's close is below the lowest low of the last 20 bars, and RSI(14) is below 45.

Exit: Take profit = 3 × ATR(14) from entry. Stop loss = 1.5 × ATR(14) from entry. Also close the trade if RSI crosses back through 50 in the direction against the trade.

Risk: Risk 0.5% of account balance per trade.

Trade management: Maximum one open position at a time. Do not re-enter a trade in the same direction if the previous trade was closed by stop loss in the same trading session (UTC day).

Use CTrade for execution. Add input parameters for all indicator periods, risk percentage, and ATR multipliers. Include error handling.

How to Prompt for Debugging

When MetaEditor shows a compile error, or when the EA behaves unexpectedly in a backtest, describe the problem precisely to the AI tool.

For compile errors:

"This MQL5 code gives the following compile error in MetaEditor. Please fix it and return the corrected full code: [paste exact error message]"

For backtest behaviour issues:

"In the MT5 backtest, this EA is opening multiple long trades simultaneously instead of just one. Here is the full code: [paste code]. Please modify it so that only one position can be open at a time in any direction."

For adding features to existing code:

"Here is my current EA code: [paste code]. I want to add a daily loss limit — if total losses today exceed 2% of my starting day balance, the EA should stop opening new trades until tomorrow. Please add this feature and return the full updated code."

Common Prompt Mistakes and How to Fix Them

✗ "Build me a profitable XAUUSD EA"

Specify exact entry and exit rules. 'Profitable' is not a strategy — it's an outcome. The AI cannot invent a proven edge for you.

✗ "Use the best indicators for gold"

Pick specific indicators with specific parameters: '20 EMA, 50 EMA, ATR(14)'. Vague instructions produce generic code.

✗ "Asking for many features at once"

Start with a minimal EA (entry, exit, risk). Add features one at a time in separate prompts — each addition is easier to debug.

✗ "Not specifying one trade at a time"

Always include: 'Only one position open at a time.' Without this, many EAs will pyramid into the same direction.

Frequently Asked Questions

What AI tools are best for writing MQL5 code?

ChatGPT (GPT-4o or above) and Claude (Sonnet or above) both produce reliable MQL5 code when given well-structured prompts. The quality of your prompt matters more than the specific AI tool — a vague prompt in GPT-4o produces worse output than a precise prompt in Claude 3.5.

What if the AI generates code that doesn't compile?

Copy the exact error message from MetaEditor's Errors tab and paste it into the AI tool with: 'This code gave the following compile error — please fix it: [error text]'. Be precise — the AI needs the exact error to fix it accurately.

Can I prompt the AI to improve an existing EA?

Yes. Paste the full existing code into the AI tool and describe the change you want: 'Here is my current EA code. I want to add a trailing stop that moves the stop loss to break-even after the trade is 1× ATR in profit. Please modify the code.' The AI will return the updated version.

How do I prompt the AI to add risk management to my EA?

Add a risk rule to your prompt: 'Calculate lot size so that if the stop loss is hit, the loss equals 1% of account balance.' You can also ask it to add a daily loss limit: 'If total losses today exceed 3% of account balance, stop trading for the rest of the day.'

See these prompts in action

AI QUANT VIP Members build EAs live using this exact framework in monthly sessions — with Q&A and real MT5 backtest review.