Skip to content

Unix69/MetaBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 MetaBot4: Algorithmic Trading Engine

MQL4 Domain License

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.


🏛️ Architectural Overview

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 via OnTimer, 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.

🧠 Algorithmic Logic & Execution Flow

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.

Execution Phases

  1. Initialization (init_EAmode): The system performs safety checks, validates market connectivity, and reads configuration parameters from the Register module.
  2. 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.
  3. 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) or closeAll() (for risk mitigation).

Timing & Instantaneous Logic

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.

⚙️ Key Features

  • Dynamic Position Scaling: Uses init_volume and volume_factor to compute position sizes based on capital management rules.
  • Slippage Protection: The slippage_factor ensures 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, and Error event ensures complete transparency of the decision-making process.

🚀 Deployment Guide

Prerequisites

  • MetaTrader 4 terminal installed.
  • Knowledge of MetaEditor for compilation.

Installation Steps

  1. Copy all .mq4 and .mqh files to your MT4 directory: MQL4/Experts/.
  2. Open metabot4.mq4 in MetaEditor and click Compile.
  3. Attach the EA to a chart.
  4. Ensure the "AutoTrading" button is enabled in the MetaTrader interface.

📜 Legal & Licensing

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.


💡 Contribution Guidelines

MetaBot4 is designed to be a foundation for more complex strategies.

  • Extensibility: Feel free to extend Order.mqh to 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.

About

MetaBot4 is a robust, modular Expert Advisor (EA) for MetaTrader 4, designed for automated order management and high-precision execution in financial markets.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages