A real-time trading platform that connects to Binance for market data and allows users to trade cryptocurrencies with features like stop-loss and take-profit orders.
This platform provides:
- Real-time market data from Binance
- Live price charts for multiple cryptocurrencies
- Trading capabilities with stop-loss and take-profit
- In-memory order management
- User balance tracking
- Historical data storage with TimescaleDB
- Efficient time-series data aggregation
We've made several key improvements to the codebase to make it production-ready:
-
Standardized Database Connections: Implemented a singleton pattern for Prisma client to prevent connection pool exhaustion and improve performance.
-
Enhanced Authentication: Added JWT-based authentication with proper middleware for securing routes.
-
Optimized Trading Engine: Streamlined the order management system with efficient in-memory data structures.
-
TimescaleDB Integration: Implemented a robust historical candle data system with TimescaleDB for efficient time-series storage and aggregation.
-
Retention Policies: Added both application-level (last 100 candles) and database-level (7-day) retention policies for optimal data management.
-
Continuous Aggregates: Created pre-computed views for different timeframes (5m, 15m, 1h, 1d) to optimize query performance.
-
Code Cleanup: Removed excessive comments and improved documentation for better maintainability.
-
API Documentation: Added comprehensive Swagger documentation for all endpoints.
-
Comprehensive Testing: Added extensive test suite covering all components and workflows.
The platform includes a comprehensive test suite that covers all major components and workflows:
- Unit Tests: Test individual components in isolation (auth, orders, symbols)
- Service Tests: Test service layer functionality (OrderManager, BalanceManager)
- Integration Tests: Test complete workflows across multiple components
- Candle Data Tests: Verify TimescaleDB integration and data aggregation
# Run all tests
bun test
# Run tests in watch mode
bun test --watch
# Run a specific test file
bun test src/tests/auth.test.ts
# Test candle data system
bun run test:candlesThe tests cover:
- Authentication flows
- Order management
- Symbol information
- Trading logic (stop-loss/take-profit)
- Balance management
- Complete trading workflows
- Historical candle data storage and retrieval
- Time-series data aggregation
The following areas require additional attention:
-
Concurrency Management: The trading engine needs robust concurrency handling to prevent race conditions during high-volume trading.
-
Error Handling: Implement more comprehensive error handling and recovery mechanisms throughout the application.
-
Performance Testing: Conduct load testing to ensure the system can handle production traffic volumes.
-
Security Auditing: Perform a thorough security audit, especially for authentication and order execution flows.
-
Monitoring and Logging: Enhance logging and add monitoring for critical system components.
-
Data Compression: Configure TimescaleDB compression for older candle data to optimize storage.
During development, these components presented the greatest challenges:
-
Price Trigger System: Implementing an efficient system to monitor price changes and trigger stop-loss and take-profit orders required careful design to balance performance with accuracy.
-
Balance Management: Ensuring accurate tracking of user balances, especially during concurrent order executions, was complex and required a robust locking mechanism.
-
WebSocket Connection Management: Handling large numbers of concurrent WebSocket connections while maintaining low latency for price updates required optimization.
-
Order Execution Logic: Implementing the business logic for order execution, especially for short positions and calculating profit/loss correctly, required careful testing.
-
Database Connection Pooling: Standardizing the Prisma client usage across the application to prevent connection pool exhaustion was critical for stability.
-
TimescaleDB Integration: Setting up and optimizing TimescaleDB for efficient time-series data storage and aggregation required careful configuration.
The backend is built with:
- Node.js & TypeScript: For type-safe server-side code
- Express: Web server framework
- WebSockets: For real-time data streaming
- Prisma: Database ORM for PostgreSQL
- TimescaleDB: For efficient time-series data storage
- Binance API: For market data
- Bun: Fast JavaScript runtime and package manager
- Connects to Binance WebSocket API
- Forwards real-time market data to clients
- Handles client subscriptions to specific symbols
- Manages client connections and message broadcasting
- Supports user authentication for private channels
- Broadcasts order updates and balance changes
- Provides real-time candle data updates
- In-memory order management
- Price trigger monitoring for stop-loss and take-profit
- Order matching and execution
- Balance management
- Processes raw Binance data into usable formats
- Creates OHLCV candles from trade data
- Stores historical data in TimescaleDB
- Aggregates candles to different timeframes
- RESTful API for historical data
- Symbol information endpoints
- User management endpoints
- Order placement and cancellation
- Candle data retrieval and aggregation
- Swagger documentation
- Efficient storage of 1-minute candles as base data
- Automatic aggregation to larger timeframes (5m, 15m, 1h, 1d)
- Retention policy to keep only the last 100 candles per symbol/timeframe
- TimescaleDB continuous aggregates for optimized queries
- Fallback to manual aggregation when needed
✅ Binance WebSocket Integration
- Real-time connection to Binance
- Multi-symbol support
- Data processing pipeline
✅ WebSocket Server
- Client connection management
- Symbol subscription system
- Real-time data broadcasting
- User authentication
- Order updates broadcasting
- Candle data subscription system
✅ Trading Engine
- In-memory order management
- Order matching logic
- Stop-loss and take-profit monitoring
- Balance management
✅ User Management
- Authentication system
- User portfolio tracking
- Balance management
- Trade history
✅ Historical Data Management
- TimescaleDB integration for efficient time-series storage
- Candle data aggregation to different timeframes
- Retention policies for optimal data management
- Continuous aggregates for optimized queries
✅ API Documentation
- Swagger UI for API endpoints
- WebSocket client example
- Comprehensive README documentation
The database uses PostgreSQL with TimescaleDB extension and Prisma ORM, including the following models:
- User: Stores user information and balances (USDC, BTC)
- Symbol: Trading pairs available on the platform
- OHLCV: Historical candlestick data for each symbol, optimized as a TimescaleDB hypertable
- Order: Historical record of executed orders
The platform uses a custom WebSocket protocol for real-time updates:
// Client subscription message
{
"type": "subscribe",
"symbols": ["BTCUSDT", "ETHUSDT"]
}
// Server price update message
{
"type": "price_update",
"symbol": "BTCUSDT",
"price": 45678.90,
"timestamp": 1629483627000
}
// Candle subscription message
{
"type": "SUBSCRIBE_CANDLES",
"symbol": "btcusdt",
"timeframe": "1m"
}
// Candle update message
{
"type": "CANDLE_UPDATE",
"data": {
"symbol": "btcusdt",
"timeframe": "1m",
"candle": {
"time": 1647352800,
"open": 45000,
"high": 45500,
"low": 44800,
"close": 45200,
"volume": 1000
}
}
}
// Order placement message
{
"type": "place_order",
"symbol": "BTCUSDT",
"side": "BUY",
"quantity": 0.01,
"price": 45000,
"stopLoss": 44000,
"takeProfit": 48000
}- Bun (v1.0+)
- PostgreSQL 14+ with TimescaleDB extension
- Binance API key (for production)
-
Clone the repository
-
Install dependencies:
cd backend bun install -
Set up environment variables:
- Copy
.env.exampleto.env - Update database connection string
- Copy
-
Initialize the database:
bun run setupThis will:
- Generate Prisma client
- Run database migrations
- Initialize the database with seed data
- Set up TimescaleDB for candle data
-
Start the development server:
bun devOr use the provided script:
./start.sh
Once the server is running, access the Swagger documentation at:
http://localhost:3001/api-docs
A comprehensive WebSocket client example is available at:
http://localhost:3001/websocket-client-example.html
This example demonstrates:
- Real-time price updates
- User authentication
- Order placement and cancellation
- Portfolio tracking
- Stop-loss and take-profit functionality
- Candle data visualization
- ✅ WebSocket connections
- ✅ Data processing
- ✅ Basic UI
- ✅ Order management system
- ✅ Price trigger monitoring
- ✅ Balance management
- ✅ Authentication and user profiles
- ✅ Advanced charting
- ✅ Mobile responsiveness
- ✅ TimescaleDB integration
- ✅ Efficient candle data storage
- ✅ Time-series data aggregation
- ✅ Retention policies
- ✅ Continuous aggregates
- ⏳ Backtesting capabilities
- ⏳ Trading bots integration
- ⏳ Social trading features
- ⏳ Data compression for older candles
- ⏳ Redis caching for frequently accessed data