Skip to content

TeoVibe/ai-crypto-tradebot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Professional Sentiment-Driven Solana Trading Bot

A microservices-based AI trading system that monitors Twitter for cryptocurrency signals, analyzes sentiment using large language models, and executes trades on Solana DEX (Raydium). The system uses a decoupled architecture with Redis message streaming for reliable communication between services.

🎯 How It Works

The system consists of two independent microservices communicating via Redis streams:

Sentiment Service (Analysis & Signal Generation)

1. Trending Token Discovery

The sentiment service fetches the top trending Solana tokens from the Raydium API (sorted by 24h trading volume). This drives targeted Twitter searches instead of using a single broad query.

  • Fetches from https://api-v3.raydium.io/pools/info/list?poolSortField=volume24h
  • Filters out stablecoins and wrapped SOL
  • Results are cached in Redis for 15 minutes to avoid excessive API calls
  • Configurable: top N tokens, minimum 24h volume threshold
  • Falls back to a static TWITTER_QUERY if the Raydium API is unreachable
  • Can be disabled entirely with USE_TRENDING=false

2. Tweet Discovery & Monitoring

For each trending token, the sentiment service searches Twitter (via RapidAPI) using targeted queries like "$SYMBOL solana". This yields more relevant results than a single generic search.

3. Intelligent Deduplication

  • Uses Redis to track already-processed tweets (7-day TTL)
  • Fetches multiple tweets per search and filters out seen ones
  • Prevents wasting LLM calls on repeated content
  • Logs: Fetched X tweets, Y unseen

4. Sentiment Analysis & Signal Extraction

When an unseen tweet is found, it's analyzed by an AI language model (Ollama or OpenRouter) that:

  • Extracts Solana token addresses (validates base58 format)
  • Determines sentiment (bullish, bearish, neutral, or spam)
  • Assigns a confidence score (0-100) based on signal clarity
  • Provides reasoning for the analysis

5. Credibility Scoring

The service calculates a credibility score for the tweet author based on:

  • Number of followers (higher = more credible)
  • Verification status (verified users get +30 points)
  • Combined score ranges from 0-100

6. Trade Signal Publishing

When a trade signal meets criteria, the sentiment service:

  • Generates a unique order ID
  • Publishes the trade order to Redis trade_orders stream
  • Does NOT execute trades itself (separation of concerns)
  • Trades are only published if ALL criteria are met:
    • βœ… Valid Solana token address detected
    • βœ… Sentiment is bullish
    • βœ… Confidence score β‰₯ MIN_CONFIDENCE (default 60%)
    • βœ… Credibility score β‰₯ MIN_CREDIBILITY (default 30%)
    • βœ… Token hasn't been traded before (prevents duplicates)

Trader Service (Execution Engine)

7. Order Consumption

  • Listens on Redis trade_orders stream
  • Receives trade signals from the sentiment service
  • Processes orders in real-time using Redis consumer groups

8. Trade Execution

  • Paper Trading Mode (default): Logs would-be trades without executing
  • Live Trading Mode: Executes real swaps on Solana using Raydium DEX
  • Each trade is executed independently and tracked

9. Result Reporting

  • Publishes execution results back to Redis trade_results stream
  • Sentiment service receives confirmations asynchronously
  • All trades are logged locally by both services

Communication Flow

Sentiment Service          Redis Streams           Trader Service
      β”‚                                                  β”‚
      │── Analyze Tweet                                  β”‚
      β”‚                                                   β”‚
      │── Publish Order ──────►  trade_orders  ─────────►│
      β”‚                                                   β”‚
      β”‚                                            Execute Swap
      β”‚                                                   β”‚
      │◄─── Result ───────────  trade_results  ◄─────────│
      β”‚                                                   β”‚
   Log Result                                        Log Result

πŸ“Š Sentiment Analysis Algorithm

The bot uses a sophisticated multi-factor analysis:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Tweet Found    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  AI Sentiment Analysis  β”‚
β”‚  β€’ Token extraction     β”‚
β”‚  β€’ Sentiment (B/B/N/S)  β”‚
β”‚  β€’ Confidence (0-100)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Credibility Scoring    β”‚
β”‚  β€’ Follower count       β”‚
β”‚  β€’ Verification status  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Trade Decision Logic   β”‚
β”‚  IF bullish AND         β”‚
β”‚     confidence β‰₯ 60 AND β”‚
β”‚     credibility β‰₯ 30    β”‚
β”‚  THEN execute trade     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Execute or Simulate    β”‚
β”‚  β€’ Paper: Log only      β”‚
β”‚  β€’ Live: Swap on DEX    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Docker & Docker Compose
  • Twitter/X API access via RapidAPI
  • Solana wallet with private key (for live trading)
  • Optional: OpenRouter API key (if not using local Ollama)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd velesio-trader
  2. Configure environment:

    cp .env.example .env
    nano .env  # Edit with your API keys
  3. Start the services (paper trading mode by default):

    docker compose up --build
  4. Access the dashboard:

    http://localhost:3000
    
  5. View logs:

    # Sentiment service
    docker compose logs -f sentiment
    
    # Trader service
    docker compose logs -f trader
    
    # Redis
    docker compose logs -f redis

Service Ports

βš™οΈ Configuration

Paper Trading Mode (Safe Testing)

By default, both services run in PAPER TRADING mode - the sentiment service will analyze tweets and publish signals, but the trader service won't execute real trades. You'll see logs like:

Sentiment service:

INFO Searching tweets: solana token
INFO Fetched 15 tweets, 3 unseen
INFO Trade order [abc123] published: 0.0001 SOL β†’ <token_address>

Trader service:

INFO Received trade order [abc123]: 100000 lamports β†’ <token_address> (paper=True)
INFO PAPER TRADE logged: 0.0001 SOL β†’ <token_address>
INFO Result published: paper_executed

To enable live trading (⚠️ uses real funds!):

  1. Set PAPER_TRADING=false in your .env file
  2. Ensure your wallet has sufficient SOL balance
  3. Restart the services with docker compose up --build

Environment Variables

Create a .env file in the root directory. Variables are shared between services as needed:

# ── Sentiment Service Configuration ──────────────────────────────

# Twitter API (get from RapidAPI)
RAPIDAPI_KEY=your_rapidapi_key_here
TWITTER_QUERY=solana token     # Fallback query when trending is off/unavailable
POLL_INTERVAL=300

# Trending Token Discovery (Raydium API)
USE_TRENDING=true              # true = search by trending tokens, false = use TWITTER_QUERY
TRENDING_TOP_N=10              # Number of top tokens to track
TRENDING_MIN_VOLUME=100000     # Minimum 24h volume (USD) to qualify

# Trading Criteria (used by sentiment service)
MIN_CONFIDENCE=60   # Minimum sentiment confidence (0-100)
MIN_CREDIBILITY=30  # Minimum user credibility (0-100)

# LLM Configuration
USE_OLLAMA=true
OLLAMA_MODEL=gemma3
# If using OpenRouter instead:
# USE_OLLAMA=false
# OPENROUTER_API_KEY=your_key_here

# ── Trader Service Configuration ──────────────────────────────

# Solana Configuration (required for live trading)
PRIVATE_KEY=[your,solana,private,key,array]
RPC_URL=https://api.mainnet-beta.solana.com

# ── Shared Configuration ──────────────────────────────────────

# Paper Trading Mode (affects both services)
PAPER_TRADING=true  # true = simulate, false = execute real trades

# Trade Amount
SOL_AMOUNT=0.0001   # Amount of SOL per trade

# Redis Connection (auto-configured in docker-compose)
REDIS_HOST=redis
REDIS_PORT=6379

# Bot Auto-start (optional)
AUTO_START_BOT=false  # true = bot starts automatically with container

Note: The sentiment service uses Twitter/LLM configs, while the trader service uses Solana configs. Both services share the PAPER_TRADING setting to ensure consistency.

Adjusting Risk Parameters

Control trading aggressiveness by adjusting thresholds:

# Conservative (fewer trades, higher quality)
MIN_CONFIDENCE=80
MIN_CREDIBILITY=60

# Moderate (balanced)
MIN_CONFIDENCE=60
MIN_CREDIBILITY=30

# Aggressive (more trades, higher risk)
MIN_CONFIDENCE=40
MIN_CREDIBILITY=20

Custom Search Queries

Target specific signals by customizing the Twitter search:

# Monitor specific user
TWITTER_QUERY=from:vitalik.eth solana

# Search for buy signals
TWITTER_QUERY="just bought" OR "bullish on" crypto

# Target specific tokens
TWITTER_QUERY=#SOL OR #Solana new token

πŸ“ˆ Analyzing Performance

Both services maintain their own trade history logs:

Sentiment Service History

# View trade signals published
docker compose exec sentiment cat trade_history.json | tail -n 50

# Copy to local machine
docker cp sentiment:/app/trade_history.json ./sentiment_history.json

Trader Service History

# View executed trades (includes tx IDs)
docker compose exec trader cat trade_history.json 2>/dev/null || echo "No history yet"

# The trader service logs are in-memory by default
# Check logs for execution details:
docker compose logs trader | grep "Trade executed"

Redis Stream Inspection

# View pending trade orders
docker compose exec redis redis-cli XLEN trade_orders

# View recent trade results
docker compose exec redis redis-cli XREVRANGE trade_results + - COUNT 10

# Check seen tweets count
docker compose exec redis redis-cli SCARD seen_tweets

Example sentiment service record:

{
  "timestamp": "2026-02-10T21:15:30.123456",
  "token_address": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
  "sentiment": "bullish",
  "confidence": 75,
  "credibility_score": 85,
  "reasoning": "Clear buy signal with verified Solana token address",
  "executed": false,
  "paper_trade": true,
  "amount_sol": 0.0001
}

Example trader service execution log:

INFO Received trade order [a1b2c3d4]: 100000 lamports β†’ 7xKXt... (paper=True)
INFO PAPER TRADE logged: 0.0001 SOL β†’ 7xKXt...
INFO Result published: paper_executed

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Docker Compose Stack                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Redis           β”‚  Sentiment       β”‚  Trader          β”‚  Ollama    β”‚
β”‚  Container       β”‚  Service         β”‚  Service         β”‚  Container β”‚
β”‚                  β”‚                  β”‚                  β”‚            β”‚
β”‚  β€’ Streams       β”‚  β€’ Tweet fetch   β”‚  β€’ Order consume β”‚  β€’ LLM API β”‚
β”‚  β€’ Pub/Sub       β”‚  β€’ Analysis      β”‚  β€’ Swap execute  β”‚  β€’ gemma3  β”‚
β”‚  β€’ Dedup cache   β”‚  β€’ Order publish β”‚  β€’ Result report β”‚            β”‚
β”‚  Port: 6379      β”‚  Port: 8000      β”‚  Port: 8001      β”‚ Port: 11434β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                  β”‚                  β”‚
         β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚    β”‚             β”‚                  β”‚        β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β–Όβ”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”‚
    β”‚  Stream 1  β”‚   β”‚ Twitter API β”‚   β”‚  LLM API    β”‚ β”‚
    β”‚trade_ordersβ”‚   β”‚ (RapidAPI)  β”‚   β”‚  (Ollama)   β”‚ β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
         β”‚                                              β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
    β”‚  Stream 2   β”‚                          β”‚  Solana RPC    β”‚
    β”‚trade_resultsβ”‚                          β”‚   (Raydium)    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Redis Set  β”‚
    β”‚seen_tweets β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Service Responsibilities

Sentiment Service:

  • Twitter monitoring and tweet fetching
  • LLM-based sentiment analysis
  • Token address extraction and validation
  • Credibility scoring
  • Trade signal generation
  • Publishing orders to Redis
  • Receiving execution confirmations

Trader Service:

  • Consuming trade orders from Redis
  • Executing Solana swaps via Raydium
  • Managing Solana wallet and transactions
  • Publishing execution results
  • Paper trading simulation

Redis:

  • Message broker between services
  • trade_orders stream (Sentiment β†’ Trader)
  • trade_results stream (Trader β†’ Sentiment)
  • seen_tweets set for deduplication
  • Reliable delivery with consumer groups

Frontend:

  • React dashboard (port 3000)
  • Real-time logs via WebSocket
  • Start/stop bot controls
  • Trade history display
  • Connects to Sentiment service API

πŸ”’ Security Best Practices

  1. Never commit .env file - it contains your private keys

    echo ".env" >> .gitignore
  2. Use dedicated trading wallet - don't use your main wallet

    solana-keygen new -o trading-wallet.json
  3. Start with small amounts - test with minimal SOL first

    # In main.py, adjust SOL_AMOUNT
    SOL_AMOUNT = int(0.0001 * LAMPORTS_PER_SOL)  # 0.0001 SOL
  4. Monitor actively - watch logs when first deploying

    docker compose logs -f trading-bot
  5. Regular backups - save trade history periodically

    docker cp trading-bot:/app/trade_history.json ./backups/

πŸ› οΈ Advanced Usage

Monitoring Individual Services

Each service has its own API for monitoring:

# Sentiment service status
curl http://localhost:8000/api/status

# Trader service status  
curl http://localhost:8001/api/status

# View real-time logs via WebSocket
# Connect to ws://localhost:8000/ws/logs (sentiment)
# Connect to ws://localhost:8001/ws/logs (trader)

Scaling the Architecture

The microservices architecture allows independent scaling:

Multiple Sentiment Services (different queries):

# docker-compose.yml
services:
  sentiment-sol:
    extends: sentiment
    container_name: sentiment-sol
    environment:
      - TWITTER_QUERY=solana new token
      
  sentiment-memes:
    extends: sentiment
    container_name: sentiment-memes
    environment:
      - TWITTER_QUERY=memecoin launch

Multiple Trader Services (load distribution):

services:
  trader-1:
    extends: trader
    container_name: trader-1
    
  trader-2:
    extends: trader
    container_name: trader-2

Both will consume from the same Redis stream using consumer groups, automatically load-balancing orders.

Using Custom LLM Models

The sentiment service supports any Ollama-compatible model:

# Pull a different model
docker exec -it ollama ollama pull mistral

# Update .env
OLLAMA_MODEL=mistral

# Restart sentiment service
docker compose restart sentiment

Redis Persistence

To persist Redis data across restarts:

# docker-compose.yml
services:
  redis:
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes

Integration with Trading Dashboard

Export trades for analysis:

import json
import pandas as pd

# Combine data from both services
with open('sentiment_history.json') as f:
    signals = json.load(f)
    
with open('trader_history.json') as f:
    executions = json.load(f)
    
df_signals = pd.DataFrame(signals)
df_executions = pd.DataFrame(executions)

# Merge on order_id or timestamp
print(df_signals.describe())
print(df_executions.describe())

πŸ› Troubleshooting

Services Not Communicating

Symptom: Sentiment service publishes orders but trader doesn't execute

# Check Redis is running
docker compose ps redis

# Verify Redis connection
docker compose exec redis redis-cli ping

# Check trade_orders stream
docker compose exec redis redis-cli XLEN trade_orders

# If orders are piling up, check trader logs
docker compose logs trader

Duplicate Tweet Processing

Symptom: Same tweet analyzed multiple times

# Check seen_tweets set size
docker compose exec redis redis-cli SCARD seen_tweets

# Verify TTL is set
docker compose exec redis redis-cli TTL seen_tweets

# Clear seen tweets if needed (will reprocess everything)
docker compose exec redis redis-cli DEL seen_tweets

Sentiment Service Issues

LLM not detecting token addresses:

  • Try more specific search query: TWITTER_QUERY="CA:" solana
  • Use a more powerful model: OLLAMA_MODEL=llama3
  • Check Ollama logs: docker compose logs ollama

No tweets found:

  • Check RapidAPI subscription status
  • Verify RAPIDAPI_KEY is correct
  • Try broader search query
  • Check sentiment service logs: docker compose logs sentiment

All tweets already processed:

  • This is normal! The system is working correctly
  • Wait for new tweets matching your query
  • Reduce POLL_INTERVAL to check more frequently
  • Or change TWITTER_QUERY to search different content

Trader Service Issues

Trades not executing:

  • Check PAPER_TRADING=false for live trading
  • Verify trader service is running: docker compose ps trader
  • Check wallet has SOL balance
  • Review trader logs: docker compose logs trader

Order consumption errors:

  • Restart trader service: docker compose restart trader
  • Check Redis consumer groups: docker compose exec redis redis-cli XINFO GROUPS trade_orders

Redis Issues

Connection errors:

# Restart Redis
docker compose restart redis

# Check both services reconnect
docker compose logs -f sentiment trader | grep "Redis connection"

Memory issues:

# Check Redis memory usage
docker compose exec redis redis-cli INFO memory

# Clear old data
docker compose exec redis redis-cli FLUSHDB

Ollama Issues

Model errors:

  • Increase Docker memory allocation (minimum 4GB recommended)
  • Pull model manually: docker exec -it ollama ollama pull gemma3
  • Try a smaller model: OLLAMA_MODEL=gemma2:2b

Dashboard Not Loading

# Check frontend service
docker compose ps frontend

# Verify nginx proxy config
docker compose logs frontend

# Test API endpoints directly
curl http://localhost:8000/api/status
curl http://localhost:8001/api/status

✨ Architecture Benefits

Why Microservices?

Separation of Concerns:

  • Sentiment service focuses on analysis and signal generation
  • Trader service focuses on execution and blockchain interaction
  • Each service can be developed, tested, and deployed independently

Reliability:

  • If the trader crashes, sentiment analysis continues
  • If sentiment service restarts, trader keeps processing queued orders
  • Redis streams provide guaranteed message delivery

Scalability:

  • Run multiple sentiment services for different Twitter queries
  • Run multiple trader services for parallel execution
  • Scale each component based on load

Resource Efficiency:

  • Trader service doesn't need LLM dependencies
  • Sentiment service doesn't need Solana/crypto libraries
  • Deduplication prevents wasted LLM calls on repeated tweets

Testing & Development:

  • Test services independently
  • Mock Redis streams for unit testing
  • Easy to add new services (e.g., a separate analytics service)

πŸ“„ License

MIT License - see LICENSE file for details

⚠️ Disclaimer

This bot is for educational purposes only. Cryptocurrency trading carries significant risk. Always:

  • Start with paper trading mode
  • Never invest more than you can afford to lose
  • Do your own research (DYOR)
  • Be aware of rug pulls and scams
  • This is not financial advice

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

πŸ“§ Support

For issues and questions:

  • Open a GitHub issue
  • Check existing discussions
  • Review the troubleshooting section

Built with ❀️ for the Solana community

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors