Skip to content

A high-performance, modular cryptocurrency trading system that automatically executes trades based on market news events. Built with a pluggable architecture for ultra-low latency execution, allowing traders to react to market-moving news before the broader market.

License

Notifications You must be signed in to change notification settings

mumtazkahn/crypto-news-trader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto News Trader

A high-performance, modular cryptocurrency trading system that automatically responds to market news events and executes trades on cryptocurrency exchanges like Binance. This system is designed for ultra-low latency execution, enabling traders to capitalize on market-moving news before the broader market can react.

Key Features

  • Pluggable Architecture: Every component is interchangeable and extensible via a well-defined interface system
  • Ultra-Fast Execution: WebSocket connections throughout the pipeline for sub-10ms order execution latency
  • Asynchronous Processing: Fully asynchronous design to maximize throughput and eliminate bottlenecks
  • Separation of Concerns: Clear boundaries between news ingestion, data analysis, signal generation, and execution
  • Multiple Signal Support: Process and execute multiple trading signals simultaneously
  • Configurable Components: Simple YAML-based configuration for component selection

System Architecture

The system implements a modular architecture with four primary interchangeable components:

flowchart LR
    News[News Sources] -->|News Events| Classifier
    Data[Data Sources] -.->|Optional Market Data| Classifier
    Classifier[Classifiers] -->|Trading Signals| Trader
    Trader[Traders] -->|Order Execution| Exchange[Exchange APIs]
    
    Config[Configuration] --> NewsTrading[News Trading System]
    NewsTrading --> News
    NewsTrading --> Data
    NewsTrading --> Classifier
    NewsTrading --> Trader
    
    classDef core fill:#f8f8f8,stroke:#333,stroke-width:2px;
    classDef component fill:#e1f5fe,stroke:#01579b,stroke-width:1px;
    classDef external fill:#f1f8e9,stroke:#33691e,stroke-width:1px;
    
    class NewsTrading,Config core;
    class News,Data,Classifier,Trader component;
    class Exchange external;
Loading
  1. News Sources: Monitor external news feeds and deliver market-moving events
  2. Data Sources (Optional): Provide supplementary market data for enhanced decision making
  3. Classifiers: Analyze news and market data to generate actionable trading signals
  4. Traders: Execute generated signals on cryptocurrency exchanges

Each component adheres to a base interface, allowing developers to create custom implementations while maintaining compatibility with the overall system.

System Flow Chart

The following diagram illustrates the detailed processing flow of the system:

flowchart TD
    Start[Start Application] --> Logger[Initialize Logger]
    Logger --> NewsTrading[Initialize NewsTrading Class]
    NewsTrading --> LoadConfig[Load Configuration]
    LoadConfig --> LoadComponents[Load Components]
    
    subgraph ComponentLoading["Component Loading"]
        LoadComponents --> NewsSource[Load News Source]
        NewsSource --> DataSource[Load Data Source]
        DataSource --> Classifier[Load Classifier]
        Classifier --> Trader[Initialize Trader]
    end
    
    Trader --> Connect[Connect Trader to Binance]
    
    Connect --> StartStreams[Start All Data Streams]
    
    subgraph Streams["Data Streams"]
        StartStreams --> |Parallel| NewsStream[Start News Stream]
        StartStreams --> |Parallel| DataStream[Start Data Stream]
        StartStreams --> |Parallel| ProcessSignals[Start Signal Processing]
    end
    
    subgraph ProcessLoop["Signal Processing Loop"]
        ProcessSignals --> GetNewsBuffer[Get News Buffer]
        GetNewsBuffer --> LoopSymbols[Loop Through Symbols]
        LoopSymbols --> CheckActiveTrades{Active Trades for Symbol?}
        CheckActiveTrades --> |Yes| NextSymbol[Skip to Next Symbol]
        CheckActiveTrades --> |No| GetData[Get Supplementary Data]
        GetData --> RunClassifier[Run Classifier]
        RunClassifier --> SignalGenerated{Signal Generated?}
        SignalGenerated --> |Yes| ExecuteTrade[Execute Trade via Trader]
        SignalGenerated --> |No| NextSymbol
        ExecuteTrade --> NextSymbol
        NextSymbol --> |More Symbols| LoopSymbols
        NextSymbol --> |No More Symbols| Sleep[Sleep 1 Second]
        Sleep --> GetNewsBuffer
    end
    
    subgraph NewsSourceFlow["News Source Flow"]
        NewsStream --> ConnectWebSocket[Connect to WebSockets]
        ConnectWebSocket --> MessageLoop[Message Handling Loop]
        MessageLoop --> ParseMessage[Parse News Message]
        ParseMessage --> ExtractSymbols[Extract Symbols]
        ExtractSymbols --> UpdateBuffer[Update News Buffer]
        MessageLoop --> PeriodicCleanup[Periodic News Buffer Cleanup]
    end
    
    subgraph DataSourceFlow["Data Source Flow"]
        DataStream --> DataSubscribe[Subscribe to Data Feeds]
        DataSubscribe --> TradeMessageLoop[Trade Message Loop]
        TradeMessageLoop --> ProcessTrade[Process Trade Data]
        ProcessTrade --> UpdateDataStore[Update Data Store]
        TradeMessageLoop --> DataCleanup[Clean Old Trade Data]
    end
    
    subgraph ClassifierFlow["Classifier Flow"]
        RunClassifier --> FindNewsIndex[Find News Index in Trades]
        FindNewsIndex --> ProcessTradeData[Process Trade Data]
        ProcessTradeData --> ClassificationLogic{Classification Logic}
        ClassificationLogic --> |Positive Signal| FormatBuyOrder[Format Buy Orders]
        ClassificationLogic --> |Negative Signal| FormatSellOrder[Format Sell Orders]
        ClassificationLogic --> |No Signal| ReturnNone[Return No Signal]
        FormatBuyOrder --> GenerateSignalBatch[Generate Signal Batch]
        FormatSellOrder --> GenerateSignalBatch
    end
    
    subgraph TraderFlow["Trader Flow"]
        ExecuteTrade --> ProcessBatch[Process Signal Batch]
        ProcessBatch --> ProcessSingleSignal[Process Single Signal]
        ProcessSingleSignal --> SeparateOrders[Separate Immediate/Dependent Orders]
        SeparateOrders --> PlaceImmediateOrders[Place Immediate Orders]
        PlaceImmediateOrders --> OrderSuccess{Order Successful?}
        OrderSuccess --> |Yes| PlaceDependentOrders[Place Dependent Orders]
        OrderSuccess --> |No| LogError[Log Error]
    end
    
    classDef component fill:#f9f,stroke:#333,stroke-width:1px;
    classDef process fill:#bbf,stroke:#333,stroke-width:1px;
    classDef decision fill:#bfb,stroke:#333,stroke-width:1px;
    
    class NewsTrading,LoadConfig,LoadComponents component;
    class GetNewsBuffer,RunClassifier,ExecuteTrade,ProcessBatch process;
    class CheckActiveTrades,SignalGenerated,OrderSuccess decision;
Loading

Installation

# Clone the repository
git clone https://github.com/yourusername/crypto-news-trader.git
cd crypto-news-trader

# Install dependencies
pip install -r requirements.txt

Configuration

Edit config/config.yaml to specify desired components and API credentials:

# Component Selection
news_source: "news_sources.treeofalpha_ws_news.main.TreeAlphaNews"
data_source: "data_sources.binance_trades.main.BinanceTrades"
classifier: "classifiers.volume_reaction.main.VolumeReactionClassifier"

# Binance API Credentials
public_key: "your_api_key_here"
private_key: "path_to_your_private_key_file"

Quick Start

# Run the trading system
python main.py

Extending with Custom Components

Creating a Custom News Source

from news_sources.base import BaseNewsSource, NewsEvent

class CustomNewsSource(BaseNewsSource):
    def __init__(self):
        super().__init__()
        # Your initialization code
        
    async def start_stream(self):
        # Connect to your news source
        
    async def shutdown(self):
        # Clean up resources
        
    def get_news_buffer(self):
        # Return the current news buffer
        
    def clean_buffer(self):
        # Clean old entries from the buffer

Creating a Custom Data Source

from data_sources.base import BaseDataSource

class CustomDataSource(BaseDataSource):
    def __init__(self):
        super().__init__()
        # Your initialization code
        
    async def start_stream(self):
        # Start your data collection
        
    async def shutdown(self):
        # Clean up resources
        
    def get_data(self):
        # Return collected data

Creating a Custom Classifier

from classifiers.base import BaseClassifier

class CustomClassifier(BaseClassifier):
    def __init__(self):
        super().__init__()
        # Your initialization code
        
    def classify(self, news_event, supplementary_data=None, symbol=None):
        # Analyze news and data to generate signals
        # Return signal batch or None
        
    def shutdown(self):
        # Clean up resources

Creating a Custom Trader

class CustomTrader:
    def __init__(self):
        # Your initialization code
        
    async def connect(self):
        # Connect to exchange API
        
    async def process_signals(self, signal_batch):
        # Process and execute signals
        
    def get_active_trades(self, symbol=None):
        # Return active trades
        
    async def shutdown(self):
        # Clean up resources

Signal Format

Signals are passed between components using a standardized format:

signal_batch = {
    "timestamp": int,  # Timestamp in milliseconds
    "batch_id": str,   # Unique batch identifier
    "signals": [{
        "symbol": str,  # Trading pair symbol
        "exchange": str,  # Exchange identifier (e.g., "BINANCE")
        "strategy_id": str,  # Identifier for the strategy
        "timestamp": int,  # Signal timestamp
        "orders": [
            # List of order dictionaries 
            # (see order format documentation)
        ]
    }]
}

Order Format

Orders follow a specific format that supports advanced order types:

order = {
    "id": str,  # Order identifier (for dependencies)
    "type": str,  # Order type (LIMIT, MARKET, STOP_MARKET, etc.)
    "side": str,  # BUY or SELL
    "symbol": str,  # Trading pair symbol
    "quantity": str,  # Order quantity as string
    "price": str,  # Optional: Order price as string
    "stopPrice": str,  # Optional: Stop price for stop orders
    "timeInForce": str,  # Time in force (e.g., "GTC")
    "reduceOnly": bool,  # Optional: Whether order reduces position only
    "execution": {
        "trigger": {
            "type": str,  # IMMEDIATE or ORDER_PLACED
            "reference_orders": [str]  # Optional: IDs of reference orders
        }
    }
}

Performance Optimizations

  • WebSocket connections used throughout for minimal latency
  • Binary search for efficient trade timestamp matching
  • Smart caching of trade data to minimize processing time
  • Concurrent processing of multiple data streams
  • Separation of immediate and dependent orders for optimal execution

Logs

The system includes configurable logging with file rotation and error tracking for monitoring and debugging.

Contributing

Contributions are strongly encouraged! This project is designed to be extended and improved by the community. Ways to contribute:

  1. Implement New Components: Create custom news sources, data sources, classifiers, or traders
  2. Optimize Performance: Improve execution speed or memory usage
  3. Enhance Existing Components: Add features or fix bugs in current implementations
  4. Documentation: Improve or expand the documentation
  5. Testing: Add test cases or improve test coverage

Please submit pull requests with clear descriptions of changes and their purpose.

Disclaimer

This software is provided as a reference implementation for educational and research purposes only. It is not intended for direct deployment in live trading environments without additional development, testing, and risk management. The current code demonstrates the architecture and component interactions but requires customization for production use. Automated trading involves significant risk of financial loss. Use at your own risk and with proper testing.

License

MIT License

About

A high-performance, modular cryptocurrency trading system that automatically executes trades based on market news events. Built with a pluggable architecture for ultra-low latency execution, allowing traders to react to market-moving news before the broader market.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages