""" 04_mql5_signal_reader.py ======================== Read MQL5 indicator signals (e.g. Alpha Trend) from MT5 GlobalVariables through the mt5bridge-ccxt wrapper, and react to them in Python. This is the recommended pattern when you want to use complex MQL5 indicators in your Python strategies without re-implementing them. """ import time from datetime import datetime import mt5bridge_ccxt def main(): exchange = mt5bridge_ccxt.mt5bridge({ "apiKey": "your-api-key", "host": "http://localhost:8080", "symbols": {"XAU/USD": "XAUUSDc"}, }) # ─── 1. List all GlobalVariables on MT5 ─────────────────────── print("All GlobalVariables on MT5:") for gv in exchange.mql5.list(): print(f" {gv['name']:60s} = {gv['value']}") # ─── 2. Read Alpha Trend signal (H1) ────────────────────────── print("\nAlpha Trend signal (XAUUSDc H1, length=14, ATR=1.0):") signal = exchange.mql5.alpha_trend_signal( symbol="XAUUSDc", timeframe="H1", length=14, atr_mult=1.0, use_volume=0, # 0=RSI mode, 1=MFI mode show_signals=1, # 0=no, 1=yes ) for k, v in signal.items(): print(f" {k:8s} = {v}") # ─── 3. Read custom indicator signal ────────────────────────── print("\nCustom signal from MY_SIGNAL_XAUUSDc_PERIOD_H1:") custom = exchange.mql5.get("MY_SIGNAL_XAUUSDc_PERIOD_H1") print(f" value = {custom}") # ─── 4. Combine indicator signal with order placement ───────── print("\n--- Live trading loop (CTRL+C to stop) ---") last_trend = None while True: try: sig = exchange.mql5.alpha_trend_signal("XAUUSDc", "H1") trend = sig.get("trend") if trend is None or trend == last_trend: time.sleep(30) continue print(f"\n[{datetime.now():%H:%M:%S}] Trend changed: {last_trend} -> {trend}") last_trend = trend # Get current positions positions = exchange.fetch_positions(["XAU/USD"]) has_long = any(p["side"] == "long" for p in positions) has_short = any(p["side"] == "short" for p in positions) ticker = exchange.fetch_ticker("XAU/USD") bid, ask = ticker["bid"], ticker["ask"] if trend == 1 and not has_long: # Trend turned long if has_short: print(" Closing short position") for p in positions: if p["side"] == "short": exchange.mql5.send_close_command(int(p["id"])) print(f" Opening LONG: {ask} sl={ask-5:.2f} tp={ask+10:.2f}") exchange.create_order( "XAU/USD", "market", "buy", 0.01, price=ask, params={"sl": ask - 5, "tp": ask + 10, "magic": 999, "comment": "alpha-trend"}, ) elif trend == -1 and not has_short: # Trend turned short if has_long: print(" Closing long position") for p in positions: if p["side"] == "long": exchange.mql5.send_close_command(int(p["id"])) print(f" Opening SHORT: {bid} sl={bid+5:.2f} tp={bid-10:.2f}") exchange.create_order( "XAU/USD", "market", "sell", 0.01, price=bid, params={"sl": bid + 5, "tp": bid - 10, "magic": 999, "comment": "alpha-trend"}, ) except KeyboardInterrupt: print("\nStopped.") break except Exception as e: print(f" Error: {e}") time.sleep(10) time.sleep(30) if __name__ == "__main__": main()