Bybit Multi-Exchange Trading Bot
๐ Unified Trading Bot with dual interface: Telegram Bot + MCP Server for AI integration.
๐ฏ Overview
A comprehensive cryptocurrency trading system that combines:
- ๐ฑ Telegram Bot: Mobile-first interface with natural language processing
- ๐ MCP Server: Model Context Protocol server for AI tools integration (Claude, etc.)
- ๐ Multi-Exchange Support: 6+ major cryptocurrency exchanges
- ๐ค LLM Integration: Natural language understanding for trading commands
- ๐ Real-time Data: Live market data and price comparisons
โจ Key Features
๐ค Dual Interface Architecture
- Telegram Bot: Chat-based trading interface
- MCP Server: HTTP API for external AI integration
- Unified Backend: Shared trading engine and data sources
๐ Multi-Exchange Support
| Exchange | Status | Features |
|---|---|---|
| Bybit | โ Primary | Full API (public + private endpoints) |
| Binance | โ Public | Market data, tickers, prices |
| KuCoin | โ Public | Market data, tickers, prices |
| OKX | โ Public | Market data, tickers, prices |
| Huobi | โ Public | Market data, tickers, prices |
| MEXC | โ Public | Market data, tickers, prices |
| Indodax | โ Public | IDR market data (Indonesia) |
๐ง Intelligent Features
- Natural Language Processing: Understand commands like "What's Bitcoin price?"
- Real-time Price Comparison: Compare prices across all supported exchanges
- Currency Conversion: Built-in USD/IDR converter with live rates
- Smart Error Handling: User-friendly error messages and fallbacks
- API Documentation Context: Contextual help in every response
๐ ๏ธ Installation & Setup
Prerequisites
- Python 3.11+ (recommended)
- Telegram Bot Token (from @BotFather)
- API Keys (optional, for private endpoints)
Step 1: Clone Repository
git clone https://github.com/Vanszs/Bybit_AutoTrade.git
cd Bybit_AutoTrade
Step 2: Python Environment Setup
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Linux/Mac:
source venv/bin/activate
# On Windows:
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
Step 3: Environment Configuration
Create .env file:
cp .env.example .env
Edit .env file:
# ===============================
# REQUIRED SETTINGS
# ===============================
# Telegram Bot (Required)
TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here
# LLM API (Required for natural language)
ZAI_API_KEY=your_llm_api_key_here
ZAI_BASE_URL=https://api.novita.ai/openai
# ===============================
# OPTIONAL SETTINGS
# ===============================
# Bybit API (Optional - for private endpoints)
BYBIT_API_KEY=your_bybit_api_key
BYBIT_API_SECRET=your_bybit_secret
BYBIT_PUBLIC_ONLY=true
# LLM Configuration
LLM_MODEL=gpt-3.5-turbo
LLM_ROUTER_MODEL=gpt-3.5-turbo
LLM_TEMPERATURE=0.7
# Bot Configuration
BOT_AUTH_REQUIRED=false
BYBIT_TESTNET=false
Step 4: Get Required API Keys
๐ค Telegram Bot Token
- Open Telegram and search for
@BotFather - Send
/newbotcommand - Follow instructions to create your bot
- Copy the token and add to
.envfile
๐ง LLM API Key (Novita GPT-OSS)
- Visit Novita AI
- Create account and get API key
- Add to
.envfile asZAI_API_KEY
๐ Bybit API Keys (Optional)
- Visit Bybit API Management
- Create API key with required permissions:
- Read: Account info, positions, orders
- Trade: Place/cancel orders (if needed)
- Add to
.envfile - Set
BYBIT_PUBLIC_ONLY=falseto enable private endpoints
Step 5: Verify Installation
# Test basic imports
python -c "
import sys
sys.path.insert(0, 'src')
from main import UnifiedTradingBot
print('โ
Installation successful!')
"
๐ Running the System
Option 1: Telegram Bot Only
python -m src.main
This starts the Telegram bot with integrated MCP client.
Option 2: MCP Server Only
python tools/mcp_server.py
This starts the MCP server on http://localhost:8001.
Option 3: Both (Recommended)
# Terminal 1: Start MCP Server
python tools/mcp_server.py
# Terminal 2: Start Telegram Bot
python -m src.main
๐ฑ Using the Telegram Bot
Basic Commands
/start # Initialize bot and see features
/help # Complete command guide
/status # System status and health check
# Market Data Commands
/price BTCUSDT bybit # Get BTC price from Bybit
/price ETHUSDT binance # Get ETH price from Binance
/compare BTCUSDT # Compare BTC across all exchanges
/exchanges # List all supported exchanges
# Utility Commands
/convert 100 USD to IDR # Currency converter
/convert 1500000 IDR to USD
# Private Commands (requires API keys)
/balance # Get wallet balance
Natural Language Examples
The bot understands natural language in Indonesian and English:
"Harga Bitcoin sekarang?"
"What's the current ETH price?"
"Compare BTC prices across exchanges"
"Show me SOL price on KuCoin"
"Berapa harga DOGE di Binance?"
"Convert 50 USD to IDR"
๐ MCP Server Integration
For Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"bybit-trading": {
"command": "python",
"args": ["/path/to/Bybit_AutoTrade/tools/mcp_server.py"],
"env": {
"BYBIT_PUBLIC_ONLY": "true"
}
}
}
}
Direct HTTP API Usage
# Get server info
curl http://localhost:8001/
# Get Bybit ticker
curl "http://localhost:8001/bybit/tickers?category=spot&symbol=BTCUSDT"
# Compare prices across exchanges
curl "http://localhost:8001/exchanges/compare-prices?symbol=BTCUSDT&exchanges=bybit,binance,kucoin"
# Get API documentation
curl "http://localhost:8001/api-docs?section=market"
# View API documentation
open http://localhost:8001/docs
๐๏ธ Project Structure
Bybit_AutoTrade/
โโโ ๐ README.md # This file
โโโ ๐ requirements.txt # Python dependencies
โโโ ๐ .env.example # Environment template
โโโ ๐ api-docs.txt # API documentation context
โ
โโโ ๐ src/ # Main application code
โ โโโ ๐ main.py # Unified bot entry point
โ โโโ ๐ config.py # Configuration management
โ โโโ ๐ bybit_client.py # Bybit V5 API client
โ โโโ ๐ exchange_client.py # Multi-exchange API client
โ โโโ ๐ mcp_telegram_client.py # MCP integration for Telegram
โ โโโ ๐ llm.py # LLM client (natural language)
โ โโโ ๐ auth.py # Authentication utilities
โ โโโ ๐ features.py # Technical analysis features
โ โโโ ๐ model.py # ML models for prediction
โ โโโ ๐ strategy.py # Trading strategies
โ โโโ ๐ natural_trading_assistant.py # NLP trading assistant
โ โโโ ๐ xgb_model.py # XGBoost model implementation
โ
โโโ ๐ tools/ # External tools
โ โโโ ๐ mcp_server.py # FastAPI MCP server
โ
โโโ ๐ scripts/ # Utility scripts
โ โโโ ๐ debug_llm.py # LLM debugging
โ โโโ ๐ debug_specific_query.py # Query testing
โ โโโ ๐ debug_tool_calls.py # Tool call debugging
โ โโโ ๐ simple_llm_test.py # Simple LLM test
โ
โโโ ๐ docs/ # Documentation
โ โโโ ๐ MCP_TELEGRAM_INTEGRATION.md # MCP integration guide
โ โโโ ๐ multi_exchange.md # Multi-exchange documentation
โ โโโ ๐ natural_language_implementation.md # NLP implementation
โ โโโ ๐ private_endpoints.md # Private endpoint guide
โ
โโโ ๐ data/ # Runtime data
โโโ ๐ auth.json # Authentication store
๐งช Testing & Debugging
Test Individual Components
# Test LLM integration
python scripts/debug_llm.py
# Test specific queries
python scripts/debug_specific_query.py
# Test tool calls
python scripts/debug_tool_calls.py
# Simple LLM functionality test
python scripts/simple_llm_test.py
Test MCP Server
# Start MCP server
python tools/mcp_server.py
# Test endpoints (in another terminal)
curl http://localhost:8001/
curl "http://localhost:8001/bybit/tickers?category=spot&symbol=BTCUSDT"
curl "http://localhost:8001/exchanges/compare-prices?symbol=BTCUSDT"
Test Exchange Connections
python -c "
import asyncio
import sys
sys.path.insert(0, 'src')
async def test_exchanges():
from exchange_client import ExchangeClient
client = ExchangeClient()
exchanges = ['bybit', 'binance', 'kucoin']
for exchange in exchanges:
try:
result = await client.get_ticker('BTCUSDT', exchange)
print(f'โ
{exchange}: Connected')
except Exception as e:
print(f'โ {exchange}: {str(e)}')
await client.close()
asyncio.run(test_exchanges())
"
๐ Security & Best Practices
API Key Security
- Never commit API keys to version control
- Use environment variables for all sensitive data
- Enable IP restrictions on API keys when possible
- Use read-only permissions when trading is not required
- Test with testnet first before using mainnet
Safe Development
# Always use testnet for development
BYBIT_TESTNET=true
# Start with public-only mode
BYBIT_PUBLIC_ONLY=true
# Enable private endpoints only when needed
BYBIT_PUBLIC_ONLY=false
Production Deployment
# Disable debug logging
export LOG_LEVEL=INFO
# Use production API keys with proper restrictions
# Set up proper firewall rules
# Monitor API usage and rate limits
๐ณ Docker Deployment
Dockerfile
FROM python:3.11-slim
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy application
COPY . .
# Create data directory
RUN mkdir -p data
# Expose MCP server port
EXPOSE 8001
# Default command (can be overridden)
CMD ["python", "-m", "src.main"]
Docker Compose
version: '3.8'
services:
bybit-bot:
build: .
environment:
- TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
- ZAI_API_KEY=${ZAI_API_KEY}
- BYBIT_PUBLIC_ONLY=true
volumes:
- ./data:/app/data
restart: unless-stopped
mcp-server:
build: .
command: python tools/mcp_server.py
ports:
- "8001:8001"
environment:
- BYBIT_PUBLIC_ONLY=true
restart: unless-stopped
Build and Run
# Build image
docker build -t bybit-trading-bot .
# Run Telegram bot
docker run -d --env-file .env bybit-trading-bot
# Run MCP server
docker run -d -p 8001:8001 --env-file .env bybit-trading-bot python tools/mcp_server.py
๐ง Configuration Guide
Environment Variables Reference
| Variable | Required | Default | Description |
|---|---|---|---|
TELEGRAM_BOT_TOKEN |
โ Yes | - | Telegram bot token from @BotFather |
ZAI_API_KEY |
โ Yes | - | LLM API key for natural language |
ZAI_BASE_URL |
No | novita.ai | LLM API base URL |
BYBIT_API_KEY |
No | - | Bybit API key (for private endpoints) |
BYBIT_API_SECRET |
No | - | Bybit API secret |
BYBIT_PUBLIC_ONLY |
No | true |
Restrict to public endpoints only |
BYBIT_TESTNET |
No | false |
Use Bybit testnet |
LLM_MODEL |
No | gpt-3.5-turbo |
LLM model name |
LLM_TEMPERATURE |
No | 0.7 |
LLM response creativity (0-1) |
BOT_AUTH_REQUIRED |
No | false |
Require authentication for bot |
Exchange Configuration
The bot supports multiple exchanges with different capabilities:
# Public endpoints (no API key required)
exchanges = [
'bybit', # Primary exchange with full support
'binance', # Market data only
'kucoin', # Market data only
'okx', # Market data only
'huobi', # Market data only
'mexc', # Market data only
'indodax', # Indonesian exchange (IDR pairs)
]
# Private endpoints (API key required)
private_exchanges = [
'bybit', # Trading, balance, positions
]
๐จ Troubleshooting
Common Issues
1. Bot doesn't respond to commands
# Check bot token
echo $TELEGRAM_BOT_TOKEN
# Verify bot token with Telegram API
curl "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getMe"
# Check bot logs
python -m src.main
2. LLM not working
# Test LLM connection
python scripts/simple_llm_test.py
# Check API key
echo $ZAI_API_KEY
# Verify API endpoint
curl -H "Authorization: Bearer $ZAI_API_KEY" $ZAI_BASE_URL/models
3. Exchange API errors
# Test exchange connections
python -c "
import asyncio, sys
sys.path.insert(0, 'src')
from exchange_client import ExchangeClient
async def test():
client = ExchangeClient()
result = await client.get_ticker('BTCUSDT', 'bybit')
print(result)
await client.close()
asyncio.run(test())
"
4. MCP Server not starting
# Check port availability
lsof -i :8001
# Start with debug logging
python tools/mcp_server.py --debug
# Test MCP endpoints
curl http://localhost:8001/
5. Private endpoints not working
# Verify API keys are set
echo "API Key: ${BYBIT_API_KEY:0:8}..."
echo "Public only: $BYBIT_PUBLIC_ONLY"
# Test API key permissions
python -c "
import sys
sys.path.insert(0, 'src')
from bybit_client import BybitClient
client = BybitClient()
print('Can access private:', client.can_access_private_endpoints())
"
Performance Optimization
# Monitor memory usage
python -c "
import psutil
print(f'Memory: {psutil.virtual_memory().percent}%')
print(f'CPU: {psutil.cpu_percent()}%')
"
# Check API rate limits
tail -f logs/api_requests.log
# Optimize for production
export PYTHONOPTIMIZE=1
export PYTHONDONTWRITEBYTECODE=1
๐ Monitoring & Logging
Log Levels
# Debug (development)
export LOG_LEVEL=DEBUG
# Info (production)
export LOG_LEVEL=INFO
# Error only
export LOG_LEVEL=ERROR
Health Checks
# Bot health check
curl http://localhost:8001/health
# Exchange connectivity
python scripts/debug_specific_query.py
# Database connectivity (if using)
python -c "import sqlite3; print('DB OK')"
๐ค Contributing
Development Setup
# Clone and setup
git clone https://github.com/Vanszs/Bybit_AutoTrade.git
cd Bybit_AutoTrade
# Create development environment
python -m venv venv-dev
source venv-dev/bin/activate
pip install -r requirements.txt
# Install development dependencies
pip install pytest black flake8 mypy
# Run tests
python -m pytest tests/
# Format code
black src/
flake8 src/
Contribution Guidelines
- Fork the repository
- Create feature branch:
git checkout -b feature/new-feature - Make changes with proper tests
- Follow code style: Use black formatter
- Update documentation if needed
- Test thoroughly with both testnet and mainnet
- Submit pull request with clear description
Code Style
# Format Python code
black src/ tools/ scripts/
# Check style
flake8 src/ --max-line-length=100
# Type checking
mypy src/
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
โ ๏ธ Disclaimer
Important: This software is for educational and research purposes only.
- Trading Risk: Cryptocurrency trading involves substantial financial risk
- No Warranty: Software provided "as is" without warranty of any kind
- Not Financial Advice: This is not investment advice
- User Responsibility: Users are responsible for their own trading decisions
- API Limits: Respect exchange API rate limits and terms of service
Risk Management
- Start with testnet and small amounts
- Never invest more than you can afford to lose
- Understand the markets before automated trading
- Monitor bot behavior closely
- Have stop-loss strategies in place
๐ Support
Documentation
- ๐ Full API Documentation - Complete API reference
- ๐ MCP Integration Guide - Claude integration
- ๐ Multi-Exchange Guide - Exchange-specific docs
- ๐ค Natural Language - NLP features
Community
- ๐ Issues - Bug reports and feature requests
- ๐ฌ Discussions - Community discussions
- ๐ง Contact - Direct support
Quick Links
- ๐ Bybit API Docs - Official Bybit API
- ๐ค Telegram Bot API - Telegram bot development
- ๐ Model Context Protocol - MCP specification
- ๐ง Novita AI - LLM API provider
๐ Happy Trading with Multi-Exchange MCP Integration!
Built with โค๏ธ by the Bybit AutoTrade community
- To enable trading and other private endpoints, set
BYBIT_PUBLIC_ONLY=falseand provide valid API keys. - For faster small-talk replies, consider a lighter
LLM_ROUTER_MODELand smallerLLM_MAX_TOKENS(e.g., 512โ2048).
Run MCP Server (optional)
- Install deps:
pip install -r requirements.txt - Start server:
python -m tools.mcp_server - The server exposes two example tools:
health_checkandecho. Extend to callZaiClientor Bybit.
Telegram Commands
/startand/help: Info bantuan./time: Server time Bybit (public, production)./ticker <category> [symbol]: Ticker market V5, kategorispot|linear|inverse|option./balance [COIN]: Wallet balance V5 (private, defaultaccountType=UNIFIED)./signal <category> <symbol> <interval> <mode>: Prediksi XGBoost (scalping/swing) dari kline terkini./ask <pertanyaan>: Natural language router โ LLM memilih aksi (tickers, kline, orderbook, trades, balance, positions, orders, create/cancel order) dan bot mengeksekusi.