An intelligent algorithmic trading bot that leverages Natural Language Processing (NLP) and Machine Learning to analyze market sentiment and execute data-driven trades. The bot uses FinBERT for financial news sentiment analysis integrated with the Alpaca Trading API for execution.
- Sentiment-Based Trading: Analyzes financial news using FinBERT to determine market sentiment (positive/negative/neutral)
- Automated Trade Execution: Executes buy/sell orders based on sentiment signals with configurable risk management
- Bracket Orders: Implements take-profit (TP) and stop-loss (SL) targets automatically
- Position Sizing: Dynamic position sizing based on account equity and risk tolerance
- Backtesting Engine: Historical backtesting using Yahoo Finance data (2025-2026 period)
- Real-Time News Fetching: Integrates Alpaca News API for up-to-date market news
- Paper Trading Support: Test strategies in paper trading mode before live deployment
- Configurable Strategy: Adjustable symbols, cash-at-risk percentages, and sentiment thresholds
- LumiBot - Python algorithmic trading framework for backtesting and live trading
- Alpaca Trading API - Commission-free stock trading broker with paper trading support
- FinBERT - BERT-based model fine-tuned for financial sentiment analysis
- PyTorch - Deep learning framework powering transformer models
- Transformers - HuggingFace library for pre-trained NLP models
- Pandas - Data manipulation and analysis
- NumPy - Numerical computing
- YFinance - Yahoo Finance data fetching
- Alpaca Trade API - Python SDK for Alpaca
- Python 3.13 - Latest Python runtime
- CUDA/GPU Support - Optional GPU acceleration for sentiment analysis
- WebSockets - Real-time market data streaming
┌─────────────────────────────────────────────────────────┐
│ Sentiment-Driven Trading Agent │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ News Feed │ │ Price Data │ │
│ │ (Alpaca API) │ │ (Yahoo Finance) │ │
│ └────────┬─────────┘ └────────┬─────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌────────────────────────────────────────┐ │
│ │ FinBERT Sentiment Analysis │ │
│ │ (PyTorch + Transformers) │ │
│ │ │ │
│ │ Output: {positive, negative, │ │
│ │ neutral} + confidence │ │
│ └────────┬─────────────────────────────┘ │
│ │ │
│ ┌────────▼─────────────────────────────────────┐ │
│ │ MLTrader Strategy Engine (LumiBot) │ │
│ │ • Position Sizing │ │
│ │ • Signal Generation │ │
│ │ • Risk Management (TP/SL) │ │
│ └────────┬──────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────────────┐ │
│ │ Trade Execution (Alpaca Broker) │ │
│ │ • Paper / Live Trading │ │
│ │ • Commission-Free │ │
│ └────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘
- Python 3.13 or higher
- 4GB+ RAM (8GB+ recommended for model inference)
- GPU (optional): NVIDIA GPU with CUDA support for faster sentiment analysis
- Alpaca Trading Account - Sign up here
- API Key
- Secret Key
- Paper trading enabled (recommended for testing)
git clone https://github.com/yourusername/ml-trading-bot.git
cd ml-trading-bot# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txt# Trading & Data
pip install lumibot alpaca-trade-api yfinance pandas numpy
# ML & NLP
pip install torch torchvision torchaudio transformers
pip install quantstats-lumi
# Utilities
pip install websocketsThe first run will automatically download the FinBERT model (~438MB). Subsequent runs will use cached models.
# Optional: Pre-download manually
python -c "from finbert_utils import estimate_sentiment; print('FinBERT ready!')"Edit tradingbot.py and replace with your credentials:
API_KEY = "YOUR_ALPACA_API_KEY"
API_SECRET = "YOUR_ALPACA_SECRET_KEY"
BASE_URL = "https://paper-api.alpaca.markets" # Paper trading
# For live trading, use:
# BASE_URL = "https://api.alpaca.markets"
ALPACA_CREDS = {
"API_KEY": API_KEY,
"API_SECRET": API_SECRET,
"PAPER": True # Set to False for live trading
}Modify the backtest initialization:
strategy = MLTrader(
name="mlstrat",
broker=broker,
parameters={
"symbol": "SPY", # Trading symbol
"cash_at_risk": 0.05 # Risk 5% of capital per trade
}
)Update date ranges:
start_date = datetime(2025, 4, 2) # Backtest start
end_date = datetime(2026, 3, 31) # Backtest endAdjust sentiment confidence thresholds in on_trading_iteration():
if sentiment == "positive" and probability > 0.999: # 99.9% confidence
# Execute buy orderExecute historical backtesting to evaluate strategy performance:
python tradingbot.py2026-04-02 00:55:43,988 | INFO | LumiBot v4.4.58 starting
Sentiment: positive, Confidence: 0.82
Sentiment: neutral, Confidence: 1.00
Sentiment: negative, Confidence: 0.76
Progress |----------| 1.56% [Elapsed: 0:00:02 ETA: 0:02:54] | Sim Time: 2025-04-15
python finbert_utils.pyOutput:
tensor(0.8234) positive
True # GPU available
# Modify tradingbot.py to run continuously
# Requires live market hoursml-trading-bot/
├── README.md # Project documentation
├── requirements.txt # Python dependencies
├── tradingbot.py # Main trading strategy & execution
├── finbert_utils.py # FinBERT sentiment analysis utilities
├── .gitignore # Git ignore file
└── results/ # Backtest results (auto-generated)
├── plots/
└── metrics.csv
def get_sentiment(self):
# Fetch news from past 3 days
today, three_days_prior = self.get_dates()
news = self.api_.get_news(symbol=self.symbol,
start=three_days_prior,
end=today)# FinBERT processes headlines
probability, sentiment = estimate_sentiment(news_headlines)
# Output: 0.82, "positive"if sentiment == "positive" and probability > 0.999:
# Generate buy signal
quantity = position_sizing()
order = create_order(symbol, quantity, "buy")order = self.create_order(
self.symbol,
quantity,
"buy",
type="bracket",
take_profit_price=last_price * 1.20, # Sell at +20%
stop_loss_price=last_price * 0.95, # Exit at -5%
)| Sentiment | Confidence | Action | Take Profit | Stop Loss |
|---|---|---|---|---|
| Positive | > 99.9% | BUY | +20% | -5% |
| Negative | > 99.9% | SELL/EXIT | N/A | N/A |
| Neutral | Any | HOLD | N/A | N/A |
Example backtest output (2025-2026):
Initial Capital: $100,000
Final Portfolio Value: $115,000
Total Return: 15%
Win Rate: 62%
Max Drawdown: -8%
Sharpe Ratio: 1.45
- Risk Warning: This bot trades real or paper money. Use paper trading first.
- No Guarantee: Past performance does not guarantee future results.
- Market Risk: Sentiment analysis can fail during volatile market conditions.
- API Limits: Alpaca has rate limits; adjust sleep times accordingly.
- API Keys: Never commit API keys to version control.
- Multi-symbol portfolio tracking
- Technical indicators integration (RSI, MACD, Bollinger Bands)
- Advanced sentiment weighting (source credibility)
- Real-time WebSocket streaming
- Performance dashboard/visualization
- Hyperparameter optimization
- Monte Carlo simulation
- Docker containerization
- Cloud deployment (AWS/GCP)
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Alpaca Markets - Commission-free trading API
- ProsusAI - FinBERT model
- LumiWealth - LumiBot framework
- HuggingFace - Transformers library
Last Updated: April 2026
Status: ✅ Production Ready (Paper Trading)
Python Version: 3.13