Skip to content

huning2009/Hedging_quantification

Repository files navigation

Hedging Quantification - 对冲量化系统

English | 简体中文

A high-performance cryptocurrency exchange monitoring system that supports real-time price tracking across multiple exchanges.

Python Version License: MIT Code style: black Documentation Status

Features

  • Multi-Exchange Support: Simultaneously monitor multiple cryptocurrency exchanges
  • Real-time Price Monitoring: WebSocket-based real-time price updates
  • Market Type Support: Support for spot, futures, and margin markets
  • High Precision: Advanced price precision handling following CCXT standards
  • Market Parameters: Comprehensive market information including:
    • Minimum purchase amount
    • Leverage range
    • Trading fees (maker/taker)
    • Price and amount precision
  • Error Handling: Robust error handling and automatic reconnection
  • Cross-Platform: Optimized for both Windows and Linux systems
  • Performance Optimization: Utilizes uvloop on Linux and optimized event loops on Windows
  • Data Validation: Comprehensive input validation and error checking
  • Extensible Architecture: Easy to add new exchanges and features

Documentation

Full documentation is available at hedging-quantification.readthedocs.io.

Requirements

Installation

  1. Clone the repository:
git clone https://github.com/maodou7/Hedging_quantification.git
cd Hedging_quantification
  1. Create and activate environment:

Option 1 - Using Anaconda (Recommended):

# List all environments
conda env list

# Create a new environment with Python 3.12.8
conda create -n hedging python=3.12.8

# Activate the environment
conda activate hedging

# Deactivate the environment when done
conda deactivate

# Remove environment if needed
conda remove -n hedging --all

Option 2 - Using venv:

For Linux/macOS:

python -m venv venv
source venv/bin/activate

For Windows:

python -m venv venv
.\venv\Scripts\activate
  1. Install dependencies:

For Linux/macOS:

# Install system dependencies
sudo apt-get update
sudo apt-get install python3-dev build-essential

# Install Python dependencies
pip install --upgrade pip
pip install -r requirements.txt

# Install uvloop for better performance (Linux only)
pip install uvloop

For Windows:

pip install --upgrade pip
pip install -r requirements.txt

Configuration

Edit Config/exchange_config.py to configure:

  • Exchanges to monitor
  • Market types (spot/futures/margin)
  • Quote currencies
  • Market structure settings

Example configuration:

EXCHANGES = ['binance', 'bybit', 'okx']
MARKET_TYPES = {
    'spot': True,
    'future': True,
    'margin': False
}
QUOTE_CURRENCIES = ['USDT', 'BTC']

Usage

Run the main program:

python main.py

The program will:

  1. Initialize exchange connections
  2. Find common trading pairs
  3. Start real-time price monitoring
  4. Output formatted JSON price data

Example output:

{
  "exchange": "binance",
  "type": "spot",
  "symbol": "BTC/USDT",
  "quote": "USDT",
  "price": "45123.45",
  "min_cost": "5.0",
  "leverage": "1-100",
  "fees": {
    "taker": "0.001",
    "maker": "0.001"
  },
  "precision": {
    "price": 2,
    "amount": 6
  }
}

Architecture

  • main.py: Main program entry point
  • ExchangeModules/:
    • exchange_instance.py: Exchange connection management
    • monitor_manager.py: Price monitoring system
    • market_processor.py: Market data processing
    • common_symbols_finder.py: Common trading pair detection
    • market_structure_fetcher.py: Market structure handling

Performance Optimization

  • Automatic event loop optimization:
    • Linux: Uses uvloop for maximum performance (up to 2-4x faster than default)
    • Windows: Uses WindowsSelectorEventLoopPolicy for optimal performance
  • Efficient WebSocket connections with automatic reconnection
  • Optimized data structures for quick lookups
  • Memory-efficient data processing
  • Concurrent exchange processing
  • Connection pooling for REST API calls

Monitoring and Debugging

  • Real-time performance metrics
  • Detailed logging with different levels
  • WebSocket connection status monitoring
  • Memory usage tracking
  • Response time measurements
  • Error rate monitoring

Best Practices

  1. Always use the virtual environment
  2. Regularly update dependencies
  3. Monitor system resources
  4. Back up configuration files
  5. Check logs regularly
  6. Keep API keys secure
  7. Use appropriate timeouts for API calls

Testing

Run the test suite:

pytest tests/

For coverage report:

coverage run -m pytest tests/
coverage report

Code Style

This project uses Black for code formatting and follows PEP 8 guidelines. To format your code:

black .

Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Security

For security issues, please see our Security Policy.

Changelog

See CHANGELOG.md for a list of changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

对冲量化

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages