Back to Projects
Quantitative Trading • Project

Trailing Edge Trading Bot

High-Performance Async Trading Bot with Dynamic Trailing Take-Profit

Python Async (asyncio) WebSockets Ed25519 Authentication Donchian Channels ATR Indicators Telegram Bot API Systemd
2024 - 2025
Python 3.10
Production Trading System

Overview

Trailing Edge is a high-performance asynchronous Python trading bot for Binance featuring dynamic trailing take-profit strategies, regime detection, Donchian channel gating, and Ed25519 authentication. Built for 24/7 autonomous operation on low-cost infrastructure with low-latency reaction times.

The system processes high-velocity market data streams using asyncio for non-blocking I/O. It implements sophisticated trading strategies including dynamic trailing stops with exponential decay, automatic regime switching between BASE and QUOTE modes, and intelligent hard stops with Donchian channel re-entry gating.

Deployed as a systemd service for production reliability, the bot features comprehensive logging, Telegram notifications, and graceful shutdown handling. It uses WebSocket reconciliation for stateless operation, eliminating the need for database persistence.

Key Achievement: Built production-ready 24/7 trading bot with async architecture, WebSocket reconciliation, systemd deployment, and Ed25519 authentication for autonomous operation

Key Metrics & Results

Async
Architecture
Systemd
Deployment
WebSocket
Reconciliation
Ed25519
Authentication

Problem Statement

Cryptocurrency trading requires real-time market data processing, low-latency order execution, and 24/7 autonomous operation. Traditional synchronous architectures cannot handle high-frequency market data streams without blocking or dropping events. Trading bots need sophisticated risk management with dynamic stop-loss and take-profit strategies.

Business Context

Market opportunities in cryptocurrency trading occur in milliseconds. Manual trading is impossible at scale. Automated systems must process thousands of market updates per second while maintaining low-latency reaction times for order execution. Risk management is critical to prevent catastrophic losses.

Technical Challenges

Solution Architecture

An event-driven async architecture: (1) WebSocket streams for market data and account updates, (2) Async event loop processing all streams concurrently, (3) Regime detection switching between BASE (inventory) and QUOTE (cash) modes, (4) Dynamic trailing take-profit with exponential decay, (5) Donchian channel-based hard stops with intelligent re-entry gating.

System Components

Async WebSocket Layer

Handles concurrent market data and account streams using asyncio. Features auto-reconnect with exponential backoff. Processes high-velocity updates without blocking.

Regime Detection System

Automatically switches between BASE (holding inventory) and QUOTE (holding cash) modes based on balance updates. Implements all-in compounding strategy for bull rallies.

Dynamic Trailing Take-Profit

Callback-based profit-taking with exponential decay from start to min factor. Automatically adjusts profit targets as gains increase, locking in profits while allowing upside.

Donchian Channel Hard Stop

Volatility-aware stop-loss using Donchian Channels. Implements smart re-entry gating that waits for price to cross mid-channel in favorable direction after stop triggers.

Persistent Maker Exit

Uses Binance order.replace API for aggressive price-chasing. Places post-only maker orders that persist until fill or manual override.

Technology Stack Rationale

asyncio chosen over threading for I/O-bound workload (WebSocket streams). Ed25519 provides secure, non-expiring authentication. Donchian Channels react instantly to price action (forward-looking) vs ATR (backward-looking). Systemd provides production-grade service management. WebSocket reconciliation eliminates database latency.

Implementation Highlights

Key Features

Detailed Code Documentation

Deep dive into the technical implementation with annotated code examples

View Technical Details

Challenges & Solutions

Challenge 1

Processing high-velocity market data streams without blocking the trading loop

Solution

Implemented async architecture with asyncio.Queue for non-blocking I/O. Generator emits logs in batches to reduce overhead. All WebSocket streams processed concurrently on single event loop.

Challenge 2

Maintaining state consistency across WebSocket reconnections

Solution

Implemented WebSocket reconciliation pattern. Jumpstart orders trigger immediate balance snapshots. System rebuilds state from stream events, eliminating need for database persistence.

Challenge 3

Implementing sophisticated trailing stop with dynamic adjustments

Solution

Created callback-based system with exponential decay function. Trailing factor decreases from start to min as price moves favorably. Automatically locks in profits while preserving upside potential.

Challenge 4

Handling partial fills and order management at scale

Solution

Uses Binance order.replace API for persistent maker orders. Tracks order state in-memory and replaces orders every loop iteration to chase best prices. Guarantees fill or manual override.

Results & Impact

Deployed production trading bot running 24/7 on cloud VPS. System processes high-velocity market updates with low-latency reaction times. Achieved autonomous operation with systemd service management. Comprehensive test coverage ensures reliability.

Production Performance

  • Processes high-velocity market data streams without dropping events
  • Low-latency reaction time for order execution
  • 24/7 autonomous operation with systemd auto-restart
  • Memory-efficient async architecture (no database overhead)
  • Comprehensive test coverage with pytest

Lessons Learned

What Worked Well

What I'd Do Differently

Future Enhancements

Related Projects