# ersingencturk/freeexpertadvisor architecture

> Architecture diagram of the GitHub repository ersingencturk/freeexpertadvisor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ersingencturk/freeexpertadvisor
- Repository: https://github.com/ersingencturk/freeexpertadvisor

## Overview

##### Project type and purpose
- This is an open-source Forex Expert Advisor (EA) script written in MQL4 for the MetaTrader 4 platform.
- Its purpose is to run on live or back-testing charts, apply three overbought/oversold strategies, and automatically open and close scalping trades on the 5-minute timeframe with built-in risk management.

##### File structure
- Top-level:
– FreeExpertAdvisor.mq4 – the entire EA code (no separate frontend/backend, all logic lives here)
– README.md – project overview, settings, strategy descriptions
– LICENSE
- No subdirectories: implies a single-component codebase that plugs into the MT4 terminal.

##### README for additional insights
- Core features: three strategies, true stop-loss plus virtual stop-loss/take-profit via ATR multiplier, FIFO compliance, one trade at a time, dynamic lot sizing.
- Supported symbols and broker requirements.
- Configurable parameters: fixed lots, dynamic lot % risk, hard stop-loss, GMT offset, chart symbol.
- Backtesting results shown via screenshots.

a. MetaTrader 4 Terminal – host application that loads the EA and provides market data & order API.
b. Market Data Feed – real-time tick data (or historical data in the back-tester).
c. FreeExpertAdvisor EA – subdivide into internal modules:
- Initialization & Parameter Loader
- Strategy Engine – implements three overbought/oversold algorithms
- ATR Module – computes current ATR and virtual stop-loss/take-profit levels
- Risk Management Module – dynamic lot calculation or fixed lot, single-order enforcement
- Order Execution Module – MT4 order/send API calls, real SL vs virtual SL/TP handling
- Logging/Monitoring (optional)
d. Broker Server – receives orders from MT4, returns fills & trade status.
e. Backtest Engine (MT4) – alternative path where historical data flows into the EA.

##### Relationships and interactions
- From Strategy/Risk Management → Order Execution → Broker Server, then back to EA for confirmations.
- Parameter Loader feeds the Strategy and Risk Management modules at startup.
- Two modes: Live trading (real ticks) vs Backtesting (historical data). Show these as parallel paths.

##### Architectural patterns & design principles
- Modular design within a single script: separate concerns (init, strategies, risk, execution).
- Event-driven: OnTick callback drives execution loop.
- Single instance enforcement & FIFO compliance ensure orderly trade flow.
- Configuration via input parameters for flexibility and reuse.

##### Technologies, frameworks, and libraries
- MQL4 language (MetaQuotes proprietary), MT4 platform APIs (OnInit, OnTick, OrderSend, etc.).
- No external libraries; all code is self-contained.

##### Detail level
- Essential MT4 interactions (OnInit, OnTick, OnDeinit, OrderSend, OrderModify, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Sources
    MarketData["Market Data Feed"]:::feed
    BacktestEngine["MT4 Backtest Engine"]:::backtest

    %% Terminal
    MT4Terminal["MetaTrader 4 Terminal"]:::terminal

    %% Expert Advisor Modules
    subgraph "FreeExpertAdvisor EA"
        direction TB
        Param["Initialization & Parameter Loader"]:::module
        Strat["Strategy Engine"]:::module
        ATR["ATR Computation Module"]:::module
        Risk["Risk Management Module"]:::module
        Exec["Order Execution Module"]:::module
        Log["Logging/Monitoring"]:::module
    end

    %% Broker
    BrokerServer["Broker Server"]:::broker

    %% Documentation
    README["Project Documentation & Configuration"]:::docs
    LICENSE["License Information"]:::docs

    %% Flows
    MarketData --> MT4Terminal
    BacktestEngine --> MT4Terminal
    MT4Terminal -->|OnInit| Param
    Param -->|loads parameters| Strat
    Strat -->|signals| ATR
    ATR -->|virtual SL/TP| Risk
    Risk -->|trade decisions| Exec
    Exec -->|OrderSend/Modify| BrokerServer
    BrokerServer -->|fills/status| Exec
    Exec -->|confirmations| MT4Terminal
    MT4Terminal -->|OnTick events| Log

    %% Click Events
    click Param "https://github.com/ersingencturk/freeexpertadvisor/blob/master/FreeExpertAdvisor.mq4"
    click Strat "https://github.com/ersingencturk/freeexpertadvisor/blob/master/FreeExpertAdvisor.mq4"
    click ATR "https://github.com/ersingencturk/freeexpertadvisor/blob/master/FreeExpertAdvisor.mq4"
    click Risk "https://github.com/ersingencturk/freeexpertadvisor/blob/master/FreeExpertAdvisor.mq4"
    click Exec "https://github.com/ersingencturk/freeexpertadvisor/blob/master/FreeExpertAdvisor.mq4"
    click Log "https://github.com/ersingencturk/freeexpertadvisor/blob/master/FreeExpertAdvisor.mq4"
    click README "https://github.com/ersingencturk/freeexpertadvisor/blob/master/README.md"
    click LICENSE "https://github.com/ersingencturk/freeexpertadvisor/tree/master/LICENSE"

    %% Styles
    classDef feed fill:#FFEB3B,stroke:#FBC02D,color:#000
    classDef backtest fill:#FFF59D,stroke:#F9A825,color:#000
    classDef terminal fill:#90CAF9,stroke:#1E88E5,color:#000
    classDef module fill:#A5D6A7,stroke:#388E3C,color:#000
    classDef broker fill:#CE93D8,stroke:#8E24AA,color:#000
    classDef docs fill:#B0BEC5,stroke:#607D8B,color:#000
```
