Overview
This prompt aims to guide developers in creating a MetaTrader 5 Expert Advisor using MQL5 for a 2-MA crossover strategy. Coders and traders will benefit by automating trading processes and enhancing their trading efficiency.
Prompt Overview
Purpose: This EA automates trading based on a 2-MA crossover strategy in MetaTrader 5.
Audience: It is designed for traders and developers familiar with MQL5 and automated trading systems.
Distinctive Feature: The EA utilizes the CTrade class for efficient order management and error handling.
Outcome: Users will receive a fully functional and customizable MQL5 source code for immediate deployment.
Quick Specs
- Media: Text
- Use case: Content Creation, Expansion & Elaboration, Generation
- Industry: Consulting (Management, Strategy), Fintech & Digital Banking, General Business Operations
- Techniques: Output Constraints, Role/Persona Prompting, Structured Output
- Models: Claude 3.5 Sonnet, Gemini 2.0 Flash, GPT-4o, Llama 3.1 70B
- Estimated time: 5-10 minutes
- Skill level: Beginner
Variables to Fill
No inputs required — just copy and use the prompt.
Example Variables Block
No example values needed for this prompt.
The Prompt
Create an Expert Advisor (EA) for MetaTrader 5 (MT5) using MQL5 that implements a 2-MA crossover trading strategy. The EA should utilize the CTrade class for effective order management.
**Details:**
– Implement two moving averages:
– A fast MA
– A slow MA
– Generate buy signals when:
– The fast MA crosses above the slow MA
– Generate sell signals when:
– The fast MA crosses below the slow MA
– Use MQL5 CTrade class methods for:
– Opening and closing positions
– Safely managing orders
– Include parameters for:
– MA periods
– MA types (e.g., EMA, SMA)
– Manage:
– Trade position sizing
– Stop loss and take profit as needed
– Ensure the EA handles order states correctly to prevent duplicate orders.
**Steps:**
1. Define input parameters for:
– Moving average periods
– Moving average types
– Trade parameters
2. Calculate the current values of both MAs on each new tick.
3. Detect crossover events.
4. Use CTrade methods to:
– Open buy trades upon crossovers
– Open sell trades upon crossovers
5. Manage existing positions appropriately:
– Close opposite positions as necessary
6. Include error handling and logging.
**Output Format:**
Provide the complete MQL5 source code (.mq5) for the Expert Advisor implementing the 2-MA crossover strategy using CTrade. Ensure the code is:
– Fully commented
– Well-organized
**Notes:**
– Ensure the EA can be easily adjusted with input parameters.
– Follow MQL5 best practices for trading operations.
– The output should be ready to compile and run in MT5 without further modification.
Screenshot Examples
How to Use This Prompt
- Copy the prompt provided above.
- Paste it into your preferred coding environment.
- Modify any parameters as needed for your strategy.
- Compile the MQL5 code in MetaTrader 5.
- Test the Expert Advisor on a demo account.
- Adjust settings based on performance results.
Tips for Best Results
- Define Inputs: Set parameters for MA periods, types, and trade settings for flexibility.
- Calculate MAs: On each tick, compute the fast and slow moving averages to detect crossovers.
- Manage Trades: Use CTrade methods to open or close trades based on crossover signals, ensuring no duplicate orders.
- Error Handling: Implement logging and error checks to ensure robust operation and easy troubleshooting.
FAQ
- What is an Expert Advisor (EA) in MetaTrader 5?
An EA is an automated trading system that executes trades based on predefined strategies in MetaTrader 5. - What does the CTrade class do in MQL5?
The CTrade class simplifies order management, allowing for easy opening, closing, and modifying of trades. - How do moving averages generate buy signals?
Buy signals occur when a fast moving average crosses above a slow moving average. - What is the purpose of error handling in an EA?
Error handling ensures the EA can manage issues gracefully, preventing crashes and maintaining trading integrity.
Compliance and Best Practices
- Best Practice: Review AI output for accuracy and relevance before use.
- Privacy: Avoid sharing personal, financial, or confidential data in prompts.
- Platform Policy: Your use of AI tools must comply with their terms and your local laws.
Revision History
- Version 1.0 (February 2026): Initial release.


