Why We Built It
In algorithmic trading, continuity and traceability are mission critical. The Persistence Module was developed to ensure trading systems can securely store, track, and recover all trading operations across sessions, without data loss or inconsistency. Whether managing thousands of trades or orchestrating multiple orders per strategy, our module brings institutional-grade structure and resilience to every trade lifecycle.
System Overview
This module powers long-term storage and real-time synchronization of trade states, orders, and lock mechanisms. Built on SQLAlchemy ORM with PostgreSQL (and TimescaleDB support), it provides thread-safe scoped sessions, automatic database migrations, and flexible querying.
It consists of:
- Trade Management: Real-time tracking of trading positions.
- Order Management: History and lifecycle control of all exchange orders.
- Pair Locks: Risk control mechanisms that restrict trading activity for defined durations.
- Scoped Sessions: Thread-safe ORM sessions for high-concurrency environments.
- Migration System: Schema change detection and auto-migration support.
Core Architecture
ORM Layer: Declarative models (Trade, Order, PairLock) with alignment to LocalTrade class used in backtesting.
Initialization Logic: Central init_db() configures engine, manages scoped sessions, and auto-applies migrations.
Session Safety: Scoped sessions assigned per thread or async context using custom get_request_or_thread_id() method.
Modular Design: Each model is decoupled for single-responsibility, with persistent storage and recovery features.
Key Features
Trade Model
- Tracks open and closed trade positions.
- Records fees, order IDs, leverage, stop-loss, max/min price, strategy, and more.
- Provides helper methods to fetch trade records, close positions, and sync with external exchanges.
Order Model
- Represents orders tied to a trade, mapped to CCXT structure.
- Stores execution details, timestamps, and pricing.
- Includes retrieval methods like order_by_id() and get_open_orders().
Pair Lock Model
- Prevents trading on specific pairs.
- Includes reason, direction (long/short), and lock windows.
- Ensures compliance and risk-adjusted operations.
Migration & Initialization
- Tables auto-created on first run.
- Migration logic adds columns or tables without manual intervention.
- Compatible with SQLite for development and PostgreSQL for production.
Example Use Cases
Trade Continuity: Restore open trade states after unexpected downtime.
Order History Audits: Reconcile executed trades and missed fills.
Dynamic Risk Management: Lock high-volatility pairs temporarily across strategies.
Performance Metrics: Extract trade-level profit/loss, leverage performance, and stop-loss efficiency.
Designed for Reliability and Scale
- Thread-safe Sessions: Supports multi-threaded and async environments.
- Atomic Operations: SQL transactions ensure integrity.
- Extensible Design: Easily integrate custom fields, strategies, or execution modes.
Final Thoughts
Whether powering a backtesting engine or a live trading cluster, the Persistence Module ensures your system never forgets a trade. With its modular design, production-grade reliability, and seamless integration with EdgeCore, it’s built for trading operations that demand accountability, precision, and scale.