# kr0st/trade_replicator architecture

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

- Interactive diagram: https://gitdiagram.com/kr0st/trade_replicator
- Repository: https://github.com/kr0st/trade_replicator

## Overview

##### Project type and purpose
- This is an open-source Forex trade-replicator tool (not a full-stack web app).
- It consists of two MQL4 scripts (master and slave) for MetaTrader 4 and a Java-based master strategy for JForex.
- Purpose: copy trades from a “master” account to one or more “slave” accounts via a PostgreSQL database.

##### File structure
- Top-level files:
- trade_replicator_master.mq4 – MT4 master script
- trade_replicator_slave.mq4 – MT4 slave script
- TradeReplicatorMaster.java – JForex master strategy
- readme.txt / license.txt – documentation & BSD license
- No separate “src” or “lib” folders: all components live at root.
- The MQL4 scripts depend on the PostgreMQL4 DLL (external library) to connect to PostgreSQL.
- The Java strategy depends on a PostgreSQL JDBC driver.

##### Extract architecture insights from the README
- Masters generate and write trade events to two tables: master_trades and slave_trades.
- Slave scripts poll the DB (once per second) for new/closed trades and execute them in MT4.
- Java version runs inside JForex, writes to the same DB.
- Configuration is done by embedding parameters (DB credentials, master IDs) in script code or at runtime.

##### Detail to capture
- Master ID generation & uniqueness constraint
- Table creation step on first run
- Error handling path (slave writes error code back to DB)
- Multi-master subscription capability (slave side can list multiple master IDs)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Masters group
    subgraph "Masters"
        direction TB
        MT4Master["MT4 Master Script"]:::client
        JForexMaster["JForex Master Strategy"]:::client
    end

    %% Slaves group
    subgraph "Slaves"
        direction TB
        MT4Slave["MT4 Slave Script"]:::client
    end

    %% Dependencies
    subgraph "Dependencies"
        direction TB
        PostgresMQL4DLL["PostgreMQL4 DLL"]:::library
        JDBCDriver["PostgreSQL JDBC Driver"]:::library
    end

    %% Database
    PostgreSQL["PostgreSQL Database\n(master_trades, slave_trades)"]:::database

    %% Documentation
    subgraph "Documentation"
        direction TB
        Readme["readme.txt"]:::library
        License["license.txt"]:::library
    end

    %% Relationships
    MT4Master -->|"Writes trade rows"| PostgreSQL
    JForexMaster -->|"Writes trade rows"| PostgreSQL
    PostgreSQL -->|"Poll new trades (1s interval)"| MT4Slave
    MT4Slave -->|"Executes trades & writes status/closed rows"| PostgreSQL
    MT4Slave -->|"Writes error codes on failure"| PostgreSQL

    %% Dependency connections
    MT4Master --> PostgresMQL4DLL
    MT4Slave --> PostgresMQL4DLL
    JForexMaster --> JDBCDriver

    %% Click events
    click MT4Master "https://github.com/kr0st/trade_replicator/blob/master/trade_replicator_master.mq4"
    click MT4Slave "https://github.com/kr0st/trade_replicator/blob/master/trade_replicator_slave.mq4"
    click JForexMaster "https://github.com/kr0st/trade_replicator/blob/master/TradeReplicatorMaster.java"
    click Readme "https://github.com/kr0st/trade_replicator/blob/master/readme.txt"
    click License "https://github.com/kr0st/trade_replicator/blob/master/license.txt"

    %% Styles
    classDef client fill:#9f6,stroke:#333,stroke-width:1px
    classDef database fill:#add8e6,stroke:#333,stroke-width:1px
    classDef library fill:#ffa500,stroke:#333,stroke-width:1px
```
