MetaBot4 is a robust, modular Expert Advisor (EA) for MetaTrader 4, designed for automated order management and high-precision execution in financial markets. It leverages Object-Oriented Programming (OOP) in MQL4 to provide a reliable framework for systematic trading.
The system is built on a decoupled architecture, ensuring that trade logic, configuration, and logging remain distinct:
metabot4.mq4(Core Engine): The orchestrator. It manages the heartbeat of the bot viaOnTimer, handling state transitions and order lifecycle management.Order.mqh(Data Model): Encapsulates order parameters (Magic Number, SL/TP, Volume). This ensures data integrity when passing trade instructions to the MetaTrader API.Register.mqh(Configuration): Handles parameter persistence. By reading configurations from external files, it allows for dynamic strategy adjustments without needing to recompile the source code.Log.mqh(Auditing): Provides comprehensive transaction logging. Every state change or error is recorded, creating a detailed audit trail essential for post-trade analysis.
MetaBot4 operates as a Finite State Machine (FSM), transitioning through specific lifecycle phases. The logic is driven by a high-frequency timer (fast_timer_period = 10ms), ensuring market events are captured with minimal latency.
- Initialization (
init_EAmode): The system performs safety checks, validates market connectivity, and reads configuration parameters from theRegistermodule. - Decision Phase (
choice_EAmode): The bot evaluates market conditions based on predefined factors (price, volume, slippage). It determines whether to open, modify, or close positions. - Execution & Lifecycle Management:
- The bot calls
OpenedTotal()to synchronize its internal state with the broker's platform. - If the number of active orders deviates from the strategy's targets, it triggers
startEA()(to enter) orcloseAll()(for risk mitigation).
- The bot calls
At every tick or timer interval, the bot performs the following:
- State Synchronization: Updates current open positions.
- Sanity Checks: Verifies if the order count matches the expected strategy footprint.
- Emergency Override: If an inconsistency is detected (e.g., active orders > strategy max), it calls
stopEA()to enter a safety halt state.
- Dynamic Position Scaling: Uses
init_volumeandvolume_factorto compute position sizes based on capital management rules. - Slippage Protection: The
slippage_factorensures that orders are only executed if price stability is within the configured threshold. - Graceful Shutdown: The
closeAll()function ensures that all bot-managed positions are liquidated synchronously if the EA is stopped or a critical error occurs. - Post-Trade Audit: Automated logging of every
Modify,Delete, andErrorevent ensures complete transparency of the decision-making process.
- MetaTrader 4 terminal installed.
- Knowledge of MetaEditor for compilation.
- Copy all
.mq4and.mqhfiles to your MT4 directory:MQL4/Experts/. - Open
metabot4.mq4in MetaEditor and click Compile. - Attach the EA to a chart.
- Ensure the "AutoTrading" button is enabled in the MetaTrader interface.
This project is distributed under the BSD 2-Clause License. Disclaimer: This software is provided "as-is" without warranty of any kind. Automated trading involves significant risk of financial loss. Always test in a demo environment before deploying with real capital.
MetaBot4 is designed to be a foundation for more complex strategies.
- Extensibility: Feel free to extend
Order.mqhto implement Trailing Stop logic or integrate technical indicators (RSI, Moving Averages). - Feedback: If you identify optimization opportunities in the FSM logic, please submit a Pull Request.