How shreesha345/binance-trading-bot works
Project type and purpose
• This is an automated trading system for Binance Futures, written in Python.
• It consists of two main runtime services:
a) The Trading Engine (main.py + utils + api/) that connects to Binance REST & WebSocket, implements the Heikin Ashi strategy, manages orders, state, persistence, and exposes a small HTTP API.
b) The Telegram Bot Service (telegram_bot/) that handles user commands, subscription payments via Razorpay, and proxies requests to the trading engine.
• Supporting artifacts include configuration, data storage, logging, and Docker deployment.
File structure
Top-level:
• .env, pyproject.toml, Dockerfile.backend, docker-compose.yml: Deployment and environment.
• main.py: Entry point for the trading engine service.
• api/ (routes.py, trading_config.json): HTTP routes for status, manual controls, and configuration.
• utils/: Core trading logic (HA calculation, order management, WebSocket client, quantity calculation, logging, etc.).
• data/: Persistent JSON stores (order_book.json, init scripts).
• telegram_bot/: Standalone service with its own Dockerfile. Contains bot logic, Razorpay integration, and server calls.
• logs/: Centralized logs for API, WebSocket, and errors.
Extract architectural insights from the README
• The bot opens a WebSocket connection to Binance for live kline data.
• Each new candle triggers strategy logic: place/cancel/update orders via Binance REST API.
• Order and state tracking in memory + JSON persist.
• Environment variables drive mode (test vs live), API keys, offsets, quantity settings.
• docker-compose can orchestrate both services and shared volumes (for data, logs).
Main components & interactions
a. External Systems
• Binance
• WebSocket Endpoint (stream OHLC data)
• REST API (order placement, cancelation, account/balance queries)
• Telegram Users via Telegram servers
• Razorpay (external payment gateway)
b. Internal Services
1. Trading Engine Service (Dockerfile.backend)
• WebSocket Client (utils/websocket_client) listens to Binance kline data
• Strategy & Order Manager (utils/strategy, buy_sell_handler, pnl_analyzer)
• HTTP API (api/routes.py) for status, manual overrides, config exposure
• Persistence Layer (data/order_book.json, utils/order_storage)
• Logging (utils/logger → logs/)
2. Telegram Bot Service (Dockerfile.telegram)
• Bot Interface (telegram_bot/bot.py) registers commands
• Server Call Module (telegram_bot/server_call.py) talks to Trading Engine API
• Payment Module (telegram_bot/razerpay.py + payments.json, payment_cycle.json, etc.)
• Customer Data Store (telegram_bot/customer_details.json)
c. Shared Infrastructure
• Environment Configuration (.env) mounted into both containers
• Docker Compose (docker-compose.yml) for multi-container orchestration
• Volume Mounts for logs/ and data/
Architectural patterns & principles
• Separation of Concerns: Trading logic vs user interface vs payment logic.
• Event-Driven: WebSocket events trigger strategy execution.
• Stateful Order Management: Finite state machine tracks position states in memory + persisted JSON.
• Configuration-Driven: trading behavior from JSON (api/trading_config.json) + ENV vars.
• Resilience: Auto-reconnect logic for WebSocket; error logging; tick-size validation.
Final checklist
• Internal services (containers)
• Data stores (JSON volumes)
• External APIs (Binance, Razorpay, Telegram)
• Call out architectural patterns: event-driven WebSocket, stateful FSM, config-driven behavior