Skip to main content
Back to Projects

SmallCapTrader

Algorithmic trading platform for small-cap stocks

PythonFastAPINext.jsReactTypeScriptPostgreSQLQuestDBRedisDocker

Private Repository

Problem

Small-cap stock trading demands real-time detection of fast-moving stocks, sophisticated multi-strategy signal arbitration, and reliable execution across brokers. Existing retail platforms either offer only basic indicators with manual order entry, or lock advanced features behind institutional pricing. There is no self-hosted, full-featured platform that handles the entire pipeline — from market data streaming through automated strategy discovery to live execution at scale.

Approach

Built a production-grade trading system with a Python 3.12+ async backend (FastAPI + Granian) and a Next.js 16 frontend. The platform covers the full trading lifecycle: real-time market data ingestion, multiple built-in trading strategies spanning momentum, pullback, breakout, and volume-driven patterns, a strategy fingerprinting subsystem that profiles what existing strategies catch and directs rule mining toward the gaps, an automated rule-mining engine that discovers and promotes new strategies to live trading, backtesting with parameter optimization, multi-broker execution across Alpaca and Interactive Brokers, and a production correctness layer covering reconnect behavior, trade-accounting, and historical pricing fidelity.

Market Data Feeds (WebSocket) INGESTION LAYER Real-time Streaming · Symbol Scanner STRATEGY ENGINE Multiple Strategies · Signal Arbitration · Backtesting RULE MINING ENGINE Beam Search · Auto-Promotion EXECUTION LAYER Alpaca · Interactive Brokers DUAL MODE Monolithic Sharded (scalable) DATA LAYER PostgreSQL QuestDB (TSDB) Redis Next.js Frontend

Architecture

Clean Architecture with Domain-Driven Design, supporting dual-mode deployment — monolithic for development and sharded microservices for production. The same codebase switches between modes via a configuration flag without code changes.

Monolithic Mode

All services run in a single Python process: market data parsing, trading strategies, position monitoring, and Telegram notifications. Simple deployment, minimal latency between components.

Sharded Mode

Horizontally scalable architecture. A stream router parses market data and distributes it to shard workers via Redis pub/sub, partitioned by symbol range. Each shard worker runs all strategies independently. A dedicated tick recorder writes all market data to QuestDB for backtesting replay. A central signal consumer receives validated signals from all shards and routes them to broker execution.

Data Layer

  • PostgreSQL + asyncpg — Relational domain data: trades, positions, orders, strategy configs, discovered rules
  • QuestDB — High-throughput time-series: tick data, OHLCV bars (InfluxDB Line Protocol ingestion)
  • Redis — Caching, pub/sub for shard communication, real-time state

Trading Strategies

Multiple built-in strategies spanning different market conditions and detection types:

  • Momentum Detection — Real-time identification of fast-moving and slow-moving stocks using volume and price regime analysis
  • Pullback Entries — Multiple pullback-based strategies for entering running stocks at favorable prices
  • Breakout Patterns — Volume-confirmed breakout strategies across consolidation, range, and trend setups
  • Mean-Reversion — Strategies targeting key technical levels for support/reclaim entries
  • Discovered Rules — Auto-mined strategies from the rule-mining engine, promoted to live trading after validation

Each strategy includes configurable risk parameters, signal confidence scoring, priority-based arbitration when multiple strategies fire simultaneously on the same symbol, and detection-type filtering so strategies only activate on matching stock behavior (fast-moving, slow-moving, or both).

Key Technical Decisions

Strategy Fingerprinting

Before the rule-mining engine proposes new rules, the platform first profiles what each existing strategy already catches across historical data, measures coverage and cross-strategy overlap, and generates curated seed candidates that target the gaps rather than searching blindly. The fingerprinting dashboard surfaces runs with a multi-tab detail view covering overview, coverage, cross-strategy overlap, and seed candidates.

Rule Mining

Automated strategy discovery layered on top of fingerprinting. An optional Phase 0 trains a LightGBM model on historical trade data and extracts decision paths as seed candidates that the beam search then refines. Condition screening covers price-based indicators, temporal flags, and trajectory-aware derivative conditions that capture how indicator values change over time. The engine combines top performers into multi-condition rules, optimizes exit parameters via grid search, and explores multi-phase temporal patterns. Fast-mover replay gating during mining ensures the discovery universe reflects what live detection would surface, and carry-mode simulation evaluates multi-day holding behavior end-to-end. Dual validation modes — time-series split with walk-forward cross-validation and reproducible random split — cover temporal and i.i.d. integrity. Beyond aggregate P&L, an exit-quality scoring mode weighs P&L, drawdown, hold efficiency, exit type quality, and peak-to-exit giveback, with a scoring-mode selector for P&L-centric or exit-quality-centric evaluation. Rules that pass validation are promoted to live trading with per-rule exit configurations.

Multi-Broker Execution

A BaseBroker interface enables seamless switching between Alpaca (paper + live trading, SIP data feed, fractional shares) and Interactive Brokers (trailing stops, global market access including futures, forex, and options). Configured via a single environment variable.

Backtesting Infrastructure

A distributed backtesting layer that sweeps parameter combinations across all strategies and compares results across arbitrary multi-day date ranges in a single operation. Backtest and rule-mining workloads are fully offloaded to dedicated workers via Redis work queues, with per-date data pre-caching and a finalize queue that aggregates results asynchronously so the API process stays lightweight. Tick data replay from QuestDB enables high-fidelity simulation against historical market conditions, cross-day aggregation surfaces strategy consistency and parameter stability, and best-performing configurations can be promoted to live trading in one click. Progress streams to the dashboard over WebSocket throughout.

Unified Exit Engine

Centralized exit logic handling stop loss, trailing stops, scaled profit targets, and time-based exits. Each strategy and promoted rule can define its own exit configuration, enabling fine-grained risk management without global fallback behavior.

Production Correctness

Live-system failure modes that simple code paths and backtests hide. The market-data WebSocket reconnects with exponential backoff on transient failure and preserves subscription state across disconnects. A Redis connection manager with auto-reconnect, keepalive heartbeats, periodic health pings, and payload compression keeps the pub/sub and cache layer available under network wobble. Structural buy-sell linking replaces FIFO trade matching so P&L attribution follows the actual order relationship even when exits fill out of submission order. Historical bars are split-adjusted at the broker so backtests and live price references don't misfire around corporate actions.

Authentication & Security

JWT-based authentication with access and refresh tokens. Protected API routes require bearer tokens. Extended session support for persistent dashboard access.

Observability Stack

Ephemeral workers — shards, backtest workers, rule-mining workers — expose the same metrics surface as long-running services, pushed via Prometheus Pushgateway, and Grafana dashboards are provisioned as code through grafanalib so observability config ships with the repo rather than living in a clicked-together UI. Structured JSON logging via structlog, distributed tracing with OpenTelemetry, and Jaeger for trace visualization round out the stack. Telegram notifications cover trade alerts and system events.

Real-Time Infrastructure

The platform uses a domain event architecture where backend services publish events through Redis pub/sub, which are forwarded to frontend clients via WebSocket connections. This enables sub-second UI updates across all dashboard pages without polling. When the WebSocket connection is unavailable, the system falls back to automatic polling to maintain data freshness.

  • WebSocket-Driven Cache Invalidation — Domain events trigger targeted query invalidation for sub-second dashboard updates
  • Redis Pub/Sub Event Bridge — Cross-process event delivery from background workers to API server to frontend clients
  • Stale Price Guards — Prevents exit decisions and order submissions when market data exceeds freshness thresholds
  • Atomic Order Repricing — Background monitoring adjusts pending orders using broker-native replace operations
  • Live Notification Bell — Severity-tiered event notifications with toast alerts for critical trading events, connection status indicator, and automatic backfill on reconnect

Frontend

Next.js 16 dashboard with React 19, Tailwind CSS v4, and shadcn/ui components, styled with a Bloomberg Terminal-inspired dark theme for a professional trading aesthetic. TanStack Query handles server state. Key pages include real-time portfolio monitoring, order entry, strategy configuration with independent fast/slow detection toggles, backtest analytics with equity curves, campaign backtesting for multi-day comparison, a strategy discovery interface for visualizing and promoting mined rules with detection-type tagging, a fingerprinting dashboard with multi-tab run details, a trade detail view that shows the decision audit trail (signal, filters, arbiter result, risk gate, conditions that fired) alongside entry/exit markers on candlestick charts, a live position monitor with exit state tracking, and a rule lifecycle dashboard with health scoring and degradation detection. The backend also includes a CLI built with Typer and Rich for administrative operations and PDT (Pattern Day Trader) compliance tracking.