Strategy Trader Bot

An end-to-end algorithmic trading system that pairs Hidden Markov Model regime detection with a causality DAG of 95 equities, executes through a multi-agent orchestrator, and runs as a production systemd service against the Alpaca API.

Problem

Most retail trading bots fall into one of two traps. They either fit a single signal — a moving-average crossover, a momentum factor, an RSI threshold — and assume the market behaves the same way every day, or they bolt a black-box ML model onto raw price data and learn correlations that fall apart the moment the regime changes.

Markets aren't stationary. The same signal that prints money in a low-vol uptrend gets shredded in a choppy, mean-reverting tape. I wanted to build a system that recognizes which regime it's in before deciding how to act, and that grounds its cross-asset signals in something stronger than pairwise correlation — which is famously fragile and direction-blind.

Approach

The system has three layers: a regime detector, a causal feature engine over the broader market, and a multi-agent execution layer that turns predictions into orders with conflict resolution and risk controls.

Regime detection with a Hidden Markov Model

An HMM fits hidden market states (roughly: trending up, trending down, choppy) to observed daily features. The model emits a posterior over the next day's state, which acts as a context switch — different strategies are unlocked or suppressed depending on which regime is most likely.

HMMs are a good fit here because they make the state-transition structure explicit and let you reason about regime persistence, not just point predictions. That's a property a black-box classifier doesn't give you for free.

Market causality DAG via quantile Granger causality

Pairwise correlation tells you two assets move together. It doesn't tell you which one moves first, and it collapses to noise in the tails — exactly the moments that matter for risk. Instead, the system builds a directed acyclic graph over 95 equities using quantile Granger causality, which tests whether asset A's tail moves predict asset B's tail moves over a chosen horizon.

The result is a sparse, directed graph of "who leads whom" in different market conditions. A causal feature engine then extracts DAG-based features (lagged tail drivers, fan-in centrality, regime-conditional edges) and feeds them into the downstream models. This gives the bot signal that survives the breakdown of correlation in stressed markets.

Multi-agent orchestration

Rather than collapsing every output into a single score, the system runs multiple strategy agents in parallel — each consuming the regime posterior and the DAG features — and resolves their disagreements through an orchestrator. Conflicts (e.g. one agent wants to enter long, another wants to flatten) are settled by confidence-weighted voting and risk-budget constraints. Each agent's logic is YAML-configurable, so adding or retiring a strategy doesn't require touching execution code.

Backtesting and deployment

Strategies are validated with vectorbt before any capital — paper or otherwise — is committed. The full system runs as a systemd service with auto-restart, structured logging, and a websocket connection to Alpaca during market hours. Configuration (capital limits, per-strategy weights, risk caps) is YAML-driven so changes don't require redeploying code.

Results

Replace the placeholders below with your real backtest / paper-trading numbers. Keep the comparison honest — name the period, the universe, and the benchmark.

Sharpe (backtest)
{{0.00}}
Hit rate
{{00%}}
Max drawdown
{{0%}}
Universe
95

{{Period covered, benchmark used (e.g. SPY buy-and-hold), and a one-line takeaway. If the regime-aware version beat a single-strategy baseline, say so with the spread.}}

What I'd Do Differently

{{Honest reflection. A few angles to consider — pick whichever is true:}}

  • {{Survivorship bias in the 95-equity universe — how the selection was made and what it costs.}}
  • {{HMM state count chosen by AIC/BIC vs. economic interpretability — what tradeoff you'd revisit.}}
  • {{Granger causality assumes linearity in the conditional mean — quantile version helps but doesn't fix everything.}}
  • {{Slippage / fee modeling in backtest vs. what live execution actually delivers.}}

What's Next

  • {{e.g. Extending the causality engine to options-implied data}}
  • {{e.g. Replacing the multi-agent voting with a learned meta-controller}}
  • {{e.g. Live performance tracking dashboard}}