# rohitworks/mt5-smart-sltp-trailing-manager architecture

> Architecture diagram of the GitHub repository rohitworks/mt5-smart-sltp-trailing-manager, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rohitworks/mt5-smart-sltp-trailing-manager
- Repository: https://github.com/rohitworks/mt5-smart-sltp-trailing-manager

## Overview

1. **Repository shape**
   - This repo is a single-file MetaTrader 5 Expert Advisor: `MT5_Smart_SLTP_Trailing_Manager.mq5`.
   - There are no backend services, UI apps, or libraries. The whole system lives inside the MT5 terminal runtime.

2. **Primary architectural boundary**
   - The EA sits between the trader’s account state and MT5’s trade engine.
   - It does not create entries; it only observes existing pending orders and open positions, then modifies their protective levels.

3. **Execution model**
   - Behavior is driven by the MT5 EA event loop.
   - The EA repeatedly scans current trade state, identifies eligible orders/positions, and issues modification requests back to the terminal/broker.

4. **Main responsibility: trade protection**
   - For **pending orders**, it fills in missing stop loss and take profit before the order becomes a live position.
   - For **open positions**, it ensures SL/TP exist and then maintains the stop loss with trailing logic.

5. **Main responsibility: step trailing**
   - Once a trade moves into profit, the EA advances stop loss in fixed profit steps.
   - The trailing behavior is based on points, not price percentages or currency risk, which makes the math deterministic and broker-symbol aware.

6. **Strategy profile subsystem**
   - The EA contains two hard-coded strategy profiles configured through inputs.
   - Each profile defines:
     - lot size key
     - initial SL points
     - take profit points
     - profit step threshold
     - SL move step
   - This makes the EA act like a small rule engine keyed by volume.

7. **Profile selection and matching**
   - Trade volume is used as the routing key.
   - Matching is tolerant to floating-point precision via `MathAbs(volume - ProfileLot) <= LotTolerance`.
   - If no profile matches, the EA skips that order/position instead of forcing a default behavior.

8. **Filtering and scope control**
   - `OnlyCurrentSymbol` limits processing to the chart symbol.
   - `MagicNumber` limits processing to trades carrying a specific MT5 magic identifier.
   - Together these act as the EA’s main boundaries for trade selection.

9. **Data flow**
   - Input parameters define profiles and filters.
   - The EA reads account trade state from MT5.
   - It classifies each pending order or position by symbol, magic number, and lot size.
   - It computes desired SL/TP values in points.
   - It sends modification requests back through MT5 to the broker.

10. **External dependencies**
   - The only runtime dependency is the MetaTrader 5 platform and its trading API.
   - Broker-side constraints still matter implicitly: symbol digits, minimum stop distance, and order modification rules can affect whether a requested SL/TP update succeeds.

11. **Behavioral scope**
   - The EA is intentionally narrow: it manages protection only.
   - Entry logic, signal generation, and trade opening belong elsewhere, making this repo a post-entry trade management component.

12. **Diagram takeaway**
   - For an architecture diagram, model this as a single MT5 EA component with:
     - configuration inputs
     - trade-scanning logic
     - profile-matching logic
     - SL/TP enforcement
     - step trailing adjustment
     - outbound modification calls to the MT5/broker trade layer

Main entry points: [`MT5_Smart_SLTP_Trailing_Manager.mq5`](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5) (Position Protection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_mt5["MT5 Terminal"]
  node_ea["EA Loop<br/>mt5 expert advisor"]
  node_inputs["Inputs<br/>config"]
  node_filters{{"Trade Filters<br/>scope"}}
  node_scanner(("Trade Scanner<br/>state scan"))
  node_matcher{{"Profile Matcher<br/>lot routing"}}
  node_pending["Pending Protection<br/>order protection"]
  node_position["Position Protection"]
  node_trail{{"Step Trailing<br/>trailing logic"}}
  node_calc(("SL/TP Calc<br/>points math"))
  node_modify["Modify Requests<br/>trade command"]
  node_state[("Trade State<br/>account data")]
  node_engine["MT5 Trade Engine<br/>platform api"]
end

node_inputs -->|"configure"| node_filters
node_inputs -->|"profile params"| node_matcher
node_ea -->|"ticks/events"| node_scanner
node_scanner -->|"read"| node_state
node_state -->|"classify"| node_filters
node_filters -->|"eligible trades"| node_matcher
node_matcher -->|"select profile"| node_calc
node_calc -->|"desired levels"| node_pending
node_calc -->|"desired levels"| node_position
node_position -->|"maintain"| node_trail
node_pending -->|"request update"| node_modify
node_position -->|"request update"| node_modify
node_modify -->|"execute"| node_engine
node_engine -->|"reflect"| node_state

click node_ea "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_inputs "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_filters "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_scanner "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_matcher "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_pending "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_position "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_trail "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_calc "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"
click node_modify "https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ea,node_inputs,node_filters,node_scanner,node_matcher,node_pending,node_position,node_trail,node_calc,node_modify,node_state,node_engine toneBlue
```

## Components

### MT5 Terminal

Single MetaTrader 5 EA running inside the terminal runtime.

- [EA Loop](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): mt5 expert advisor · Main event-driven controller that repeatedly scans trades and applies protection rules. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Inputs](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): config · Hard-coded EA parameters for profile settings and trade filters. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Trade Filters](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): scope · Applies OnlyCurrentSymbol and MagicNumber boundaries to decide which trades are managed. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Trade Scanner](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): state scan · Reads pending orders and open positions from the terminal trade state. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Profile Matcher](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): lot routing · Matches trade volume to one of the fixed strategy profiles using tolerance-based lot comparison. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Pending Protection](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): order protection · Adds missing stop loss and take profit to eligible pending orders before activation. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Position Protection](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): position protection · Ensures open positions have protective levels and maintains them once active. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Step Trailing](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): trailing logic · Advances stop loss in fixed profit steps as trades move into profit. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [SL/TP Calc](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): points math · Computes desired stop loss and take profit levels from symbol points and profile settings. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- [Modify Requests](https://github.com/rohitworks/mt5-smart-sltp-trailing-manager/blob/main/MT5_Smart_SLTP_Trailing_Manager.mq5): trade command · Sends order and position modification requests back through the MT5 trade API. · `MT5_Smart_SLTP_Trailing_Manager.mq5`
- **Trade State**: account data · Current pending orders, open positions, symbol, volume, and magic metadata read from MT5.
- **MT5 Trade Engine**: platform api · MetaTrader 5 trading layer that executes and validates modification requests.

## Connections

- Inputs → Trade Filters: configure · Filter parameters define which trades are in scope.
- Inputs → Profile Matcher: profile params · Profile lot and trailing settings are loaded from inputs.
- EA Loop → Trade Scanner: ticks/events · The EA event loop triggers repeated trade scans.
- Trade Scanner → Trade State: read · Scans current pending orders and open positions from terminal state.
- Trade State → Trade Filters: classify · Each trade is checked against symbol and magic boundaries.
- Trade Filters → Profile Matcher: eligible trades · Only in-scope trades proceed to profile matching.
- Profile Matcher → SL/TP Calc: select profile · Matched profile provides SL, TP, and trailing step values.
- SL/TP Calc → Pending Protection: desired levels · Calculated stop loss and take profit are applied to pending orders.
- SL/TP Calc → Position Protection: desired levels · Calculated protective levels are applied to open positions.
- Position Protection → Step Trailing: maintain · Open positions enter the step trailing adjustment path.
- Pending Protection → Modify Requests: request update · Pending-order protection is sent as a modification request.
- Position Protection → Modify Requests: request update · Position protection and trailing changes are sent as modification requests.
- Modify Requests → MT5 Trade Engine: execute · MT5 trade API forwards the change to the broker-side engine.
- MT5 Trade Engine → Trade State: reflect · Successful updates are reflected back in the live trade state.
