Binance Spot Algo Trader

Open Source High-Frequency Trading System · 2023-2024

Visit GitHub

<100ms

API Response

3-4s

Cycle Time

800-1200/min

API Calls

Zero

Rate Violations

Overview

An open-source high-frequency trading (HFT) system built for Binance Spot markets, executing triangular arbitrage strategies across cryptocurrency pairs. The bot monitors real-time price inefficiencies, automatically places orders, and manages complex multi-step transactions with sub-second execution times. Built entirely with pure Node.js for maximum performance—eliminating framework overhead to achieve optimal latency for high-frequency operations.

The Challenge

Situation

High-frequency trading requires sub-second execution and split-second decision making. Triangular arbitrage opportunities in cryptocurrency markets exist for less than 1 second before market forces close the gap. Additionally, Binance enforces strict rate limits (1200 requests/minute with weight-based system) and any violation results in temporary IP bans.

Task

Build a production-grade HFT bot capable of detecting arbitrage opportunities across 3 trading pairs (USDT → ETH → BTC → USDT), executing a complex 4-transaction workflow with partial fill handling, conditional reversal logic, and comprehensive error recovery—all while maintaining zero rate-limit violations and sub-second latency.

My Approach

  • Designed pure Node.js architecture with zero framework overhead, achieving sub-100ms API latency
  • Implemented Bottleneck.js rate limiter with priority queuing and API weight tracking to prevent violations
  • Built 4-transaction arbitrage workflow with conditional logic for reversals when profitability disappears
  • Developed aggressive order polling (every 100ms) for real-time status monitoring and quick reaction to partial fills
  • Implemented partial fill handling that forwards executed quantity to next transaction and retries remaining
  • Added multi-layer error handling covering API errors (all Binance codes mapped), transaction logic failures, and process-level crashes
  • Integrated PM2 for automatic crash recovery (<1 second restart), process monitoring, and zero-downtime restarts
  • Built Winston structured logging with 14-day rotation and CSV reporting for trade analysis and tax compliance

Results & Impact

  • Open-sourced on GitHub (MIT License) as solo project with comprehensive documentation
  • Achieved sub-100ms API response times through pure Node.js implementation (vs 100-200ms industry standard)
  • Successfully handles 800-1200 API calls/minute with zero rate-limit violations
  • Executes complete 3-pair arbitrage cycles in 3-4 seconds (industry standard: 5-10 seconds)
  • Production-ready error recovery with automatic process restart in under 1 second
  • 66.7% success rate on executed trades with comprehensive partial fill recovery

Tech Stack

Node.js (Pure)
PM2
Winston
Bottleneck.js
Binance Spot API
dotenv
Nodemailer

Triangular Arbitrage Flow

USDTETHBTCUSDTBinancePM2Rate LimiterLogger
Animated flow
Click for details
Tap to interact

3-step cycle: USDT → ETH → BTC → USDT (with profit after 0.3% fees)

Infrastructure: PM2 auto-restart + Bottleneck.js rate limiting + Winston logging for 3-4s cycles

Performance vs Industry Standard

Price fetch latency

50-80msvs 100-200ms

Order placement

100-150msvs 200-400ms

Full cycle time

3-4 secondsvs 5-10 seconds

API efficiency

800-1200/minvs 500-800/min

Binance API Error Handling

-1021Timestamp sync

NTP clock synchronization

-1003Rate limit hit

Bottleneck.js auto-backoff

-2010Insufficient balance

Log and skip trade

-1013Invalid quantity

Round to LOT_SIZE precision

NetworkECONNREFUSED/ETIMEDOUT

Exponential backoff (3 attempts)

Key Technical Decisions

  • Pure Node.js over Express.js: Eliminated middleware overhead to achieve sub-100ms latency (Express adds +10-50ms per request)
  • Limit GTC orders exclusively: Better pricing through maker fee rebates (0.1% per trade) vs market order slippage
  • 100ms aggressive polling: Near real-time order awareness for quick partial fill reactions (vs 1s standard polling)
  • 4-transaction conditional workflow: Automatic reversal when profitability disappears mid-execution preserves capital
  • Partial fill forwarding: Executed quantity proceeds to next transaction while remaining retries—no wasted fills
  • Bottleneck.js priority queue: Order placement prioritized over price fetching to maximize execution speed
  • 90% drawdown restart: Prevents spiraling losses by restarting session with fresh market conditions
  • CSV over database: Lightweight trade logging enabling easy external analysis and tax reporting
  • API weight tracking: Proactive monitoring (475 weight/min usage vs 1200 limit) prevents violations before they happen