A Python-based crypto trading bot that combines technical analysis, machine learning, grid trading, and sentiment analysis to make automated trading decisions.
This bot monitors a configured trading pair, evaluates market conditions using multiple signals, and executes trades when all conditions align.
It integrates:
- 📊 Technical Indicators (SMA, EMA, RSI, MACD, Bollinger Bands)
- 🤖 Machine Learning (RandomForestClassifier)
- 📉 Grid Trading Strategy
- 🐦 Twitter/X Sentiment Analysis
- Fetches OHLCV market data using
ccxt - Computes key technical indicators using
ta - Trains a machine learning model on recent market data
- Implements a grid-based trading strategy
- Uses Twitter sentiment as an additional signal filter
- Logs all activity for monitoring and debugging
- Stores trade state locally
-
Loads API credentials from
.env -
Loads trading configuration from
config.json -
Connects to the exchange
-
Fetches market data and computes indicators
-
Trains ML model on historical data
-
Generates grid levels
-
Runs continuous loop:
- Fetch latest data
- Recalculate indicators
- Predict market movement
- Analyze sentiment
- Execute buy/sell orders if conditions match
bot.py # Main trading loop
data_fetch.py # Exchange connection & data retrieval
indicators.py # Technical indicators
grid_strategy.py # Grid trading logic
ml_strategy.py # ML model training & prediction
twitter_sentiment.py # Sentiment analysis
state_manager.py # Trade state handling
utils.py # Helper functions
config.json # Bot configuration
requirements.txt # Dependencies
- Python 3.10+
- Exchange API credentials (e.g., Binance)
- Twitter/X Bearer Token
Install dependencies:
pip install -r requirements.txtCreate a .env file:
BINANCE_API_KEY=your_api_key
BINANCE_API_SECRET=your_api_secret
TWITTER_BEARER_TOKEN=your_twitter_bearer_token- Never commit real credentials
- Use minimal permissions
- Prefer testnet/paper trading first
{
"exchange": "binance",
"symbol": "BTC/USDT",
"timeframe": "1h",
"grid_levels": 10,
"grid_step_pct": 0.5,
"order_size": 0.001,
"base_currency": "USDT",
"twitter": {
"query": "BTC OR Bitcoin -is:retweet lang:en",
"max_tweets": 80
}
}python bot.pyThe bot uses three filters together:
Trades only when price is near predefined grid levels.
- Buy → High probability of upward movement
- Sell → Low probability
- Buy → Positive sentiment
- Sell → Negative sentiment
Default thresholds:
Buy → prob_up > 0.55 AND sentiment > 0.1
Sell → prob_up < 0.45 AND sentiment < -0.1
logs/bot.log→ Logs, signals, and errorstrade_state.json→ Trade history/stateml_model.pkl→ Saved ML model
- Executes real market orders
- No backtesting pipeline yet
- ML model is basic (no validation)
- Limited error handling (fees, slippage, API failures)
- Sentiment model may be slow on first run
- Add backtesting module
- Support testnet/paper trading
- Improve risk management
- Save scaler/model pipeline
- Add alerts & monitoring
- Write unit tests
This project is for educational purposes only. Cryptocurrency trading is highly risky. Use at your own risk.
- Python
- ccxt
- ta (Technical Analysis)
- scikit-learn
- Transformers (for sentiment analysis)