gogo2/NN/exchanges
Dobromir Popov 73c5ecb0d2 enhancements
2025-04-01 13:46:53 +03:00
..
2025-04-01 13:46:53 +03:00
2025-04-01 13:46:53 +03:00
2025-04-01 13:46:53 +03:00

Trading Agent System

This directory contains the implementation of a modular trading agent system that integrates with the neural network models and can execute trades on various cryptocurrency exchanges.

Overview

The trading agent system is designed to:

  1. Connect to different cryptocurrency exchanges using a common interface
  2. Execute trades based on signals from neural network models
  3. Manage risk through position sizing, trade limits, and cooldown periods
  4. Monitor and report on trading activity

Components

Exchange Interfaces

  • ExchangeInterface: Abstract base class defining the common interface for all exchange implementations
  • BinanceInterface: Implementation for the Binance exchange, with support for both mainnet and testnet
  • MEXCInterface: Implementation for the MEXC exchange

Trading Agent

The TradingAgent class (trading_agent.py) manages trading activities:

  • Connects to the configured exchange
  • Processes trading signals from neural network models
  • Applies trading rules and risk management
  • Tracks and reports trading performance

Neural Network Orchestrator

The NeuralNetworkOrchestrator class (neural_network_orchestrator.py) coordinates between models and trading:

  • Manages the neural network inference process
  • Routes model signals to the trading agent
  • Provides integration with the RealTimeChart for visualization

Usage

Basic Usage

from NN.exchanges import BinanceInterface, MEXCInterface
from NN.trading_agent import TradingAgent

# Initialize an exchange interface
exchange = BinanceInterface(
    api_key="your_api_key",
    api_secret="your_api_secret",
    test_mode=True  # Use testnet
)

# Connect to the exchange
exchange.connect()

# Create a trading agent
agent = TradingAgent(
    exchange_name="binance",
    api_key="your_api_key",
    api_secret="your_api_secret",
    test_mode=True,
    trade_symbols=["BTC/USDT", "ETH/USDT"],
    position_size=0.1,
    max_trades_per_day=5,
    trade_cooldown_minutes=60
)

# Start the trading agent
agent.start()

# Process a trading signal
agent.process_signal(
    symbol="BTC/USDT",
    action="BUY",
    confidence=0.85,
    timestamp=int(time.time())
)

# Stop the trading agent when done
agent.stop()

Integration with Neural Network Models

The system is designed to be integrated with neural network models through the NeuralNetworkOrchestrator:

from NN.neural_network_orchestrator import NeuralNetworkOrchestrator

# Configure exchange
exchange_config = {
    "exchange": "binance",
    "api_key": "your_api_key",
    "api_secret": "your_api_secret",
    "test_mode": True,
    "trade_symbols": ["BTC/USDT", "ETH/USDT"],
    "position_size": 0.1,
    "max_trades_per_day": 5,
    "trade_cooldown_minutes": 60
}

# Initialize orchestrator
orchestrator = NeuralNetworkOrchestrator(
    model=model,
    data_interface=data_interface,
    chart=chart,
    symbols=["BTC/USDT", "ETH/USDT"],
    timeframes=["1m", "5m", "1h", "4h", "1d"],
    exchange_config=exchange_config
)

# Start inference and trading
orchestrator.start_inference()

Configuration

Exchange-Specific Configuration

  • Binance: Supports both mainnet and testnet environments
  • MEXC: Supports mainnet only (no test environment available)

Trading Agent Configuration

  • exchange_name: Name of exchange ('binance', 'mexc')
  • api_key: API key for the exchange
  • api_secret: API secret for the exchange
  • test_mode: Whether to use test/sandbox environment
  • trade_symbols: List of trading symbols to monitor
  • position_size: Size of each position as a fraction of balance (0.0-1.0)
  • max_trades_per_day: Maximum number of trades to execute per day
  • trade_cooldown_minutes: Minimum time between trades in minutes

Adding New Exchanges

To add support for a new exchange:

  1. Create a new class that inherits from ExchangeInterface
  2. Implement all required methods (see exchange_interface.py)
  3. Add the new exchange to the imports in __init__.py
  4. Update the _create_exchange method in TradingAgent to support the new exchange

Example:

class KrakenInterface(ExchangeInterface):
    """Kraken Exchange API Interface"""
    
    def __init__(self, api_key=None, api_secret=None, test_mode=True):
        super().__init__(api_key, api_secret, test_mode)
        # Initialize Kraken-specific attributes
        
    # Implement all required methods...

Security Considerations

  • API keys should have trade permissions but not withdrawal permissions
  • Use environment variables or secure storage for API credentials
  • Always test with small position sizes before deploying with larger amounts
  • Consider using test mode/testnet for initial testing