Skip to content

03 Execution and MT5 Bridge

ElMoorish edited this page Sep 12, 2026 · 1 revision

⚡ 03. Execution & MT5 Bridge

High-performance quantitative models often fail in production due to execution latency, broker spread widening, and adverse selection. TriDomainMoE incorporates a native C-extension bridge to MetaTrader 5 (src/execution/mt5_bridge.py) operating with sub-20ms latency and four institutional defense vectors.


1. Live Execution Loop

  Live MT5 Market Feed ────> Extract Microstructure (Tick/M5) ────> TriDomainMoE Inference
                                                                            │
      ┌─────────────────────────────────────────────────────────────────────┘
      ▼
  Vector 1: Anti-Adverse Order Book Shield (OFI < -0.20 or VPIN > 0.65?) ──> REJECT
      │ PASS
      ▼
  Vector 3: Router Shannon Entropy Gating (H >= 1.00 -> High Drift Filter) ──> REJECT
      │ PASS
      ▼
  Conviction Check (Sizing s_t >= 0.20?) ──> REJECT
      │ PASS
      ▼
  Dispatch MT5 Market Order with Hard Stop-Loss & Take-Profit Brackets
      │
      ▼
  Vector 2: Active Position Breakeven Ratchet Polling Loop (Every 5s)
      │
      └──> When Profit >= +1.5σ (50% TP distance) ──> Ratchet SL to Entry + Spread + 2 pts

2. The 4 Enhancement Vectors

2.1 Vector 1: Pre-Trade Anti-Adverse Order Book Shield

  • BUY Rejection: Triggered if normalized_ofi < -0.20 or vpin > 0.65. Prevents buying into informed institutional limit book dumps.
  • SELL Rejection: Triggered if normalized_ofi > 0.20 or vpin > 0.65. Prevents shorting directly into aggressive buyer absorption walls.

2.2 Vector 2: Volatility-Adaptive Breakeven Ratchet

  • Trigger: Once unrealized profit reaches $50%$ of the Take-Profit distance ($+1.5\sigma$ price expansion).
  • Action: An asynchronous TRADE_ACTION_SLTP modification ratchets the Stop-Loss to $\text{Entry Price} + \text{Floating Spread} + 2\text{ points}$.
  • Effect: Guarantees that profitable trades cannot reverse into a loss, converting potential round-trip losses into risk-free scratch trades ($\ge $0.00$).

2.3 Vector 3: Router Shannon Entropy Gating

  • Condition: Router entropy $H(g) \ge 1.00$.
  • Action: Elevates the minimum directional drift forecast requirement: $$|\hat{y}_{\text{pred}}| \ge 0.0380 \quad (\text{raised from standard } 0.0300)$$
  • Effect: Prunes choppy low-conviction trades when technical and macro experts disagree during market consolidation.

2.4 Vector 4: London / New York Overlap Sizing Boost

  • Condition: Trading hour between 12:00 UTC and 18:00 UTC.
  • Action: Scales conviction sizing $s_t$ by $+15%$.
  • Effect: Concentrates exposure during peak institutional depth of book and narrowest broker spreads.

3. Position Sizing & Prop-Firm Risk Clamp

Lot sizing is calculated dynamically from the MT5 account balance and stop-loss distance:

$$\text{Lots} = \frac{\text{Equity} \times \text{Risk Pct}}{|\text{Entry} - \text{SL}| \times \text{Contract Size} \times \text{Tick Value}}$$

  • Default Risk per Trade: $0.20%$ ($0.0020$).
  • Single-Deal Constraint: Only one active position is permitted per symbol. Concurrent duplicate exposure is strictly prohibited to maintain prop-firm drawdown safety.
  • Slippage & Spread Guard: If the floating spread exceeds $2.5\times$ the rolling 20-period median spread, trade dispatch is paused until liquidity returns.

Clone this wiki locally