DEV Community

MOHAMMAD Ali charooseh
MOHAMMAD Ali charooseh

Posted on Edited on

Building a Resilient Prop Trading Execution Engine for Gold (XAUUSD) Using Python and MetaTrader 5

Python Risk Controller and Bridge Implementation
`import time
import MetaTrader5 as mt5

class GoldRiskEngineInterface:
def init(self, symbol: str = "XAUUSD", max_spread_points: int = 35):
self.symbol = symbol
self.max_spread_points = max_spread_points
self.connected = False

def initialize_terminal(self) -> bool:
    if not mt5.initialize():
        print(f"[!] MetaTrader5 initialization failed, error code: {mt5.last_error()}")
        return False

    selected = mt5.symbol_select(self.symbol, True)
    if not selected:
        print(f"[!] Failed to select symbol {self.symbol}")
        return False

    self.connected = True
    print(f"[+] Terminal connected. Tracking asset: {self.symbol}")
    return True

def validate_market_health(self) -> bool:
    symbol_info = mt5.symbol_info(self.symbol)
    if symbol_info is None:
        return False

    current_spread = symbol_info.spread
    if current_spread > self.max_spread_points:
        print(f"[-] Spread blocked: {current_spread} pts exceeds max {self.max_spread_points} pts.")
        return False

    return True

def execute_protected_order(self, order_type: str, volume: float, sl_points: int, tp_points: int):
    if not self.validate_market_health():
        print("[!] Execution skipped: Risk health constraints triggered.")
        return None

    print(f"[*] Processing protected {order_type} execution on {self.symbol}...")
    return {"status": "SUCCESS", "symbol": self.symbol, "volume": volume}

def shutdown(self):
    mt5.shutdown()
    print("[+] Terminal session safely closed.")
Enter fullscreen mode Exit fullscreen mode

if name == "main":
engine = GoldRiskEngineInterface(symbol="XAUUSD", max_spread_points=30)
if engine.initialize_terminal():
engine.execute_protected_order(order_type="BUY", volume=0.5, sl_points=250, tp_points=500)
time.sleep(1)
engine.shutdown()
`
Commercial Licensing and Custom Quantitative Engineering

Looking for a production-ready institutional execution bot or a bespoke quantitative architecture for your prop firm challenge?

Available Service Tiers:

  • Tier 1 - Basic Engine License ($350): Core Python-MT5 bridge modules, standard risk configuration templates, and setup documentation.
  • Tier 2 - Enterprise Source Code ($800): Complete, unlocked source code with dynamic slippage filtering, virtual trailing stop system, and automated volatility position sizing.
  • Tier 3 - Custom Quantitative Setup and Bot Development ($1,200+): Full bespoke EA/Python engine development, multi-account setup, and VPS integration.

Direct Inquiries and Orders:

  • Telegram: @M4ro3
  • Author: Mohammad Ali Charooseh

Top comments (0)