Overview
TheExchangeSimulator is a stateful, event-driven simulator that mirrors live trading behavior without risking real capital. It maintains realistic orderbook state, applies slippage and fees, and automatically triggers stop-loss and take-profit orders.
The simulator is the default execution backend in development. Production uses the Lighter API for real trading.
Architecture
- AccountState: Tracks cash, positions, P&L, and exit plans per trading account
- MarketState: Fetches and caches live orderbook data from exchanges
- OrderMatching: Simulates realistic fills with slippage and partial fills
- Auto-Close Engine: Monitors positions and triggers exits when stop/target hit
Initialization & Bootstrap
The simulator is initialized once per server lifecycle:Orders table:
Account State Management
Each trading account has its ownAccountState instance:
Order Matching Engine
The simulator applies realistic orderbook matching:Default slippage is 10 bps (0.1%). This models liquidity taking cost and price impact.
Auto-Close Engine
EveryrefreshIntervalMs (default: 5s), the simulator:
- Fetches latest market prices
- Checks all open positions for stop/target triggers
- Queues auto-close orders
- Executes closes and updates database
Event Bus
The simulator emits events for real-time UI updates:Configuration Options
Switching to Live Trading
To use the Lighter API instead of the simulator:- Set
USE_LIVE_TRADING=truein.env.local - Configure Lighter API key:
LIGHTER_API_KEY=your_key - The trading logic automatically uses
lighterApi.placeOrder()instead ofsimulator.placeOrder()
Both backends use the same
Orders table schema, so switching between simulator and live trading is seamless.Testing & Debugging
Reset a specific account’s simulator state:Related Resources
Autonomous Trading Loop
How agents interact with the simulator
Database Schema
Orders table structure and exit plans
Order Execution
Order placement and fill tracking
Deployment Guide
Deploy the backend with trading configuration

