How rohitworks/mt5-smart-sltp-trailing-manager works
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 Position Protection
Components 12
MT5 Terminal
Single MetaTrader 5 EA running inside the terminal runtime.
- EA Loop · Main event-driven controller that repeatedly scans trades and applies protection rules.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Inputs · Hard-coded EA parameters for profile settings and trade filters.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Trade Filters · Applies OnlyCurrentSymbol and MagicNumber boundaries to decide which trades are managed.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Trade Scanner · Reads pending orders and open positions from the terminal trade state.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Profile Matcher · Matches trade volume to one of the fixed strategy profiles using tolerance-based lot comparison.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Pending Protection · Adds missing stop loss and take profit to eligible pending orders before activation.
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 · Advances stop loss in fixed profit steps as trades move into profit.
MT5_Smart_SLTP_Trailing_Manager.mq5 - SL/TP Calc · Computes desired stop loss and take profit levels from symbol points and profile settings.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Modify Requests · Sends order and position modification requests back through the MT5 trade API.
MT5_Smart_SLTP_Trailing_Manager.mq5 - Trade State · Current pending orders, open positions, symbol, volume, and magic metadata read from MT5.
- MT5 Trade Engine · MetaTrader 5 trading layer that executes and validates modification requests.
Connections 14
- 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.