Skip to content

princeniu/Multi-Exchange-Account-Monitoring-System

Repository files navigation

Multi-Exchange Account Monitoring System

English | 简体中文

This project is used to monitor position changes and asset status of multiple cryptocurrency exchanges including Binance, Bybit, and Gate.io. The system supports both main accounts and sub-accounts, with notifications through Feishu or Telegram. The system monitors account position changes in real-time, sends immediate notifications for opening and closing positions, and sends daily account total asset reports at fixed times.

Features

  • Multi-Exchange Support: Supports Binance, Bybit, and Gate.io exchanges
  • Multi-Account Monitoring: Supports simultaneous monitoring of main accounts and sub-accounts
  • Real-time Position Monitoring: Monitors futures account position changes in real-time
  • Automatic Position Detection: Automatically identifies opening and closing positions
  • Daily Scheduled Reports: Sends daily account total asset reports at fixed times
  • Report Storage: Automatically stores daily reports locally
  • Multiple Notification Channels: Supports Feishu and Telegram notifications
  • Telegram Interactive Features: Supports Telegram query functionality and command interactions
  • Customizable Monitoring: Configurable monitoring intervals and report times
  • Error Handling: Automatic retry and error notifications
  • Position Duration Statistics: Tracks and displays position holding duration
  • Profit/Loss Calculation: Calculates and displays profit/loss and return rates
  • Sorted Position Display: Displays positions sorted by profit/loss
  • Multi-Account Asset Summary: Provides comprehensive asset statistics across all accounts
  • Timezone Support: All times displayed in Eastern Time (ET) with automatic DST conversion

Supported Exchanges

Binance

  • Main account and sub-account support
  • Real-time position monitoring
  • Comprehensive balance and PnL tracking

Bybit

  • Main account and sub-account support
  • Position change detection
  • Asset balance monitoring

Gate.io

  • Main account and sub-account support
  • Swap contract monitoring
  • Real-time position tracking

Notification Features Explained

Position Change Notifications

  • New Position Notifications:

    • Account type (Main Account/Sub Account)
    • Currency and trading pair
    • Position direction (Long/Short)
    • Opening price
    • Position size
    • Margin used
    • Opening time (Eastern Time)
  • Closing Position Notifications:

    • Account type (Main Account/Sub Account)
    • Currency and trading pair
    • Position direction
    • Opening and closing prices
    • Position size
    • Profit/loss amount and return rate
    • Position duration (days/hours/minutes)
    • Closing time (Eastern Time)

Daily Account Report

  • Account Overview:
    • Total assets for each account (USDT/USDC)
    • Portfolio total assets
    • Available balance for each account
    • Margin occupied for each account
    • Unrealized PnL for each account
  • Position Details (sorted by account and PnL):
    • Account type
    • Trading pair
    • Position direction
    • Unrealized PnL
    • Return rate percentage

Installation Requirements

  • Python 3.8+
  • ccxt >= 2.0.0
  • requests >= 2.26.0
  • schedule >= 1.1.0
  • python-dotenv >= 0.19.0
  • pytz >= 2021.1
  • python-telegram-bot==20.8

Installation Steps

Method 1: Direct Deployment

If deploying on a server:

# Download miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

# Add execution permissions
chmod +x Miniconda3-latest-Linux-x86_64.sh

# Install miniconda
./Miniconda3-latest-Linux-x86_64.sh

# Update bashrc
source ~/.bashrc

# Install screen (if not installed)
yum install screen -y

# Create screen session
screen -S exchange_monitor

# Exit screen session
# ctrl + a + d

# Reconnect session
screen -r exchange_monitor
  1. Clone project locally
  2. Create virtual environment
conda create -n exchange_monitor
conda activate exchange_monitor
conda install pip

# View environments
# conda env list

# Exit environment
# conda deactivate

# Remove environment
# conda env remove -n exchange_monitor
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure environment variables:
    • Copy env.example to .env
    • Fill in the following configurations:

Exchange API Keys

# Binance API Configuration
BINANCE_API_KEY=your_binance_main_api_key
BINANCE_API_SECRET=your_binance_main_api_secret
SUB_ACCOUNT_API_KEY=your_binance_sub_api_key
SUB_ACCOUNT_API_SECRET=your_binance_sub_api_secret

# Bybit API Configuration
BYBIT_API_KEY=your_bybit_main_api_key
BYBIT_API_SECRET=your_bybit_main_api_secret
BYBIT_SUB_API_KEY=your_bybit_sub_api_key
BYBIT_SUB_API_SECRET=your_bybit_sub_api_secret

# Gate.io API Configuration
GATEIO_API_KEY=your_gateio_main_api_key
GATEIO_API_SECRET=your_gateio_main_api_secret
GATEIO_SUB_API_KEY=your_gateio_sub_api_key
GATEIO_SUB_API_SECRET=your_gateio_sub_api_secret

Account Enablement

# Enable/disable accounts (true/false)
ENABLE_BINANCE_MAIN=true
ENABLE_BINANCE_SUB=true
ENABLE_BYBIT_MAIN=true
ENABLE_BYBIT_SUB=false
ENABLE_GATEIO_MAIN=true
ENABLE_GATEIO_SUB=false

Notification Configuration

# Notification settings
NOTIFICATION_TYPE=TELEGRAM  # or FEISHU
NOTIFY_INTERVAL=30  # monitoring interval in seconds
DAILY_REPORT_TIME=09:00  # daily report time (HH:MM)

# Feishu configuration
FEISHU_WEBHOOK_URL=your_feishu_webhook_url

# Telegram configuration
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
TELEGRAM_CHAT_ID=your_telegram_chat_id

Method 2: Docker Deployment

  1. Clone project locally
  2. Configure environment variables:
    • Copy env.example to .env and fill in the configurations (same as direct deployment above)
  3. Build and start containers using docker-compose:
# Build and start in background
docker-compose up -d

# View logs
docker-compose logs -f

# Stop service
docker-compose down

# Restart service
docker-compose restart

Usage

  1. After ensuring correct configuration, run the main program:
python main.py
  1. After program startup:
    • Immediately sends an account report
    • Monitors position changes according to set interval
    • Sends daily report at specified time

Notification Services

The system supports multiple notification channels:

Feishu Notifications

  • Send notifications through Feishu bot Webhook
  • Support rich text format
  • Real-time position change notifications
  • Daily account reports

Telegram Notifications

  • Send notifications through Telegram Bot
  • Support real-time query functionality
  • Support command interactions
  • Support message formatting with buttons
  • Interactive account overview and position queries

Configuration method:

  1. Users need to configure in .env file:

    • TELEGRAM_BOT_TOKEN: Bot token obtained from @BotFather
    • TELEGRAM_CHAT_ID: Target chat ID
    • NOTIFICATION_TYPE: Choose 'FEISHU' or 'TELEGRAM'
  2. When configured as TELEGRAM:

    • All notifications will be sent through Telegram bot
    • Interactive buttons for account queries
    • Command-based interactions for real-time data

System Features

Multi-Exchange Support

  • Binance: Full futures trading support with comprehensive position tracking
  • Bybit: Complete position monitoring with balance and PnL tracking
  • Gate.io: Swap contract monitoring with real-time position updates

Timezone Handling

  • All time displays use Eastern Time (ET)
  • Automatic daylight saving time conversion

Error Handling Mechanism

  • Automatic retry for API call failures
  • Automatic reconnection for network exceptions
  • Critical error logging
  • Real-time notification for exceptions
  • Scheduler exception auto-recovery

Performance Optimization

  • Use ccxt library to ensure API call rate limits
  • Reasonable monitoring interval settings
  • Efficient data structures for storing historical positions
  • Multi-account concurrent monitoring optimization

Connection Monitoring Features

  • Automatic system connection status detection
  • Immediate notification on disconnection
  • Automatic reconnection mechanism
  • Recovery notification after successful reconnection
  • Warning for multiple reconnection failures
  • Configurable reconnection interval and maximum retry attempts

Report Storage

  • Daily reports automatically saved to local directory
  • Historical report access
  • Structured report format for easy analysis

File Structure

├── main.py                 # Main application entry point
├── config.py              # Configuration management
├── scheduler.py           # Task scheduling system
├── binance_client.py     # Binance exchange client
├── bybit_client.py       # Bybit exchange client
├── gateio_client.py      # Gate.io exchange client
├── services/
│   ├── feishu_service.py    # Feishu notification service
│   └── telegram_service.py  # Telegram notification service
├── 每日报告/              # Daily reports storage
├── requirements.txt       # Python dependencies
├── docker-compose.yml    # Docker deployment configuration
├── Dockerfile           # Docker image definition
└── README files         # Documentation

Development Plan

Near-term Plans

  • Support more sub-account monitoring
  • Add chart display functionality
  • Support custom notification templates
  • Add account portfolio analysis functionality
  • Add more notification channels (like WeCom, Discord, etc.)
  • Web-based management interface
  • Enhanced error reporting and monitoring

Items to Optimize

  • Add unit tests
  • Optimize error retry mechanism
  • Add data persistence storage
  • Optimize multi-account concurrent monitoring
  • Add performance metrics and monitoring
  • Implement rate limiting and API quota management

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published