AI Trading Bot Data Pipeline: Kafka, n8n & Telegram Notifications

AI Trading Bot Data Pipeline – the infrastructure that powers real-time notifications. n8n orchestrates everything: consuming 4 Kafka topics, writing to the datalake, and sending Telegram alerts when positions are opened or closed.

The Challenge: Real-Time Trading Notifications

When building an autonomous AI Trading Bot, staying informed about its activities is crucial. You need to know when trades are opened, closed, when targets are hit, or when stop losses are triggered – all in real-time, wherever you are. This AI Trading Bot Data Pipeline solves exactly that.

The solution? A message-driven architecture using Apache Kafka for event streaming, n8n for workflow automation, and Telegram for instant mobile notifications.

AI Trading Bot Data Pipeline Architecture

The AI Trading Bot produces events to 4 Kafka topics. n8n acts as the central orchestrator, consuming all topics and routing them to their destinations:

                    ┌─ Market Topic ──┐
                    ├─ Alerts Topic ──┤
Trading Bot → Kafka ├─ Decisions Topic┼→ n8n → Datalake
                    └─ Executions Topic┘
                              │
                              └────────→ n8n → Telegram

Each component plays a specific role:

  • Trading Bot: Produces events to 4 Kafka topics (Market, Alerts, Decisions, Executions)
  • Apache Kafka: Message broker ensuring reliable event delivery and persistence
  • n8n Orchestrator: Central hub that consumes ALL Kafka topics and routes them
  • Datalake: n8n batches messages and writes via SSH for historical analysis
  • Telegram: n8n sends instant notifications when positions open/close

Apache Kafka: The Event Backbone

Apache Kafka serves as the central nervous system for all trading events. It provides:

  • Durability: Messages are persisted and can be replayed
  • Scalability: Handle thousands of events per second
  • Decoupling: Producers and consumers work independently
  • Ordering: Messages within a partition maintain order

The AI Trading Bot uses 4 dedicated Kafka topics:

  • Market Topic: Real-time price data and market indicators
  • Alerts Topic: System alerts, warnings, and error notifications
  • Decisions Topic: AI decision logs with scores and reasoning
  • Executions Topic: Trade executions (opens, closes, updates)

Each message contains structured JSON data:

{
  "event_type": "TRADE_OPENED",
  "symbol": "BTC-USD",
  "direction": "LONG",
  "entry_price": 94250.50,
  "target_price": 96500.00,
  "stop_loss": 93000.00,
  "leverage": 3,
  "bot_name": "trend_follower",
  "timestamp": "2025-12-30T19:58:11Z",
  "priority": "normal"
}

n8n: The Central Orchestrator

n8n is the central orchestrator for the entire AI Trading Bot data pipeline. It runs two main workflows that handle all data routing:

Workflow 1: Kafka to Datalake

This workflow consumes all 4 Kafka topics simultaneously, batches messages by file, and writes them to the datalake via SSH:

AI Trading Bot Data Pipeline - n8n workflow consuming 4 Kafka topics to Datalake
n8n workflow consuming Market, Alerts, Decisions, and Executions topics – batching and writing to datalake

Workflow 2: Kafka to Telegram

This workflow listens for trade notifications and sends instant alerts to Telegram when positions are opened or closed:

AI Trading Bot Data Pipeline - Kafka to Telegram notifications via n8n
n8n workflow: Kafka Trigger → Parse Message → Send Telegram notification

Key Workflow Components

Kafka Trigger

Listens to the trading.crypto.notifications topic with consumer group n8n-telegram-notifications

Message Parser

Extracts JSON payload and validates required fields for processing

Priority Router

Routes critical events (liquidations, large P&L) differently from normal trade notifications

Retry Logic

Critical messages retry 5 times, normal messages retry 3 times on failure

Telegram Notifications

The final destination for trading events is a dedicated Telegram channel. Each notification is formatted for quick readability with key information at a glance:

NEW TRADE OPENED
Symbol: BTC-USD
Direction: LONG
Entry: $94,250.50
Target: $96,500.00 (+2.4%)
Stop: $93,000.00 (-1.3%)
Leverage: 3x
Bot: Trend Follower

Different event types trigger different notification formats:

  • TRADE_OPENED: Entry details, targets, stop loss, risk/reward
  • TRADE_CLOSED: Exit price, P&L amount and percentage, duration
  • TARGET_HIT: Success notification with profit details
  • STOP_LOSS_HIT: Loss notification with post-mortem data
  • POSITION_UPDATE: Changes to existing positions
  • SYSTEM_ALERT: Bot errors, API issues, connectivity problems

AI Trading Bot Data Lake

The n8n datalake workflow batches messages from all 4 Kafka topics and writes them to the datalake via SSH. This enables:

  • Performance Tracking: Calculate win rates, average P&L, drawdowns
  • Strategy Analysis: Compare bot performance over time
  • Pattern Recognition: Identify which market conditions favor each strategy
  • Backtesting: Validate new strategies against historical data
  • Audit Trail: Complete record of all trading activity

The data lake stores every event with full context, enabling complex queries like “What was the win rate for LONG trades on BTC during high Fear & Greed periods?”

Docker Deployment

The entire pipeline runs in Docker containers, making it easy to deploy and manage on a home server or cloud instance:

services:
  kafka:
    image: confluentinc/cp-kafka:latest
    ports:
      - "9092:9092"

  n8n:
    image: n8nio/n8n
    ports:
      - "5678:5678"
    volumes:
      - ./n8n-data:/home/node/.n8n

  postgres:
    image: postgres:14
    volumes:
      - ./postgres-data:/var/lib/postgresql/data

All services communicate over a shared Docker network, with Kafka accessible internally at kafka:9092.

AI Trading Bot Data Pipeline Benefits

Reliability

Kafka persists messages, so even if n8n is temporarily down, no notifications are lost. They queue up and process when the service recovers.

Extensibility

Add new consumers easily – Discord notifications, email alerts, or additional analytics without changing the trading bot code.

Observability

Full visibility into what the trading bot is doing, when trades happen, and how strategies perform over time.

Decoupling

Trading bot focuses on trading logic. Notification logic lives in n8n. Each component can be updated independently.


AI Trading Bot Data Pipeline: Conclusion

Building an AI Trading Bot Data Pipeline may seem like overkill for a personal project, but the benefits are substantial. Real-time visibility into trading activity, a historical record for analysis, and the flexibility to extend the system make this AI Trading Bot Data Pipeline architecture worthwhile.

Combined with the AI Trading Bot from Part 1, this AI Trading Bot Data Pipeline creates a complete autonomous trading system that keeps you informed without requiring constant monitoring.

Disclaimer: This is a personal project running on testnet for educational purposes. Cryptocurrency trading involves significant risk. Never trade with money you cannot afford to lose.

Related Reading

1 thought on “AI Trading Bot Data Pipeline: Kafka, n8n & Telegram Notifications”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top