# mo-khalifa96/forex-trading-bot architecture

> Architecture diagram of the GitHub repository mo-khalifa96/forex-trading-bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mo-khalifa96/forex-trading-bot
- Repository: https://github.com/mo-khalifa96/forex-trading-bot

## Overview

##### Main Components
- Main Program (Forex Trading Bot.py & Jupyter version): Acts as the controller that orchestrates the trading process.
- DataProcessing Module (DataProcessing.py): Contains functions to calculate the SuperTrend indicator, process candlestick data, and analyze market trends.
- OrderProcessing Module (OrderProcessing.py): Handles trade order logic including calculating lot sizes, stop loss/take profit, and executing buy/sell orders.
- TimeProcessing Module (TimeProcessing.py): Manages time-based events such as monitoring market open/close times, new candle updates, and processing the designated timeframes for each forex pair.
- External Trading Platform (MetaTrader5 Terminal): The interface where the bot logs into a personal account and executes market orders.
- User Input/Configuration: Prompts for credentials and server details at startup.

- From the “TimeProcessing” module, indicate its role in scheduling tasks and signaling new candle updates to the other modules.
- That the “OrderProcessing” module receives signals (buy/sell conditions) derived from the processed data and interacts with the MetaTrader5 terminal to execute orders.

##### Architectural Patterns
- Modularity: The separation into specialized modules (DataProcessing, OrderProcessing, TimeProcessing) shows the Single Responsibility Principle.
- Event-Driven / Continuous Monitoring: The design uses nested loops to continuously monitor market conditions and time events (such as new candle arrivals).
- External Integration: Highlight the connection to the MetaTrader5 terminal and the real-time data and order flow between the client and the platform.
- Error Handling/Resilience: Note that the bot has mechanisms to check for internet connectivity, market open/close status, and handles order execution issues.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Subgraph for Core Bot Controller containing both main program versions
    subgraph "Core Bot Controller"
        BotPython["Forex Trading Bot.py"]:::core
        BotJupyter["Forex Trading Bot (Jupyter version)"]:::core
    end

    %% Other modules and components
    DataProc["Data Processing Module"]:::core
    OrderProc["Order Processing Module"]:::core
    TimeProc["Time Processing Module"]:::core
    UserIn["User Input/Configuration"]:::user
    MT5["MetaTrader5 Terminal"]:::external

    %% Data Flow / Control Flow Arrows
    UserIn -->|"provides"| BotPython
    UserIn -->|"provides"| BotJupyter

    BotPython -->|"processes"| DataProc
    BotPython -->|"executes"| OrderProc
    BotPython -->|"schedules"| TimeProc

    BotJupyter -->|"processes"| DataProc
    BotJupyter -->|"executes"| OrderProc
    BotJupyter -->|"schedules"| TimeProc

    TimeProc -->|"newCandle"| DataProc
    TimeProc -->|"timeSignal"| OrderProc

    DataProc -->|"marketData"| OrderProc
    DataProc -->|"processedData"| BotPython
    DataProc -->|"processedData"| BotJupyter

    OrderProc -->|"orderCall"| MT5
    BotPython -->|"login"| MT5
    BotJupyter -->|"login"| MT5

    %% Loop back arrow to represent continuous monitoring in Time Processing
    TimeProc -->|"repeat"| TimeProc

    %% Click Events
    click BotPython "https://github.com/mo-khalifa96/forex-trading-bot/blob/main/Forex Trading Bot.py"
    click BotJupyter "https://github.com/mo-khalifa96/forex-trading-bot/blob/main/Forex Trading Bot (Jupyter version).ipynb"
    click DataProc "https://github.com/mo-khalifa96/forex-trading-bot/blob/main/DataProcessing.py"
    click OrderProc "https://github.com/mo-khalifa96/forex-trading-bot/blob/main/OrderProcessing.py"
    click TimeProc "https://github.com/mo-khalifa96/forex-trading-bot/blob/main/TimeProcessing.py"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef user fill:#FFFFE0,stroke:#000,stroke-width:2px;
    classDef external fill:#90EE90,stroke:#000,stroke-width:2px;
```
