Under the hood

A Python engine for the math. A dashboard for the human.

Mercurio is a hybrid system: a Python trading engine where the strategy, risk, and execution live, and a Next.js dashboard for watching it think. They talk over Redis in real time, and the whole thing is built to fail safe.

The architecture

Two halves, one loop

Hybrid by design

A Python engine handles signal generation, risk, and order execution. A Next.js and TypeScript dashboard handles visualization and control. Each language is used for what it is best at.

Event-driven

The engine publishes state to the dashboard over Redis pub/sub, so what you see reflects the engine's reality within moments — no polling, no guessing.

Fail-safe

If the AI layer goes down, the engine keeps trading on technicals. If data goes stale, it pauses. Degradation is graceful by construction, not by accident.

The engine

Six modules, each with one job

The trading engine is deliberately decomposed. Every responsibility lives in exactly one place, which is what makes the system possible to reason about — and to trust with risk.

orchestrator

The main trading loop — pulls data, runs the gates, and coordinates every other module on a fixed cadence.

risk_manager

Enforces every limit in code: per-trade risk, exposure caps, loss limits, and the drawdown circuit breaker.

order_executor

Owns the Alpaca order lifecycle — submission, bracket legs, retries, partial fills, and cancellation.

position_sizer

Translates a signal and a stop distance into a share count under fixed-fractional risk.

portfolio_manager

Tracks open positions, cost basis, and realized and unrealized P&L as the source of truth.

watchdog

Monitors engine health, data freshness, and API connectivity, and trips safeguards when something is wrong.

Data & intelligence

State you can trust, intelligence you can lose

The data layer is the spine of the system. The intelligence layer is a bonus that the engine can live without.

PostgreSQL — persistence

A managed Postgres database is the durable record of trades and state. After a painful early lesson, anything that affects a trading decision is persisted here or in Redis — never held only in memory.

Redis — real-time state

Redis carries live engine state and the pub/sub stream to the dashboard, and tracks API rate limits so the engine stays a good citizen of every upstream service.

Alpaca — data & brokerage

Alpaca provides market data and the paper brokerage. Orders are validated against buying power, submitted, and reconciled against Alpaca's records daily.

The AI layer — optional alpha

A machine-intelligence service handles sentiment, regime classification, and trade review — each with a rule-based fallback. If it is unavailable, the engine degrades to technicals and keeps running.

Reliability

Lessons, engineered in

Every reliability feature here exists because something once went wrong. The system remembers so it cannot repeat the mistake.

State that survives restarts

Kill switches, loss-streak counters, and the drawdown breaker all persist. A strategy that was killed for poor performance stays killed through a restart — it is never silently re-enabled.

Idempotent daily reset

The daily reset runs once per calendar day, tracked in the state store, so a mid-day restart cannot accidentally re-arm something that was deliberately switched off.

Circuit breakers

Repeated API failures, stale data, or breached loss limits trip safeguards that pause or halt trading until conditions are healthy again.

Daily reconciliation

The local ledger is compared against the broker of record every day, so the engine's view of the world and Alpaca's never quietly drift apart.

Deployment & stack

Shipped on managed infrastructure

Mercurio runs on DigitalOcean's App Platform with a managed PostgreSQL database and auto-deploys on every push. The stack is boring on purpose — boring is reliable.

Python 3.11+FastAPIpandasnumpytaNext.js 16TypeScriptTailwind CSSPostgreSQLRedisAlpaca

Read the source

The engine, the risk manager, and the backtester are all open. See exactly how the math becomes a trade.