2-MA Crossover EA MQL5
Prompt
Create a fully functional MetaTrader 5 Expert Advisor (EA) in MQL5 that implements a two moving average crossover trading strategy using the CTrade class for all trade operations. Details and Requirements: - Define input parameters to configure: - Fast MA period and type (e.g., EMA, SMA) - Slow MA period and type - Lot size for position sizing - Stop loss and take profit values (in points or price, clarify within the code) - Symbol to trade (optional, default to the chart symbol) - On each new tick, calculate both the fast and slow moving averages using the specified types and periods. - Detect crossover signals: - A buy signal is generated when the fast MA crosses above the slow MA. - A sell signal is generated when the fast MA crosses below the slow MA. - Before opening a new trade, verify existing positions: - If there is an existing position opposing the new signal, close that position first. - Prevent opening duplicate trades in the same direction. - Use the MQL5 CTrade class exclusively for opening and closing trades with safe and clear commands. - Implement robust error handling around trade operations: - Log detailed messages whether orders are successful or fail, including error codes. - Organize and structure the code clearly with thorough comments explaining each part: - Inputs, initialization, deinitialization, per-tick logic, MA calculation, crossover detection, trade management. - Follow MQL5 best practices for efficiency, safety, and readability. # Steps to Implement: 1. Declare and initialize all input parameters to tune the strategy easily. 2. Calculate current and previous values of both fast and slow moving averages on each tick. 3. Implement logic to detect when the fast MA crosses above or below the slow MA by comparing current and previous values. 4. Detect trade signals based on crossover events. 5. Check current open positions for the trading symbol and determine if any action is required (closing opposite positions). 6. Use CTrade methods to open new buy or sell trades, and close opposing positions as needed. 7. Incorporate error checking after every trade operation and log outcomes with descriptive messages. 8. Add comments throughout to explain purpose and facilitate future adjustments. # Output Format Return a complete .mq5 source code file content that is immediately compilable and ready to deploy as an Expert Advisor in MetaTrader 5. # Notes - Ensure the code respects MetaTrader 5 conventions and runs efficiently on each tick. - All parameter values should be modifiable via input parameters for flexible optimization. - The strategy must handle positions carefully to avoid conflicting open trades. - Logging should be done via Print() or preferably via the ExpertAdvisor logging system for clarity. - The code should be self-contained and require no external libraries beyond standard MQL5.
Related Coding Prompts
Write Code
As a seasoned programmer, your task is to write code in [programming language] to [perform action]. The code should be efficient, well-structured, and optimized for performance. Make sure to follow best practices and industry standards while implementing the necessary algorithms and logic to achieve the desired functionality. Test the code thoroughly to ensure it functions as intended and meets all requirements. Additionally, document the code properly for future reference and maintenance.
Debug Code
Act as a seasoned programmer with over 20 years of commercial experience. Analyze the provided [piece of code] that is causing a specific [error]. Your task involves diagnosing the root cause of the error, understanding the context and functionality intended by the code, and proposing a solution to fix the issue. Your analysis should include a step-by-step walkthrough of the code, identification of any bugs or logical mistakes, and a detailed explanation of how to resolve them. Additionally, suggest any improvements or optimizations to enhance the performance, readability, or maintainability of the code based on your extensive experience. Ensure that your solution adheres to best practices in software development and is compatible with the current development environment where the code is being executed.
Do Code Review
As a seasoned programmer with over 20 years of commercial experience, your task is to perform a comprehensive code review on the provided [piece of code]. Your review should meticulously evaluate the code's efficiency, readability, and maintainability. You are expected to identify any potential bugs, security vulnerabilities, or performance issues and suggest specific improvements or optimizations. Additionally, assess the code's adherence to industry standards and best practices. Your feedback should be constructive and detailed, offering clear explanations and recommendations for changes. Where applicable, provide examples or references to support your suggestions. Your goal is to ensure that the code not only functions as intended but also meets high standards of quality and can be easily managed and scaled in the future. This review is an opportunity to mentor and guide less experienced developers, so your insights should be both educational and actionable.