Bybit Trading Bot with Real-Time Data Processing
My Role:
Full-stack Developer
Date:
05/2025
Technologies:
Python
FastAPI
PostgreSQL
asyncpg
ccxt
WebSockets
React
Vite
TypeScript
Zustand
TanStack Query
Mantine UI
Pydantic
JWT
Docker
Led the refactoring of a Bybit crypto trading bot to enhance robustness, reliability, accuracy, and performance. The project focused on persistent state management, low-latency trade execution, and accurate SL/TP management by leveraging real-time data from Bybit's V5 API and WebSockets. The backend was rebuilt with FastAPI and PostgreSQL, and the frontend with React/Vite.
Key Contributions & Impact
- Architecture Redesign: Designed a new target architecture focusing on asynchronous operations, persistent state management in PostgreSQL, and real-time data processing via WebSockets to eliminate state loss and improve reliability.
- Backend Development (FastAPI): Developed the core backend using FastAPI, including API services for authentication, settings, webhook signal ingestion, and the main TradingService for managing active positions and interacting with the Bybit exchange.
- Real-Time Data Integration (Bybit WebSockets): Implemented websocket client to manage persistent connections to Bybit's public (tickers) and private (position, execution) WebSocket streams, enabling real-time SL/TP checks and position synchronization.
- Persistent State Management: Shifted from in-memory state to PostgreSQL for storing active trade positions, SL/TP levels, and other critical data, ensuring crash recovery and data integrity.
- Low-Latency Trade Execution: Utilized library with Bybit REST API interactions for order placement and integrated real-time WebSocket data to trigger SL/TP market close orders promptly.
- Data Flow Design: Defined and implemented robust data flows for new trade signals, real-time SL/TP checks, order reversals, and dashboard updates, ensuring clear process sequences and component interactions.
- API & WebSocket Contract Definition: Established clear API endpoint contracts and WebSocket message structures for backend-frontend communication, ensuring efficient and reliable data exchange for dashboard updates and trading actions.
- Frontend Architecture (React/Vite): Proposed and guided the frontend refactor using React with Vite, TypeScript, Zustand for global state, TanStack Query for server state, and Mantine UI for components.
Challenges & Solutions
-
Eliminating State Loss & Ensuring Data Integrity: The previous system suffered from state loss on restarts due to in-memory position management. Solution: Implemented persistent state management using PostgreSQL for all active positions and critical trading data. Utilized database transactions for atomic updates, ensuring data consistency and enabling crash recovery.
-
Achieving Accurate and Low-Latency SL/TP Execution: Previous reliance on REST-only polling led to delays in SL/TP execution. Solution: Integrated Bybit's real-time WebSocket streams to monitor prices and position updates continuously. This allowed the `TradingService` to trigger SL/TP market close orders with significantly lower latency.
-
Managing Real-time WebSocket Connections and Data: Ensuring stable WebSocket connections to Bybit and efficiently processing the incoming stream of data. Solution: Developed a dedicated websocket client with robust auto-reconnection logic and efficient parsing/forwarding of messages to the trading service for immediate action or state updates.
Lessons Learned
- The critical importance of persistent state management over in-memory solutions for financial applications requiring high reliability.
- Leveraging real-time WebSocket data streams is paramount for performance-sensitive trading operations like SL/TP execution.
- Designing clear API and data flow contracts significantly improves system integration and maintainability.
- Asynchronous programming patterns (FastAPI, asyncio, asyncpg) are essential for building scalable and responsive trading bot backends.