Skip to content

hellpuffyt/ml-tradingbot

Repository files navigation

ML-Powered Trading Bot with Sentiment Analysis

Python License Status

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.

🚀 Features

  • 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

📊 Technology Stack

Core Trading Framework

  • LumiBot - Python algorithmic trading framework for backtesting and live trading
  • Alpaca Trading API - Commission-free stock trading broker with paper trading support

Machine Learning & NLP

  • 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

Data & Analysis

Development & Utilities

  • Python 3.13 - Latest Python runtime
  • CUDA/GPU Support - Optional GPU acceleration for sentiment analysis
  • WebSockets - Real-time market data streaming

🏗️ Architecture

┌─────────────────────────────────────────────────────────┐
│         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                      │            │
│  └────────────────────────────────────────┘            │
│                                                          │
└─────────────────────────────────────────────────────────┘

📋 Prerequisites

System Requirements

  • Python 3.13 or higher
  • 4GB+ RAM (8GB+ recommended for model inference)
  • GPU (optional): NVIDIA GPU with CUDA support for faster sentiment analysis

Required Accounts

  1. Alpaca Trading Account - Sign up here
    • API Key
    • Secret Key
    • Paper trading enabled (recommended for testing)

🔧 Installation

1. Clone the Repository

git clone https://github.com/yourusername/ml-trading-bot.git
cd ml-trading-bot

2. Create Virtual Environment (Recommended)

# Windows
python -m venv venv
venv\Scripts\activate

# macOS/Linux
python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

Core Dependencies:

# 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 websockets

4. Download Pre-trained Models

The 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!')"

⚙️ Configuration

1. Update Alpaca API Credentials

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
}

2. Strategy Parameters

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
    }
)

3. Backtesting Period

Update date ranges:

start_date = datetime(2025, 4, 2)   # Backtest start
end_date = datetime(2026, 3, 31)    # Backtest end

4. Sentiment Thresholds

Adjust sentiment confidence thresholds in on_trading_iteration():

if sentiment == "positive" and probability > 0.999:  # 99.9% confidence
    # Execute buy order

🚀 Usage

Run Backtesting

Execute historical backtesting to evaluate strategy performance:

python tradingbot.py

Output Example

2026-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

Run Individual Components

Test Sentiment Analysis

python finbert_utils.py

Output:

tensor(0.8234) positive
True  # GPU available

Live Trading (Paper)

# Modify tradingbot.py to run continuously
# Requires live market hours

📁 Project Structure

ml-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

🤖 How It Works

1. News Fetching

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)

2. Sentiment Analysis

# FinBERT processes headlines
probability, sentiment = estimate_sentiment(news_headlines)
# Output: 0.82, "positive"

3. Trading Decision

if sentiment == "positive" and probability > 0.999:
    # Generate buy signal
    quantity = position_sizing()
    order = create_order(symbol, quantity, "buy")

4. Risk Management

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%
)

📊 Strategy Logic

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

🧪 Backtesting Results

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

⚠️ Disclaimers

  • 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.

🔮 Future Enhancements

  • 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)

🤝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📚 References


📜 License

This project is licensed under the MIT License - see the LICENSE file for details.



🎯 Acknowledgments

  • 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

About

AI-powered trading bot with FinBERT sentiment analysis

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages