Building a Basic Fibonacci Retracement Expert Advisor in MQL5
Watch on YouTube: Fibonacci Retracement Expert Advisor in MQL5
1. Introduction to Fibonacci-Based Trading Strategies
Fibonacci retracement is one of the most widely used tools in technical analysis. It helps traders identify potential reversal levels where price may retrace before continuing in the original trend direction. The key levels (23.6%, 38.2%, 50%, 61.8%) act as potential support or resistance zones.
In algorithmic trading, incorporating Fibonacci levels into an Expert Advisor (EA) allows for a more disciplined and rule-based approach to trading retracements. In this article, we’ll explore a basic Fibonacci Retracement EA built in MQL5, and then discuss how it can be improved and made more robust for real-world trading.
2. How the Fibonacci EA Works
This simple EA performs the following actions:
Draws a Fibonacci retracement on the chart using the most recent swing high and low over the last 100 bars.
Determines the trend direction by comparing the position of the swing low and high.
Identifies the 50% retracement level and looks for a trade opportunity when price moves back into this zone.
Applies a basic momentum filter (two-bar price action) to validate entry.
Automatically closes positions if the trend direction reverses.
- Trading Logic Execution: Only when a new bar opens.
This implementation is intentionally kept simple to serve as a learning base for traders and developers who want to get started with strategy automation in MQL5.
3. Areas of Improvement & Future Enhancements
While the EA demonstrates a functional framework, there are several areas where it could be improved for real trading conditions:
Risk Management: Currently uses a fixed lot size. Integrating a dynamic lot sizing based on risk percentage and stop-loss distance would improve capital protection.
Stop Loss / Take Profit Logic: The stop loss is a simple pip offset from entry. More robust methods (e.g., ATR-based or swing-level stop loss) would add flexibility.
- Trailing SL: A trailing stop-loss can enhance trade management by securing profits as the EA aligns with the prevailing trend direction.
Position Filtering: The EA checks for open positions globally, which can cause conflicts with other EAs or manual trades. Adding symbol and magic number filtering is essential.
Additional Confirmation: Incorporating confirmation from higher timeframes, price action patterns, volume analysis, or other technical indicators can help filter out weak trade entries and improve overall signal quality.
These limitations are intentional in this basic version to keep the logic clear and easy to follow. You can expand and modify the EA based on your specific trading goals or strategies.
4. Basic Fibonacci Retracement EA Code
Below is the complete source code of the Fibonacci EA. You can copy and paste it into MetaEditor and run it on a MetaTrader 5 demo account. This code is intended solely for educational purposes and does not constitute investment or trading advice.
Full Code (To be used only on demo account!)
DOWNLOAD LINK
Conclusion
This basic Fibonacci Retracement EA provides a practical starting point for understanding how to automate a technical trading strategy in MQL5. By combining Fibonacci levels, trend direction, and simple momentum filters, it demonstrates how structured logic can be applied to the markets. While the EA is intentionally minimal for educational purposes, it lays the groundwork for more advanced development—such as dynamic risk management, higher-timeframe filtering, trailing stops, and enhanced trade execution. As with any automated system, thorough testing on a demo account is essential before considering live deployment.
