Skip to content

feat: add MetaTrader 5 bridge for real-time Kronos inference#292

Closed
janpauldelacruz wants to merge 10 commits into
shiyu-coder:masterfrom
janpauldelacruz:feature/mt5-bridge
Closed

feat: add MetaTrader 5 bridge for real-time Kronos inference#292
janpauldelacruz wants to merge 10 commits into
shiyu-coder:masterfrom
janpauldelacruz:feature/mt5-bridge

Conversation

@janpauldelacruz
Copy link
Copy Markdown

Summary

This PR adds the first production-grade MetaTrader 5 ↔ Kronos bridge, enabling live traders to use Kronos OHLCV forecasts directly inside MT5 Expert Advisors — the world's most widely used retail trading platform (millions of users).

What's included

  • examples/mt5_bridge/pooyer_bridge.py — Flask server that loads Kronos and serves predictions via HTTP on port 7799
  • examples/mt5_bridge/POOYER.mq5 — MT5 Expert Advisor that sends OHLCV bars via WebRequest and executes signals
  • examples/mt5_bridge/README.md — Full documentation, API reference, and quick start guide
  • examples/mt5_bridge/requirements_bridge.txt — Python dependencies

Why this matters

Kronos has no existing integration with live trading platforms. This bridge closes that gap — any MT5 user worldwide can now point their EA at a local Kronos instance and trade with foundation model forecasts.

Features

  • Multi-symbol support (XAUUSD, EURUSD, GBPUSD, BTCUSD, indices)
  • Works with Kronos-mini on CPU only — no GPU required
  • Built-in bar-hash cache (same candle = instant response)
  • ATR-based signal engine: direction + confidence + SL/TP
  • Full risk management: % sizing, spread guard, daily loss limit, kill hours, asymmetric breakeven
  • Health watchdog endpoint for EA monitoring

Tested on

XAUUSD, EURUSD, GBPUSD with Kronos-mini (CPU, ~800-2000ms inference)

janpauldelacruz and others added 10 commits May 8, 2026 02:16
…ment length

- pooyer_bridge.py: double-checked locking for _load_model() via _model_lock
- pooyer_bridge.py: inner double-check in multi_predict before inference
- pooyer_bridge.py: clear_cache() now protected by _lock
- pooyer_bridge.py: freq param in payload propagated to _run_inference timestamps
- requirements_bridge.txt: add torch>=2.0.0 (required for Kronos model)
- POOYER.mq5: ManageOpenPositions — guard cur_sl==0 before breakeven compare
- POOYER.mq5: FLAT signals handled explicitly (no longer logged as fetch failure)
- POOYER.mq5: order comment truncated to 31 chars (MT5 limit)
- POOYER.mq5: PingHealth timeout 3000ms → 1500ms to unblock bar processing
- README.md: remove reference to pooyer_dashboard.html (file not in commit)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Percentile path SL/TP, flip-resistant dynamic exit, ensemble variance
confidence, NO_TRADE state, forecast expiry, 2.5×ATR SL hard cap.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Full task-by-task build plan: bridge (ensemble variance, percentile
path SL/TP, /recheck), EA (flip-resistant exit, forecast expiry),
README, launcher scripts.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Adds KRONOS_PURE, a production Expert Advisor built exclusively around
Kronos BSQ structural tokenization with 30-path directional confluence.

Key design decisions:
- 30 independent predict() calls (sample_count=1) — preserves variance signal
  that sample_count>1 destroys by internal averaging
- M15/H1 native — BSQ structural tokens require M15+ to avoid ATR collapse
- Dir agreement gate (70%+) → 54% actual WR | 80%+ → 58% WR
- Model failover: Kronos-mini (primary) → Kronos-base (backup)
- SL: 30th percentile of predicted lows, floor 2.0×ATR, cap 2.5×ATR
- TP: 70th percentile of predicted highs
- Full trade logger → CSV (dir_agreement, confidence, MFE, MAE, hold_minutes)
- 1-click Windows installer (INSTALL_AND_RUN.bat) with stop + close-all

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Both EAs had the same bugs:

1. StringToCharArray null-terminator: used explicit StringLen() count which
   omits null, then ArrayResize(-1) removed the last JSON character (closing }).
   Bridge received malformed JSON → silent HTTP errors. Fix: WHOLE_ARRAY + sz-1.

2. Session filter was backwards: KillHour1/KillHour2 only blocked 2 specific
   hours (10 AM and 5 PM), trading freely at 1 AM, 2 AM Asian session.
   Fix: replaced with proper SessionStart/SessionEnd UTC window (7-20).

POOYER additional fixes:
- MaxPositions default 1 (prevents stacking identical signals)
- MinSLPoints/MaxSLPoints bounds guard in ExecuteTrade

KRONOS_EDGE additional fixes:
- SL/TP direction validation (guards invalid path levels from bridge)
- MinSLPoints/MaxSLPoints bounds guard in ExecuteTrade
- Removed dead NO_TRADE branch (eliminated from bridge)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant