This document highlights key code sections that demonstrate the technical strengths and architectural patterns implemented in this production trading bot.
Trailing Edge is a high-performance asynchronous Python trading bot for Binance. The system demonstrates production-focused patterns including async architecture, WebSocket reconciliation, systemd deployment, and Ed25519 authentication.
File: src/trailingedge/main.py
Lines: 422-502
The main trading loop demonstrates clean async architecture with concurrent WebSocket streams.
async def main_trading_loop(ws):
"""
Main trading loop for dynamic trailing stop-loss strategy.
"""
# Subscribe to user data stream
await subscribe_user_stream(ws)
# Initialize snapshots
account_snapshot = {}
_account_task = asyncio.create_task(account_ws_receiver(ws, account_snapshot))
book_snapshot = {}
_book_task = asyncio.create_task(stream_bookticker_shared(SYMBOL, book_snapshot))
kline_snapshot = {}
_kline_task = asyncio.create_task(stream_kline_shared(SYMBOL, "1m", kline_snapshot))
# Wait for snapshots to be ready
await wait_for_market_snapshot(
book_snapshot, ["bid_price", "ask_price"], label="BookTicker", timeout=10
)
Why it's notable:
- Concurrent async tasks for multiple WebSocket streams
- Non-blocking snapshot initialization
- Proper task management with asyncio.create_task
- Timeout handling for snapshot readiness
File: src/trailingedge/websocket/account_stream.py
Lines: 27-43
The account stream receiver demonstrates stateless reconciliation from WebSocket events.
async def account_ws_receiver(ws, snapshot_dict):
"""
Listens for WS messages and updates snapshot_dict for balance changes.
Never breaks on error; always continues unless ws is closed.
"""
while True:
try:
msg = await ws.recv()
parse_account_balance_event(msg, snapshot_dict)
except websockets.exceptions.ConnectionClosed:
print(f"[{now()}] [ERROR] WS connection closed in account_ws_receiver, exiting loop.")
break # <-- Break ONLY on true disconnection!
except Exception as e:
print(f"[{now()}] [ERROR] in account_ws_receiver: {e}")
continue # <-- Continue for all other (parsing/noise) errors
Why it's notable:
- Stateless operation: rebuilds state from stream events
- Resilient error handling: continues on parsing errors
- Only breaks on true disconnection
- Eliminates need for database persistence
File: src/trailingedge/auth/manager.py
Lines: Authentication implementation
The system uses Ed25519 for secure, non-expiring authentication.
Why it's notable:
- Modern asymmetric cryptography
- Non-expiring credentials (no token refresh needed)
- Secure key-based authentication
- Eliminates shared-secret risks
File: Deployment documentation
Lines: Systemd service setup
The bot is configured as a systemd service for 24/7 operation.
Why it's notable:
- Auto-restart on failure
- Resource limits configuration
- Journald logging integration
- Production-grade service management