""" 05_realtime_tick.py =================== Real-time tick monitoring with mt5bridge-ccxt. MT5Bridge uses polling-based "watching" (no WebSocket). The watch_ticker method blocks until a new tick is detected, then returns it. This is suitable for most strategies that need to react to price changes within ~1 second. For high-frequency trading (< 100ms latency), upgrade MT5Bridge to support WebSocket (see mql5/README.md and the WebSocket section in the main README). """ import asyncio from datetime import datetime import mt5bridge_ccxt async def main(): exchange = mt5bridge_ccxt.mt5bridge({ "apiKey": "your-api-key", "host": "http://localhost:8080", "symbols": {"XAU/USD": "XAUUSDc"}, }) # ─── Option 1: Just call watch_ticker in a loop ────────────── print("=== Watching XAU/USD ticks (poll-based) ===\n") for i in range(10): # Watch 10 ticks ticker = await exchange.watch_ticker( "XAU/USD", params={"poll_interval_ms": 500}, # poll every 500ms ) now = datetime.now().strftime("%H:%M:%S.%f")[:-3] print(f"[{now}] #{i+1:2d} bid={ticker['bid']:.2f} " f"ask={ticker['ask']:.2f} spread={ticker['ask']-ticker['bid']:.2f}") # ─── Option 2: Watch multiple symbols concurrently ────────── print("\n=== Watching multiple symbols concurrently ===\n") async def watch_one(sym): try: while True: t = await exchange.watch_ticker(sym, params={"poll_interval_ms": 1000}) now = datetime.now().strftime("%H:%M:%S") print(f"[{now}] {sym:8s} bid={t['bid']:.2f} ask={t['ask']:.2f}") except asyncio.CancelledError: pass tasks = [ asyncio.create_task(watch_one("XAU/USD")), asyncio.create_task(watch_one("EUR/USD")), ] # Run for 10 seconds then stop await asyncio.sleep(10) for t in tasks: t.cancel() await asyncio.gather(*tasks, return_exceptions=True) # ─── Option 3: Watch new bars (K-line formation) ──────────── print("\n=== Watching XAU/USD H1 bars (new bar formation) ===\n") bar_count = 0 while bar_count < 3: # Watch for 3 new bars ohlcv = await exchange.watch_ohlcv( "XAU/USD", "1h", limit=3, params={"poll_interval_ms": 5000}, # check every 5s ) if ohlcv: latest = ohlcv[-1] bar_time = datetime.fromtimestamp(latest[0] / 1000).strftime("%Y-%m-%d %H:%M") print(f"New bar: time={bar_time} O={latest[1]:.2f} H={latest[2]:.2f} " f"L={latest[3]:.2f} C={latest[4]:.2f}") bar_count += 1 if __name__ == "__main__": asyncio.run(main())