How okeson453/ghost-grid works
Purpose and Entry Points
GHOST GRID is a Python 3.11+ MT5 FX scalping system for paper/live deployment on a Windows VPS. It ingests MT5 market events, builds multi-timeframe market state, scores trade confluence, gates/risk-checks orders, manages positions, and enforces portfolio-level emergency exits.
Primary runtime entry point: main.py. Operational entry points include scripts/start.py, scripts/backtest_run.py, and database migration runner scripts/migrate_db.py. MT5 hosts the companion Expert Advisor: mt5_ea/GhostGrid.mq5.
MT5 Bridge and Market-Data Pipeline
The MT5 EA communicates with Python through Windows Named Pipes. Python-side bridge components handle protocol framing, reads/writes, health, and reconnect behavior: bridge/pipe_client.py, bridge/reader.py, bridge/protocol.py, bridge/writer.py.
Incoming ticks are routed into OHLCV aggregation and immutable market snapshots. The data boundary is represented by schemas/snapshots before strategy logic consumes it: data/feed_router.py, data/aggregator.py, data/snapshot_builder.py, data/schema.py. Derived inputs include ATR, VWAP, CVD, and regime data in data/atr.py, data/vwap.py, and data/cvd_engine.py.
Regime and Confluence Scoring
Market regime classification provides context for decisions through regime/classifier.py, regime/fingerprint.py, and regime/session.py.
Three independent engines score a snapshot: HMP market structure (scoring/hmp/engine.py), HLCP liquidity/trend confluence (scoring/hlcp/engine.py), and MPP order-flow/projection inputs (scoring/mpp/engine.py). scoring/fusion.py combines these into the H_c composite, while scoring/gate.py applies hysteresis and produces actionable versus rejected/watch-only decisions. Score models form the contract between strategy and execution: scoring/models.py.
Risk, Execution, and Position Lifecycle
Approved signals cross a pre-trade risk boundary: risk/governor.py, risk/validator.py, and risk/sizer.py determine whether and how much may be traded. Risk constants/settings are centralized under config/constants.py and config/settings.py.
execution/commander.py converts approved actions into MT5 order/close commands through execution/dispatcher.py; execution/paper.py supplies paper-mode behavior and execution/fill_handler.py handles confirmations. Positions are registered and progressed through explicit lifecycle/exit logic in positions/registry.py, positions/state_machine.py, and positions/exit_engine.py, including trailing and CVD-based exits.
Portfolio Guardian and Emergency Controls
Portfolio state aggregates equity, PnL, correlation, and operating mode: portfolio/state.py, portfolio/ledger.py, portfolio/equity_tracker.py, portfolio/mode_automaton.py. This state feeds both trading permissions and safety systems.
The nuclear guardian polls portfolio conditions, identifies trigger conditions, concurrently closes positions, and imposes cooldown/day-halt behavior: nuclear/controller.py, nuclear/triggers.py, nuclear/executor.py, nuclear/cooldown.py. Separately, the watchdog is an OS thread intended to remain independent of the asyncio control loop and can directly issue emergency pipe commands: watchdog/thread.py, watchdog/emergency.py.
Persistence, Control Plane, and Observability
SQLite is the durable event/state boundary, managed by db/connection.py, db/writer.py, db/reader.py, with schema evolution in db/migrations/001_initial_schema.sql. Runtime artifacts are stored under data_store/.
Telegram is an external operator-control service, connected at orchestration level via telegram/bot.py, telegram/commands.py, and telegram/alerts.py; it supports status, pause/resume, position listing, and manual nuclear actions. Monitoring writes metrics/trade journals and evaluates drift through observability/metrics.py, observability/trade_journal.py, and observability/drift_detector.py.
Runtime and Deployment Boundaries
The main control plane uses asyncio for feed processing, scoring, execution, and nuclear polling; the watchdog uses a separate thread. External dependencies are MT5/MetaTrader named-pipe IPC, SQLite via async access, Telegram Bot API, and NumPy/Pandas. Docker assets exist in Dockerfile and docker-compose.yml, but the MT5 named-pipe integration makes Windows VPS deployment the material runtime target.
Main entry points: pipe_client.py Named-pipe bridge · commander.py Order commander · snapshot_builder.py Market snapshots · state.py Portfolio state
Components 17
MT5 boundary
Windows named-pipe integration with the EA.
- MT5 Expert Advisor · MT5-side market and order endpoint.
mt5_ea/GhostGrid.mq5 - Named-pipe bridge · Frames messages and manages pipe health and reconnects.
bridge/pipe_client.py
Decision pipeline
Market state through gated trade actions.
- Market snapshots · Aggregates ticks into immutable multi-timeframe OHLCV state.
data/snapshot_builder.py - Derived indicators · Produces ATR, VWAP, CVD, and related inputs.
data/atr.py - Regime classifier · Supplies market context and session state.
regime/classifier.py - Confluence engines · Independent HMP, HLCP, and MPP scoring.
scoring/hmp/engine.py - Fusion and gate · Combines scores into H_c and applies hysteresis.
scoring/fusion.py
Risk and control
Portfolio safety and operator controls.
- Pre-trade risk · Validates trade permission and sizes approved actions.
risk/governor.py - Portfolio state · Aggregates equity, PnL, correlation, and operating mode.
portfolio/state.py - Nuclear guardian · Triggers concurrent closes and cooldown or day halts.
nuclear/controller.py - Independent watchdog · Can directly send emergency pipe commands outside asyncio.
watchdog/thread.py - Telegram control · Receives status and manual safety commands; sends alerts.
telegram/bot.py
Execution lifecycle
Order dispatch, fills, and position exits.
- Order commander · Converts approved actions into MT5 order and close commands.
execution/commander.py - Position lifecycle · Registers fills and applies state-machine, trailing, and CVD exits.
positions/state_machine.py
Durability and observability
SQLite-backed state and operational telemetry.
- SQLite event state · Durable event and state boundary.
db/connection.py - Metrics and journal · Records metrics and trades and evaluates drift.
observability/metrics.py
Other components
- Async control plane · Coordinates feed processing, decisions, execution, and nuclear polling.
main.py
Connections 25
- MT5 Expert Advisor → Named-pipe bridge: ticks, events, fills
- Named-pipe bridge → Market snapshots: incoming market events
- Market snapshots → Derived indicators: OHLCV snapshots
- Market snapshots → Regime classifier: snapshot context
- Derived indicators → Confluence engines: ATR, VWAP, CVD
- Regime classifier → Confluence engines: regime context
- Market snapshots → Confluence engines: multi-timeframe state
- Confluence engines → Fusion and gate: component scores
- Fusion and gate → Pre-trade risk: actionable signal
- Portfolio state → Pre-trade risk: trading permission
- Pre-trade risk → Order commander: sized approved order
- Order commander → Named-pipe bridge: order and close commands
- Named-pipe bridge → Order commander: confirmations
- Order commander → Position lifecycle: fills
- Position lifecycle → Portfolio state: position and PnL state
- Portfolio state → Nuclear guardian: equity and safety state
- Nuclear guardian → Order commander: emergency close actions
- Independent watchdog → Named-pipe bridge: direct emergency command
- Telegram control → Async control plane: operator commands
- Async control plane → Telegram control: alerts and status
- Async control plane → SQLite event state: persist runtime state
- Position lifecycle → SQLite event state: durable position events
- Portfolio state → SQLite event state: ledger and equity state
- Async control plane → Metrics and journal: runtime telemetry
- SQLite event state → Metrics and journal: trade journal data