Automated grid trading system with gap compensation and crash-resilient order management for US equities
Case Study2026

Automated grid trading system with gap compensation and crash-resilient order management for US equities

PythonAlpaca APIWebSocketREST APIPostgreSQLAON limit ordersDAY ordersPaper trading

About The Project

A fully automated grid trading bot for US equities. A market-maker-like system that places a ladder of limit bids below a configurable price ceiling, and for every filled bid, immediately posts a corresponding ask one interval higher. The key complexity was handling gap-up and gap-down market openings, where the price opens outside the active grid and requires intelligent compensation logic to resynchronise bids, asks, and position state. The system needed to be configurable, paper-trade first, survive crashes and reconnects via a persistent order database, and include kill-switch controls for live deployment safety.

Automated grid trading system with gap compensation and crash-resilient order management for US equities supporting graphic

System Architecture

System Architecture diagram

Key Challenges

Gap-up and gap-down compensation

When the market opens outside the active grid range, bids that should have filled did not, and asks that should have been placed are missing. The system needed logic to detect gap events, reconcile missed fills, and reconstruct a valid grid state without manual intervention.

Crash recovery and state continuity

Outages- whether from bugs, exchange downtime, or network drops; leave the bot with an unknown number of open orders and an inconsistent position state. Resuming without a persistent order record risks placing duplicate orders or leaving orphaned positions unmanaged.

Partial fill risk on a liquidity-providing strategy

The grid strategy relies on symmetric fill pairs; a bid fill triggers exactly one ask. Partial fills break this symmetry, creating uncovered inventory at incorrect price levels. All orders needed to be All-Or-None to prevent this without any custom fill-merging logic.

After-hours execution risk

Limit orders left open overnight could execute at prices valid during the previous session but misaligned with the next day's open, exposing the strategy to unintended positions. Orders needed automatic expiry at market close.

Broker selection trade-offs

Choosing between Alpaca, Interactive Brokers, and TD Ameritrade required weighing API friendliness, order matching quality, instrument coverage, and fee structure; specifically given the no-leverage, tight-spread focus of the strategy.

Our Solution

Gap compensation logic

Implemented open-of-day gap detection by comparing the prior grid ceiling to the actual opening price. Gap-down events trigger market orders to manage unhedged inventory; gap-up events cancel stale bids and rebuild the ask ladder from the new price level, restoring grid symmetry before regular trading begins.

Persistent order database

Designed a database schema to record every placed, filled, and cancelled order with its grid-level metadata. On restart, the bot queries active broker orders, reconciles them against the local record, and resumes from a known state; preventing duplicates and correctly identifying any orders that filled during the downtime.

AON + DAY order enforcement

Configured all grid orders as All-Or-None to guarantee complete fills only, and as DAY orders to ensure automatic cancellation at market close. This removed partial-fill handling complexity and eliminated after-hours exposure without requiring a separate end-of-day cancel sweep.

Kill switch controls

Provisioned tiered kill switch commands, cancelling all open orders, flattening positions, or pausing new order placement- accessible without stopping the process. Designed to allow rapid intervention if execution behaviour deviates from expectation during live operation.

Alpaca integration with configurable parameters

Selected Alpaca as the broker for its commission-free structure suited to a high-frequency liquidity-providing strategy, and integrated its REST and WebSocket APIs for order placement, real-time fill events, order status watching, and cancellation, wrapped in a configuration layer for interval size, symbols, and order size.

The Result

Fully automated grid engine

Delivered a configurable market-making bot that manages a full bid-ask grid, responds to fills in real time, and handles gap openings without manual input; ready for paper trading and live deployment.

Crash-safe order recovery

Persistent order database enables the bot to resume after any crash or outage by reconciling live broker state against local records, eliminating duplicate orders and orphaned positions.

Zero partial fills, zero after-hours exposure

AON and DAY order enforcement removed the two most operationally risky edge cases, partial fill asymmetry and overnight price drift without additional monitoring overhead.

Tiered kill switch for live safety

Operational controls allow the trader to halt new orders, cancel the full grid, or flatten positions independently; giving real-time intervention capability before committing to fully live operation.

Need a similar solution?

Talk with our team about your project goals and timeline.

Contact Us